diff options
author | Maxime “pep” Buquet <pep@bouah.net> | 2022-03-16 08:01:36 +0100 |
---|---|---|
committer | Maxime “pep” Buquet <pep@bouah.net> | 2022-08-21 17:19:58 +0200 |
commit | 429c382f608dcf298931ce679e30ae1e6b702c37 (patch) | |
tree | 724cb658da3b22479159c6051e99fae211bef362 /src/theming.rs | |
parent | 8600e053e447091a16862aa91a58ba216a052c38 (diff) | |
download | poezio-429c382f608dcf298931ce679e30ae1e6b702c37.tar.gz poezio-429c382f608dcf298931ce679e30ae1e6b702c37.tar.bz2 poezio-429c382f608dcf298931ce679e30ae1e6b702c37.tar.xz poezio-429c382f608dcf298931ce679e30ae1e6b702c37.zip |
roezio: Update nom from 4 to 7.1
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
Diffstat (limited to 'src/theming.rs')
-rw-r--r-- | src/theming.rs | 55 |
1 files changed, 36 insertions, 19 deletions
diff --git a/src/theming.rs b/src/theming.rs index ff108663..a812a732 100644 --- a/src/theming.rs +++ b/src/theming.rs @@ -1,5 +1,12 @@ use enum_set::{CLike, EnumSet}; use ncurses::{attr_t, init_pair, A_BLINK, A_BOLD, A_ITALIC, A_UNDERLINE, COLORS, COLOR_PAIR}; +use nom::{ + branch::alt, + bytes::complete::tag, + error::{Error as NomError, ErrorKind, ParseError}, + multi::many0, + Err as NomErr, IResult, +}; use std::collections::HashMap; use std::mem; use std::sync::Mutex; @@ -34,22 +41,32 @@ impl CLike for Attr { } } -named!( - pub(crate) parse_attrs<&str, EnumSet<Attr>>, - do_parse!( - vec: many0!(alt_complete!( - tag!("b") => { |_| Attr::Bold } | - tag!("i") => { |_| Attr::Italic } | - tag!("u") => { |_| Attr::Underline } | - tag!("a") => { |_| Attr::Blink } - )) >> - ({ - let mut set = EnumSet::new(); - set.extend(vec); - set - }) - ) -); +fn parse_attr(input: &str) -> IResult<&str, Attr> { + let (input, attr) = alt((tag("b"), tag("i"), tag("u"), tag("a")))(input)?; + + Ok(( + input, + match attr { + "b" => Attr::Bold, + "i" => Attr::Italic, + "u" => Attr::Underline, + "a" => Attr::Blink, + _ => { + return Err(NomErr::Error(NomError::from_error_kind( + input, + ErrorKind::Tag, + ))) + } + }, + )) +} + +pub(crate) fn parse_attrs<'a>(input: &'a str) -> Result<EnumSet<Attr>, NomErr<NomError<&'a str>>> { + let (_, vec) = many0(parse_attr)(input)?; + let mut set = EnumSet::new(); + set.extend(vec); + Ok(set) +} lazy_static! { // TODO: probably replace that mutex with an atomic. @@ -135,7 +152,7 @@ mod tests { let attrs = ""; let expected = EnumSet::new(); let received = parse_attrs(attrs).unwrap(); - assert_eq!(received.1, expected); + assert_eq!(received, expected); } #[test] @@ -144,7 +161,7 @@ mod tests { let mut expected = EnumSet::new(); expected.insert(Attr::Bold); let received = parse_attrs(attrs).unwrap(); - assert_eq!(received.1, expected); + assert_eq!(received, expected); } #[test] @@ -156,6 +173,6 @@ mod tests { expected.insert(Attr::Italic); expected.insert(Attr::Underline); let received = parse_attrs(attrs).unwrap(); - assert_eq!(received.1, expected); + assert_eq!(received, expected); } } |