From 78d7c493bb2e89e811cf1ab3b4f789e1cb369ab9 Mon Sep 17 00:00:00 2001 From: p6nj Date: Thu, 7 Nov 2024 21:43:32 -0500 Subject: [PATCH] simplify Deserialize impl for Atoms --- src/bng/score/de.rs | 49 +-------------------------------------------- 1 file changed, 1 insertion(+), 48 deletions(-) diff --git a/src/bng/score/de.rs b/src/bng/score/de.rs index e9c16a0..aa97ecd 100644 --- a/src/bng/score/de.rs +++ b/src/bng/score/de.rs @@ -60,54 +60,7 @@ impl<'de> Deserialize<'de> for Atoms { notes: String, sheet: String, } - struct NotesSheetVisitor; - impl<'de> Visitor<'de> for NotesSheetVisitor { - type Value = NotesSheet; - fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { - formatter.write_str("a \"notes\" field and a \"sheet\" field") - } - fn visit_seq(self, mut seq: A) -> Result - 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(self, mut map: A) -> Result - 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)?; + let NotesSheet { notes, sheet } = NotesSheet::deserialize(deserializer)?; if sheet.is_empty() { Ok(Default::default()) } else {