avoid parsing if string is empty

This commit is contained in:
Ponj 2024-11-07 21:12:27 -05:00
parent 2176f5dd89
commit 304a360608
Signed by: p6nj
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;
#[derive(Deref, From)]
#[derive(Deref, From, Default)]
#[cfg_attr(debug_assertions, derive(Serialize, Debug))]
pub struct Atoms(Vec<Atom>);

View file

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