#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
derive_more::AsRef,
derive_more::Display,
derive_more::From,
derive_more::FromStr,
derive_more::Into,
serde::Serialize,
serde::Deserialize,
)]
pub struct BodyClassification(String);
impl From<&str> for BodyClassification {
fn from(value: &str) -> Self {
Self(value.into())
}
}
impl BodyClassification {
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
#[cfg(test)]
mod tests {
use super::BodyClassification;
use pretty_assertions::assert_eq;
#[test]
fn from_str() {
assert_eq!(
BodyClassification("Beispiel-System".to_string()),
"Beispiel-System"
.parse()
.expect("value must be a parseable name")
);
}
}
#[cfg(test)]
mod serde_tests {
use super::BodyClassification;
use pretty_assertions::assert_eq;
use serde_json::json;
#[test]
fn serialize() {
assert_eq!(
json!(BodyClassification("Kreisfreie Stadt".to_string())),
json!("Kreisfreie Stadt")
);
}
#[test]
fn deserialize_good() {
let deserialized: BodyClassification = serde_json::from_value(json!("Kreisfreie Stadt"))
.expect("value must be deserializable as BodyClassification");
assert_eq!(deserialized, BodyClassification::from("Kreisfreie Stadt"));
}
#[test]
fn deserialize_bad() {
assert!(serde_json::from_value::<BodyClassification>(json!([])).is_err());
assert!(serde_json::from_value::<BodyClassification>(json!({})).is_err());
assert!(serde_json::from_value::<BodyClassification>(json!(true)).is_err());
assert!(serde_json::from_value::<BodyClassification>(json!(123)).is_err());
}
}