From ddd8e98dc3824fc8dabd8545f366daa96225411f Mon Sep 17 00:00:00 2001 From: Will Hedgecock Date: Thu, 13 Jun 2024 17:42:00 -0500 Subject: [PATCH] Fix PartList element --- Cargo.toml | 6 ++-- musicxml/src/elements/part_list.rs | 57 +++++++++++------------------- 2 files changed, 23 insertions(+), 40 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cb7edd9..275a4c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ resolver = "2" [workspace.package] edition = "2021" -version = "1.0.1" +version = "1.0.2" authors = ["Will Hedgecock "] license = "MIT" documentation = "https://docs.rs/musicxml/latest/" @@ -16,8 +16,8 @@ categories = ["api-bindings", "encoding", "parsing"] publish = true [workspace.dependencies] -musicxml_internal = { path = "musicxml_internal", version = "1.0.1" } -musicxml_macros = { path = "musicxml_macros", version = "1.0.1" } +musicxml_internal = { path = "musicxml_internal", version = "1.0.2" } +musicxml_macros = { path = "musicxml_macros", version = "1.0.2" } [profile.release] codegen-units = 4096 diff --git a/musicxml/src/elements/part_list.rs b/musicxml/src/elements/part_list.rs index c6fd5d1..90259bd 100644 --- a/musicxml/src/elements/part_list.rs +++ b/musicxml/src/elements/part_list.rs @@ -2,63 +2,46 @@ use super::{PartGroup, ScorePart}; use musicxml_internal::*; use musicxml_macros::*; -/// Contents of the [PartList] element. +/// The [PartListElement] specifies all possible elements available for use in a [PartList] element. #[derive(Debug, PartialEq, Eq)] -pub struct PartListContents { +pub enum PartListElement { /// The [PartGroup] element indicates a group of parts that is bracketed together. - pub part_group: Vec, + PartGroup(PartGroup), /// The [ScorePart] element identifies a part in this score. - pub score_part: ScorePart, - /// Additional [PartGroup] elements. - pub additional_part_group: Vec, - /// Additional [ScorePart] elements. - pub additional_score_part: Vec, + ScorePart(ScorePart), +} + +/// Contents of the [PartList] element. +#[derive(Debug, PartialEq, Eq)] +pub struct PartListContents { + /// Ordered list of [PartList] content elements. + pub content: Vec, } impl ContentDeserializer for PartListContents { fn deserialize(elements: &Vec) -> Result { - let mut part_group: Vec = Vec::new(); - let mut score_part: Option = None; - let mut additional_part_group: Vec = Vec::new(); - let mut additional_score_part: Vec = Vec::new(); + let mut content = PartListContents { content: Vec::new() }; for element in elements { if element.name == "part-group" { - if score_part.is_some() { - additional_part_group.push(PartGroup::deserialize(element)?); - } else { - part_group.push(PartGroup::deserialize(element)?); - } + content.content.push(PartListElement::PartGroup(PartGroup::deserialize(element)?)); } else if element.name == "score-part" { - if score_part.is_some() { - additional_score_part.push(ScorePart::deserialize(element)?); - } else { - score_part = Some(ScorePart::deserialize(element)?); - } + content.content.push(PartListElement::ScorePart(ScorePart::deserialize(element)?)); } else { return Err(format!("Unexpected element '{}'", element.name)); } } - Ok(PartListContents { - part_group, - score_part: score_part.unwrap(), - additional_part_group, - additional_score_part, - }) + Ok(content) } } impl ContentSerializer for PartListContents { fn serialize(element: &Self) -> Vec { let mut elements: Vec = Vec::new(); - for el in &element.part_group { - elements.push(PartGroup::serialize(el)); - } - elements.push(ScorePart::serialize(&element.score_part)); - for el in &element.additional_part_group { - elements.push(PartGroup::serialize(el)); - } - for el in &element.additional_score_part { - elements.push(ScorePart::serialize(el)); + for el in &element.content { + match el { + PartListElement::PartGroup(el) => elements.push(PartGroup::serialize(el)), + PartListElement::ScorePart(el) => elements.push(ScorePart::serialize(el)), + } } elements }