oparl_types/
body_classification.rs1#[derive(
7 Debug,
8 Clone,
9 PartialEq,
10 Eq,
11 PartialOrd,
12 Ord,
13 Hash,
14 derive_more::AsRef,
15 derive_more::Display,
16 derive_more::From,
17 derive_more::FromStr,
18 derive_more::Into,
19 serde::Serialize,
20 serde::Deserialize,
21)]
22#[cfg_attr(feature = "sea-orm", derive(sea_orm::DeriveValueType))]
23pub struct BodyClassification(String);
24
25impl From<&str> for BodyClassification {
26 fn from(value: &str) -> Self {
27 Self(value.into())
28 }
29}
30
31impl BodyClassification {
32 pub fn as_str(&self) -> &str {
33 self.0.as_str()
34 }
35}
36
37#[cfg(test)]
38mod tests {
39 use super::BodyClassification;
40
41 use pretty_assertions::assert_eq;
42
43 #[test]
44 fn from_str() {
45 assert_eq!(
46 BodyClassification("Beispiel-System".to_string()),
47 "Beispiel-System"
48 .parse()
49 .expect("value must be a parseable name")
50 );
51 }
52}
53
54#[cfg(test)]
55mod serde_tests {
56 use super::BodyClassification;
57 use pretty_assertions::assert_eq;
58 use serde_json::json;
59
60 #[test]
61 fn serialize() {
62 assert_eq!(
63 json!(BodyClassification("Kreisfreie Stadt".to_string())),
64 json!("Kreisfreie Stadt")
65 );
66 }
67
68 #[test]
69 fn deserialize_good() {
70 let deserialized: BodyClassification = serde_json::from_value(json!("Kreisfreie Stadt"))
71 .expect("value must be deserializable as BodyClassification");
72 assert_eq!(deserialized, BodyClassification::from("Kreisfreie Stadt"));
73 }
74
75 #[test]
76 fn deserialize_bad() {
77 assert!(serde_json::from_value::<BodyClassification>(json!([])).is_err());
78 assert!(serde_json::from_value::<BodyClassification>(json!({})).is_err());
79 assert!(serde_json::from_value::<BodyClassification>(json!(true)).is_err());
80 assert!(serde_json::from_value::<BodyClassification>(json!(123)).is_err());
81 }
82}