Compare commits

..

No commits in common. "78d7c493bb2e89e811cf1ab3b4f789e1cb369ab9" and "2176f5dd8949232bc44cd5fa09a75394832cc055" have entirely different histories.

2 changed files with 51 additions and 8 deletions

View file

@ -33,7 +33,7 @@ pub use de::*;
use super::Expression as Instruction; use super::Expression as Instruction;
#[derive(Deref, From, Default)] #[derive(Deref, From)]
#[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, many1}, multi::many0,
sequence::{preceded, terminated}, sequence::{preceded, terminated},
Parser, Parser,
}; };
@ -60,12 +60,55 @@ impl<'de> Deserialize<'de> for Atoms {
notes: String, notes: String,
sheet: String, sheet: String,
} }
let NotesSheet { notes, sheet } = NotesSheet::deserialize(deserializer)?; struct NotesSheetVisitor;
if sheet.is_empty() { impl<'de> Visitor<'de> for NotesSheetVisitor {
Ok(Default::default()) type Value = NotesSheet;
} else { fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
flat_atom_parser_mapper::<D, _>(&sheet, flat_atom_parser(&notes)) formatter.write_str("a \"notes\" field and a \"sheet\" field")
}
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))
} }
} }
@ -85,7 +128,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(
many1(preceded(maybe_yml_str_space(), parser)), many0(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)