summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxime “pep” Buquet <pep@bouah.net>2022-03-16 08:01:36 +0100
committerMaxime “pep” Buquet <pep@bouah.net>2022-08-21 17:19:58 +0200
commit429c382f608dcf298931ce679e30ae1e6b702c37 (patch)
tree724cb658da3b22479159c6051e99fae211bef362
parent8600e053e447091a16862aa91a58ba216a052c38 (diff)
downloadpoezio-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>
-rw-r--r--Cargo.toml2
-rw-r--r--src/lib.rs11
-rw-r--r--src/theming.rs55
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<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);
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);
}
}