From 429c382f608dcf298931ce679e30ae1e6b702c37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Wed, 16 Mar 2022 08:01:36 +0100 Subject: [PATCH] roezio: Update nom from 4 to 7.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- Cargo.toml | 2 +- src/lib.rs | 11 +++++----- src/theming.rs | 55 +++++++++++++++++++++++++++++++++----------------- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index da78abef..fa1271f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ authors = [ [dependencies] pyo3 = { version = "0.16", features = ["extension-module"] } -nom = "4" +nom = "7.1" chrono = "0.4" ncurses = "5" lazy_static = "1" diff --git a/src/lib.rs b/src/lib.rs index 1f4dea46..78ced5a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,6 @@ -extern crate pyo3; -#[macro_use] -extern crate nom; extern crate ncurses; +extern crate nom; +extern crate pyo3; #[macro_use] extern crate lazy_static; extern crate enum_set; @@ -33,14 +32,14 @@ macro_rules! py_object { }; } -fn nom_to_py_err(py: Python, err: nom::Err<&str>) -> PyErr { - LogParseError::new_err(py_object!(py, err.into_error_kind().description())) +fn nom_to_py_err(py: Python, err: nom::Err>) -> PyErr { + LogParseError::new_err(py_object!(py, err.to_string())) } #[pyfunction] fn to_curses_attr(py: Python, fg: i16, bg: i16, attrs: &str) -> PyResult { let attrs = match parse_attrs(attrs) { - Ok(attrs) => attrs.1, + Ok(attrs) => attrs, Err(err) => return Err(nom_to_py_err(py, err)), }; let result = curses_attr(fg, bg, attrs); 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>, - 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, NomErr>> { + 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); } }