musicxml/datatypes/
integer.rs

1use alloc::string::{String, ToString};
2use core::ops::Deref;
3use musicxml_internal::{DatatypeDeserializer, DatatypeSerializer};
4use musicxml_macros::{DatatypeDeserialize, DatatypeSerialize};
5
6/// See the definition in the [W3C XML Schema standard](https://www.w3.org/TR/xmlschema-2/#integer).
7///
8/// The value of an instance of this type may be accessed by dereferencing the struct: `*datatype_val`.
9#[derive(Debug, PartialEq, Eq, DatatypeDeserialize, DatatypeSerialize)]
10pub struct Integer(pub i32);
11
12impl Deref for Integer {
13  type Target = i32;
14  fn deref(&self) -> &Self::Target {
15    &self.0
16  }
17}
18
19#[cfg(test)]
20mod integer_tests {
21  use super::*;
22
23  #[test]
24  fn deserialize_valid1() {
25    let result = Integer::deserialize("2342");
26    assert!(result.is_ok());
27    assert_eq!(result.unwrap(), Integer(2342));
28  }
29
30  #[test]
31  fn deserialize_valid2() {
32    let result = Integer::deserialize("-234234");
33    assert!(result.is_ok());
34    assert_eq!(result.unwrap(), Integer(-234_234));
35  }
36
37  #[test]
38  fn deserialize_invalid1() {
39    let result = Integer::deserialize("0.234");
40    assert!(result.is_err());
41  }
42
43  #[test]
44  fn deserialize_invalid2() {
45    let result = Integer::deserialize("1.23432");
46    assert!(result.is_err());
47  }
48
49  #[test]
50  fn deserialize_invalid3() {
51    let result = Integer::deserialize("dsd");
52    assert!(result.is_err());
53  }
54}