musicxml/datatypes/
measure_text.rs

1use alloc::string::{String, ToString};
2use core::ops::Deref;
3use musicxml_internal::{DatatypeDeserializer, DatatypeSerializer};
4use musicxml_macros::DatatypeSerialize;
5
6/// Used for the `text` attribute of [Measure][crate::elements::Measure] elements.
7///
8/// It must have at least one character. The `implicit` attribute of the [Measure][crate::elements::Measure]
9/// element should be set to "yes" rather than setting the `text` attribute to an empty string.
10///
11/// The value of an instance of this type may be accessed by dereferencing the struct: `*datatype_val`.
12///
13/// **Minimum Length**: 1
14#[derive(Debug, PartialEq, Eq, DatatypeSerialize)]
15pub struct MeasureText(pub String);
16
17impl Deref for MeasureText {
18  type Target = String;
19  fn deref(&self) -> &Self::Target {
20    &self.0
21  }
22}
23
24impl DatatypeDeserializer for MeasureText {
25  fn deserialize(value: &str) -> Result<Self, String> {
26    if value.is_empty() {
27      Err(format!("Value {value} is invalid for the <measure-text> data type"))
28    } else {
29      Ok(MeasureText(String::from(value)))
30    }
31  }
32}
33
34#[cfg(test)]
35mod measure_text_tests {
36  use super::*;
37
38  #[test]
39  fn deserialize_valid1() {
40    let result = MeasureText::deserialize("1234");
41    assert!(result.is_ok());
42    assert_eq!(result.unwrap(), MeasureText(String::from("1234")));
43  }
44
45  #[test]
46  fn deserialize_valid2() {
47    let result = MeasureText::deserialize("test");
48    assert!(result.is_ok());
49    assert_eq!(result.unwrap(), MeasureText(String::from("test")));
50  }
51
52  #[test]
53  fn deserialize_invalid1() {
54    let result = MeasureText::deserialize("");
55    assert!(result.is_err());
56  }
57}