musicxml/datatypes/
divisions.rs

1use alloc::string::{String, ToString};
2use core::ops::Deref;
3use musicxml_internal::{DatatypeDeserializer, DatatypeSerializer};
4use musicxml_macros::{DatatypeDeserialize, DatatypeSerialize};
5
6/// Used to express values in terms of the musical divisions defined by the divisions element.
7///
8/// It is preferred that these be integer values both for MIDI interoperability and to avoid roundoff errors.
9///
10/// The value of an instance of this type may be accessed by dereferencing the struct: `*datatype_val`.
11#[derive(Debug, PartialEq, Eq, DatatypeDeserialize, DatatypeSerialize)]
12pub struct Divisions(pub i32);
13
14impl Deref for Divisions {
15  type Target = i32;
16  fn deref(&self) -> &Self::Target {
17    &self.0
18  }
19}
20
21#[cfg(test)]
22mod divisions_tests {
23  use super::*;
24
25  #[test]
26  fn deserialize_valid1() {
27    let result = Divisions::deserialize("1");
28    assert!(result.is_ok());
29    assert_eq!(result.unwrap(), Divisions(1));
30  }
31
32  #[test]
33  fn deserialize_valid2() {
34    let result = Divisions::deserialize("32343");
35    assert!(result.is_ok());
36    assert_eq!(result.unwrap(), Divisions(32343));
37  }
38
39  #[test]
40  fn deserialize_valid3() {
41    let result = Divisions::deserialize("0");
42    assert!(result.is_ok());
43    assert_eq!(result.unwrap(), Divisions(0));
44  }
45
46  #[test]
47  fn deserialize_valid4() {
48    let result = Divisions::deserialize("223234");
49    assert!(result.is_ok());
50    assert_eq!(result.unwrap(), Divisions(223_234));
51  }
52
53  #[test]
54  fn deserialize_invalid1() {
55    let result = Divisions::deserialize("a");
56    assert!(result.is_err());
57  }
58
59  #[test]
60  fn deserialize_invalid2() {
61    let result = Divisions::deserialize("2d");
62    assert!(result.is_err());
63  }
64}