1use url::Url;
7
8#[derive(
9 Debug,
10 Clone,
11 PartialEq,
12 Eq,
13 PartialOrd,
14 Ord,
15 Hash,
16 derive_more::AsRef,
17 derive_more::Display,
18 derive_more::From,
19 derive_more::FromStr,
20 derive_more::Into,
21 serde::Serialize,
22 serde::Deserialize,
23)]
24pub struct BodyId(Url);
25
26#[cfg(test)]
27mod tests {
28 use super::BodyId;
29
30 use pretty_assertions::assert_eq;
31
32 #[test]
33 fn from_str() {
34 assert_eq!(
35 BodyId(
36 "https://oparl.example.org/body/0"
37 .parse()
38 .expect("value must be a parseable url")
39 ),
40 "https://oparl.example.org/body/0"
41 .parse()
42 .expect("value must be a parseable id")
43 );
44 }
45}
46
47#[cfg(test)]
48mod serde_tests {
49 use super::BodyId;
50 use pretty_assertions::assert_eq;
51 use serde_json::json;
52
53 #[test]
54 fn serialize() {
55 assert_eq!(
56 json!(BodyId(
57 "https://oparl.example.org/body/0"
58 .parse()
59 .expect("value must be a parseable url")
60 )),
61 json!("https://oparl.example.org/body/0")
62 );
63 }
64
65 #[test]
66 fn deserialize_good() {
67 let deserialized: BodyId =
68 serde_json::from_value(json!("https://oparl.example.org/body/0"))
69 .expect("value must be deserializable as BodyId");
70 assert_eq!(
71 deserialized,
72 BodyId(
73 "https://oparl.example.org/body/0"
74 .parse()
75 .expect("value must be a parseable url")
76 )
77 );
78 }
79
80 #[test]
81 fn deserialize_bad() {
82 assert!(serde_json::from_value::<BodyId>(json!("xyzabcd")).is_err());
83 assert!(serde_json::from_value::<BodyId>(json!(true)).is_err());
84 assert!(serde_json::from_value::<BodyId>(json!(123)).is_err());
85 }
86}