use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use std::borrow::{Borrow, Cow}; #[derive(Debug, Clone, Eq, PartialEq)] pub struct Acct(String); impl Acct { pub fn new(uri_without_acct: Cow<'_, str>) -> Self { Acct(uri_without_acct.to_string()) } } impl> From for Acct { fn from(value: S) -> Self { let val = value.borrow(); Acct(val.strip_prefix("acct:").unwrap_or(val).to_string()) } } impl AsRef for Acct { fn as_ref(&self) -> &str { &self.0 } } impl Serialize for Acct { fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_str(&format!("acct:{}", self.0)) } } impl<'de> Deserialize<'de> for Acct { fn deserialize>(deserializer: D) -> Result { let acct = String::deserialize(deserializer)?; if let Some(rem) = acct.strip_prefix("acct:") { Ok(Acct(rem.to_owned())) } else { Err(de::Error::custom( "Missing acct protocol for account!".to_owned(), )) } } } #[cfg(test)] mod test { use crate::web_model::acct::Acct; use serde_json::json; #[test] fn should_remove_acct_prefix() { let json = json!("acct:natty@tech.lgbt"); let acct: Acct = serde_json::from_value(json).unwrap(); assert_eq!(acct, Acct::from("natty@tech.lgbt")) } #[test] fn should_add_acct_prefix() { let acct = Acct::from("natty@tech.lgbt"); let json = serde_json::to_value(acct).unwrap(); assert_eq!(json, json!("acct:natty@tech.lgbt")); } }