use crate::Url;
#[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 ConsultationUrl(Url);
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use super::ConsultationUrl;
#[test]
fn from_str() {
assert_eq!(
ConsultationUrl(
"https://oparl.example.org/consultation/47594"
.parse()
.expect("value must be a parseable url")
),
"https://oparl.example.org/consultation/47594"
.parse()
.expect("value must be a parseable id")
);
}
}
#[cfg(test)]
mod serde_tests {
use pretty_assertions::assert_eq;
use serde_json::json;
use super::ConsultationUrl;
#[test]
fn serialize() {
assert_eq!(
json!(ConsultationUrl(
"https://oparl.example.org/consultation/47594"
.parse()
.expect("value must be a parseable url")
)),
json!("https://oparl.example.org/consultation/47594")
);
}
#[test]
fn deserialize_good() {
let deserialized: ConsultationUrl =
serde_json::from_value(json!("https://oparl.example.org/consultation/47594"))
.expect("value must be deserializable as ConsultationId");
assert_eq!(
deserialized,
ConsultationUrl(
"https://oparl.example.org/consultation/47594"
.parse()
.expect("value must be a parseable url")
)
);
}
#[test]
fn deserialize_bad() {
assert!(serde_json::from_value::<ConsultationUrl>(json!("xyzabcd")).is_err());
assert!(serde_json::from_value::<ConsultationUrl>(json!(true)).is_err());
assert!(serde_json::from_value::<ConsultationUrl>(json!(123)).is_err());
}
}