root parser

This commit is contained in:
Ponj 2024-11-09 17:59:49 -05:00
parent bdb42ac846
commit 315238f14d
Signed by: p6nj
GPG key ID: 6FED68D87C479A59

View file

@ -1,13 +1,16 @@
use std::fmt::{Debug, Display};
use std::{
fmt::{Debug, Display},
num::TryFromIntError,
};
use derive_new::new;
use nom::{
character::complete::one_of,
combinator::all_consuming,
error::{context, convert_error, ContextError, ParseError, VerboseError},
error::{context, convert_error, ContextError, FromExternalError, ParseError, VerboseError},
multi::{many0, many1},
sequence::{preceded, terminated},
Needed, Parser,
IResult, Needed, Parser,
};
use serde::{
de::{self, Deserializer, Visitor},
@ -50,23 +53,20 @@ impl<'de> Deserialize<'de> for Atoms {
if sheet.is_empty() {
Ok(Default::default())
} else {
all_consuming(terminated(
many1(preceded(maybe_yml_str_space(), atom(&notes))),
maybe_yml_str_space(),
))(&sheet)
.map_err(|e: nom::Err<VerboseError<&str>>| match e {
nom::Err::Incomplete(Needed::Unknown) => "needed some more bytes".to_string(),
nom::Err::Incomplete(Needed::Size(n)) => format!("needed {} more bytes", n),
nom::Err::Error(e) | nom::Err::Failure(e) => convert_error(sheet.as_str(), e),
})
.map_err(AtomsSerializeError::Parsing)
.map_err(de::Error::custom)
.and_then(|(_, v)| {
inflate(v)
.map_err(AtomsSerializeError::from)
.map_err(de::Error::custom)
})
.map(Atoms)
root(&sheet, &notes)
.map_err(|e: nom::Err<VerboseError<&str>>| match e {
nom::Err::Incomplete(Needed::Unknown) => "needed some more bytes".to_string(),
nom::Err::Incomplete(Needed::Size(n)) => format!("needed {} more bytes", n),
nom::Err::Error(e) | nom::Err::Failure(e) => convert_error(sheet.as_str(), e),
})
.map_err(AtomsSerializeError::Parsing)
.map_err(de::Error::custom)
.and_then(|(_, v)| {
inflate(v)
.map_err(AtomsSerializeError::from)
.map_err(de::Error::custom)
})
.map(Atoms)
}
}
}
@ -77,3 +77,16 @@ where
{
context("yml white space", many0(one_of(" \t\r\n")))
}
fn root<'a, E>(i: &'a str, notes: &'a str) -> IResult<&'a str, Vec<FlatAtom>, E>
where
E: ParseError<&'a str>
+ ContextError<&'a str>
+ FromExternalError<&'a str, TryFromIntError>
+ FromExternalError<&'a str, E>,
{
all_consuming(terminated(
many1(preceded(maybe_yml_str_space(), atom(notes))),
maybe_yml_str_space(),
))(i)
}