From 304a360608947e04f8d20caf017cafef9b51c08a Mon Sep 17 00:00:00 2001 From: p6nj Date: Thu, 7 Nov 2024 21:12:27 -0500 Subject: [PATCH] avoid parsing if string is empty --- src/bng/score.rs | 2 +- src/bng/score/de.rs | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/bng/score.rs b/src/bng/score.rs index 4aadc1e..84ba46c 100644 --- a/src/bng/score.rs +++ b/src/bng/score.rs @@ -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); diff --git a/src/bng/score/de.rs b/src/bng/score/de.rs index 1745d76..e9c16a0 100644 --- a/src/bng/score/de.rs +++ b/src/bng/score/de.rs @@ -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::(&sheet, flat_atom_parser(¬es)) + if sheet.is_empty() { + Ok(Default::default()) + } else { + flat_atom_parser_mapper::(&sheet, flat_atom_parser(¬es)) + } } } @@ -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)