note_pen/
score.rs

1use crate::part::Part;
2use std::fmt::Display;
3
4#[derive(Clone)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum ScoreCreditKey {
7    Composer,
8    Lyricist,
9    Arranger,
10    Transcriber,
11    Translator,
12    Poet,
13    Contributor,
14    Publisher,
15    Other(String),
16}
17
18impl Display for ScoreCreditKey {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        match self {
21            ScoreCreditKey::Composer => write!(f, "Composer"),
22            ScoreCreditKey::Lyricist => write!(f, "Lyricist"),
23            ScoreCreditKey::Arranger => write!(f, "Arranger"),
24            ScoreCreditKey::Transcriber => write!(f, "Transcriber"),
25            ScoreCreditKey::Translator => write!(f, "Translator"),
26            ScoreCreditKey::Poet => write!(f, "Poet"),
27            ScoreCreditKey::Contributor => write!(f, "Contributor"),
28            ScoreCreditKey::Publisher => write!(f, "Publisher"),
29            ScoreCreditKey::Other(s) => write!(f, "{}", s),
30        }
31    }
32}
33
34/// A generic storage class for credits, such as for the composer, arranger, lyricist, etc.
35///
36/// There can be multiple values for a single key, such as multiple composers,
37/// this should be used instead of commas.
38#[derive(Clone)]
39#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
40pub struct ScoreCredit {
41    pub key: ScoreCreditKey,
42    pub value: Vec<String>,
43}
44
45impl Display for ScoreCredit {
46    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47        write!(f, "{}: {}", self.key, self.value.join(", "))
48    }
49}
50
51#[derive(Clone)]
52#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
53pub struct ScoreEncoding {
54    pub software: Option<String>,
55    pub encoding_date: Option<String>,
56    pub encoder: Option<String>,
57}
58
59/// A struct to hold identification information for a score.
60#[derive(Clone)]
61#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
62pub struct ScoreIdentification {
63    pub creator: Vec<String>,
64    pub encoding: ScoreEncoding,
65}
66
67#[derive(Clone, Default)]
68#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
69pub struct Score {
70    pub title: Option<String>,
71    pub movement_number: Option<String>,
72    pub movement_title: Option<String>,
73    pub ident: Option<ScoreIdentification>,
74    pub credits: Vec<ScoreCredit>,
75    pub parts: Vec<Part>,
76}
77
78#[cfg(test)]
79mod tests {
80    #[test]
81    fn test_score_credit_display() {
82        use super::*;
83        let credit = ScoreCredit {
84            key: ScoreCreditKey::Composer,
85            value: vec!["Johann Sebastian Bach".to_string()],
86        };
87        assert_eq!(credit.to_string(), "Composer: Johann Sebastian Bach");
88    }
89}