roezio: Update nom from 4 to 7.1

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2022-03-16 08:01:36 +01:00
parent 8600e053e4
commit 429c382f60
3 changed files with 42 additions and 26 deletions

View file

@ -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"

View file

@ -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<nom::error::Error<&str>>) -> PyErr {
LogParseError::new_err(py_object!(py, err.to_string()))
}
#[pyfunction]
fn to_curses_attr(py: Python, fg: i16, bg: i16, attrs: &str) -> PyResult<PyObject> {
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);

View file

@ -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);
}
}