avoid parsing if string is empty

This commit is contained in:
Breval Ferrari 2024-11-07 21:12:27 -05:00
parent 2176f5dd89
commit 304a360608
No known key found for this signature in database
GPG key ID: 6FED68D87C479A59
2 changed files with 8 additions and 4 deletions

View file

@ -33,7 +33,7 @@ pub use de::*;
use super::Expression as Instruction; use super::Expression as Instruction;
#[derive(Deref, From)] #[derive(Deref, From, Default)]
#[cfg_attr(debug_assertions, derive(Serialize, Debug))] #[cfg_attr(debug_assertions, derive(Serialize, Debug))]
pub struct Atoms(Vec<Atom>); pub struct Atoms(Vec<Atom>);

View file

@ -2,7 +2,7 @@ use derive_new::new;
use nom::{ use nom::{
character::complete::one_of, character::complete::one_of,
combinator::all_consuming, combinator::all_consuming,
multi::many0, multi::{many0, many1},
sequence::{preceded, terminated}, sequence::{preceded, terminated},
Parser, Parser,
}; };
@ -108,9 +108,13 @@ impl<'de> Deserialize<'de> for Atoms {
const FIELDS: &[&str] = &["notes", "sheet"]; const FIELDS: &[&str] = &["notes", "sheet"];
let NotesSheet { notes, sheet } = let NotesSheet { notes, sheet } =
deserializer.deserialize_struct("NotesSheet", FIELDS, NotesSheetVisitor)?; deserializer.deserialize_struct("NotesSheet", FIELDS, NotesSheetVisitor)?;
if sheet.is_empty() {
Ok(Default::default())
} else {
flat_atom_parser_mapper::<D, _>(&sheet, flat_atom_parser(&notes)) flat_atom_parser_mapper::<D, _>(&sheet, flat_atom_parser(&notes))
} }
} }
}
fn maybe_yml_str_space<'a, E>() -> impl Parser<&'a str, Vec<char>, E> fn maybe_yml_str_space<'a, E>() -> impl Parser<&'a str, Vec<char>, E>
where where
@ -128,7 +132,7 @@ where
P: Parser<&'a str, FlatAtom, nom::error::Error<&'a str>>, P: Parser<&'a str, FlatAtom, nom::error::Error<&'a str>>,
{ {
all_consuming(terminated( all_consuming(terminated(
many0(preceded(maybe_yml_str_space(), parser)), many1(preceded(maybe_yml_str_space(), parser)),
maybe_yml_str_space(), maybe_yml_str_space(),
))(input) ))(input)
.map_err(nom_err_message) .map_err(nom_err_message)