Compare commits

...

2 commits

Author SHA1 Message Date
78d7c493bb
simplify Deserialize impl for Atoms 2024-11-07 21:43:32 -05:00
304a360608
avoid parsing if string is empty 2024-11-07 21:12:27 -05:00
2 changed files with 8 additions and 51 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,
}; };
@ -60,55 +60,12 @@ impl<'de> Deserialize<'de> for Atoms {
notes: String, notes: String,
sheet: String, sheet: String,
} }
struct NotesSheetVisitor; let NotesSheet { notes, sheet } = NotesSheet::deserialize(deserializer)?;
impl<'de> Visitor<'de> for NotesSheetVisitor { if sheet.is_empty() {
type Value = NotesSheet; Ok(Default::default())
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { } else {
formatter.write_str("a \"notes\" field and a \"sheet\" field") flat_atom_parser_mapper::<D, _>(&sheet, flat_atom_parser(&notes))
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let notes = seq
.next_element()?
.ok_or_else(|| de::Error::invalid_length(0, &self))?;
let sheet = seq
.next_element()?
.ok_or_else(|| de::Error::invalid_length(1, &self))?;
Ok(NotesSheet::new(notes, sheet))
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: de::MapAccess<'de>,
{
let mut notes = None;
let mut sheet = None;
while let Some(key) = map.next_key()? {
match key {
Field::Notes => {
if notes.is_some() {
return Err(de::Error::duplicate_field("notes"));
}
notes = Some(map.next_value()?);
}
Field::Sheet => {
if sheet.is_some() {
return Err(de::Error::duplicate_field("sheet"));
}
sheet = Some(map.next_value()?);
}
}
}
let notes = notes.ok_or_else(|| de::Error::missing_field("notes"))?;
let sheet = sheet.ok_or_else(|| de::Error::missing_field("sheet"))?;
Ok(NotesSheet::new(notes, sheet))
}
} }
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))
} }
} }
@ -128,7 +85,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)