Skip to content

Commit

Permalink
Fix PartList element
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed Jun 13, 2024
1 parent 4238c1b commit ddd8e98
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 40 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resolver = "2"

[workspace.package]
edition = "2021"
version = "1.0.1"
version = "1.0.2"
authors = ["Will Hedgecock <[email protected]>"]
license = "MIT"
documentation = "https://docs.rs/musicxml/latest/"
Expand All @@ -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
Expand Down
57 changes: 20 additions & 37 deletions musicxml/src/elements/part_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(PartGroup),
/// The [ScorePart] element identifies a part in this score.
pub score_part: ScorePart,
/// Additional [PartGroup] elements.
pub additional_part_group: Vec<PartGroup>,
/// Additional [ScorePart] elements.
pub additional_score_part: Vec<ScorePart>,
ScorePart(ScorePart),
}

/// Contents of the [PartList] element.
#[derive(Debug, PartialEq, Eq)]
pub struct PartListContents {
/// Ordered list of [PartList] content elements.
pub content: Vec<PartListElement>,
}

impl ContentDeserializer for PartListContents {
fn deserialize(elements: &Vec<XmlElement>) -> Result<Self, String> {
let mut part_group: Vec<PartGroup> = Vec::new();
let mut score_part: Option<ScorePart> = None;
let mut additional_part_group: Vec<PartGroup> = Vec::new();
let mut additional_score_part: Vec<ScorePart> = 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 <part-list> 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<XmlElement> {
let mut elements: Vec<XmlElement> = 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
}
Expand Down

0 comments on commit ddd8e98

Please sign in to comment.