use crate::either::Either; pub use activitystreams::primitives::{ Length, MimeMediaType, RdfLangString, XsdAnyUri, XsdDateTime, XsdDuration, XsdFloat, XsdNonNegativeInteger, XsdString, }; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[serde(transparent)] pub struct AnyString(Either); #[derive( Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize, )] #[serde(transparent)] pub struct Unit(Either); #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[serde(transparent)] pub struct OneOrMany(pub Either>); impl AnyString { pub fn as_xsd_string(&self) -> Option<&XsdString> { self.0.as_ref().left() } pub fn as_rdf_lang_string(&self) -> Option<&RdfLangString> { self.0.as_ref().right() } pub fn xsd_string(self) -> Option { self.0.left() } pub fn rdf_lang_string(self) -> Option { self.0.right() } pub fn from_xsd_string(string: T) -> Self where T: Into, { AnyString(Either::Left(string.into())) } pub fn from_rdf_lang_string(string: T) -> Self where T: Into, { AnyString(Either::Right(string.into())) } pub fn set_xsd_string(&mut self, string: T) where T: Into, { self.0 = Either::Left(string.into()); } pub fn set_rdf_lang_string(&mut self, string: T) where T: Into, { self.0 = Either::Right(string.into()); } } impl OneOrMany { pub fn as_single_xsd_string(&self) -> Option<&XsdString> { self.as_one() .and_then(|any_string| any_string.as_xsd_string()) } pub fn as_single_rdf_lang_string(&self) -> Option<&RdfLangString> { self.as_one() .and_then(|any_string| any_string.as_rdf_lang_string()) } pub fn single_xsd_string(self) -> Option { self.one().and_then(|any_string| any_string.xsd_string()) } pub fn single_rdf_lang_string(self) -> Option { self.one() .and_then(|any_string| any_string.rdf_lang_string()) } pub fn from_xsd_string(string: T) -> Self where T: Into, { Self::from_one(AnyString::from_xsd_string(string)) } pub fn from_rdf_lang_string(string: T) -> Self where T: Into, { Self::from_one(AnyString::from_rdf_lang_string(string)) } pub fn add_xsd_string(&mut self, string: T) -> &mut Self where T: Into, { self.add(string.into()) } pub fn add_rdf_lang_string(&mut self, string: T) -> &mut Self where T: Into, { self.add(string.into()) } } impl OneOrMany { pub fn as_one(&self) -> Option<&T> { self.0.as_ref().left() } pub fn one(self) -> Option { self.0.left() } pub fn as_many(&self) -> Option<&[T]> { self.0.as_ref().right().map(|v| v.as_ref()) } pub fn many(self) -> Option> { self.0.right() } pub fn unwrap_to_vec(self) -> Vec { match self.0 { Either::Left(t) => vec![t], Either::Right(v) => v, } } pub fn from_one(t: T) -> Self { OneOrMany(Either::Left(t)) } pub fn from_many(items: Vec) -> Self { OneOrMany(Either::Right(items)) } pub fn set_one(&mut self, u: U) -> &mut Self where U: Into, { self.0 = Either::Left(u.into()); self } pub fn set_many(&mut self, items: impl IntoIterator) -> &mut Self where U: Into, { self.0 = Either::Right(items.into_iter().map(Into::into).collect()); self } pub fn add(&mut self, u: U) -> &mut Self where U: Into, { let mut v = match std::mem::replace(&mut self.0, Either::Right(vec![])) { Either::Left(one) => vec![one], Either::Right(v) => v, }; v.push(u.into()); self.0 = Either::Right(v); self } pub fn add_many(&mut self, items: impl IntoIterator) -> &mut Self where U: Into, { let mut v = match std::mem::replace(&mut self.0, Either::Right(vec![])) { Either::Left(one) => vec![one], Either::Right(v) => v, }; v.extend(items.into_iter().map(Into::into)); self.0 = Either::Right(v); self } } impl Unit { /// Create a new unit measuring Centimeters /// /// ```rust /// use activitystreams_new::primitives::Unit; /// /// Unit::centimeters(); /// ``` pub fn centimeters() -> Self { Unit(Either::Left(Length::Centimeters)) } /// Check if the unit is Centimeters /// /// ```rust /// use activitystreams_new::primitives::Unit; /// /// assert!(Unit::centimeters().is_centimeters()); /// ``` pub fn is_centimeters(&self) -> bool { self.0 .as_ref() .left() .map(|l| l.is_centimeters()) .unwrap_or(false) } /// Create a new unit measuring Meters /// /// ```rust /// use activitystreams_new::primitives::Unit; /// /// Unit::meters(); /// ``` pub fn meters() -> Self { Unit(Either::Left(Length::Meters)) } /// Check if the unit is Meters /// /// ```rust /// use activitystreams_new::primitives::Unit; /// /// assert!(Unit::meters().is_meters()); /// ``` pub fn is_meters(&self) -> bool { self.0 .as_ref() .left() .map(|l| l.is_meters()) .unwrap_or(false) } /// Create a new unit measuring Kilometers /// /// ```rust /// use activitystreams_new::primitives::Unit; /// /// Unit::kilometers(); /// ``` pub fn kilometers() -> Self { Unit(Either::Left(Length::Kilometers)) } /// Check if the unit is Kilometers /// /// ```rust /// use activitystreams_new::primitives::Unit; /// /// assert!(Unit::kilometers().is_kilometers()); /// ``` pub fn is_kilometers(&self) -> bool { self.0 .as_ref() .left() .map(|l| l.is_kilometers()) .unwrap_or(false) } /// Create a new unit measuring Feet /// /// ```rust /// use activitystreams_new::primitives::Unit; /// /// Unit::feet(); /// ``` pub fn feet() -> Self { Unit(Either::Left(Length::Feet)) } /// Check if the unit is Feet /// /// ```rust /// use activitystreams_new::primitives::Unit; /// /// assert!(Unit::feet().is_feet()); /// ``` pub fn is_feet(&self) -> bool { self.0.as_ref().left().map(|l| l.is_feet()).unwrap_or(false) } /// Create a new unit measuring Inches /// /// ```rust /// use activitystreams_new::primitives::Unit; /// /// Unit::inches(); /// ``` pub fn inches() -> Self { Unit(Either::Left(Length::Inches)) } /// Check if the unit is Inches /// /// ```rust /// use activitystreams_new::primitives::Unit; /// /// assert!(Unit::inches().is_inches()); /// ``` pub fn is_inches(&self) -> bool { self.0 .as_ref() .left() .map(|l| l.is_inches()) .unwrap_or(false) } /// Create a new custom unit /// /// ```rust /// use activitystreams_new::primitives::Unit; /// /// Unit::custom("Yards"); /// ``` pub fn custom(string: T) -> Self where T: Into, { Unit(Either::Right(string.into())) } /// Check if a unit is custom /// /// ```rust /// use activitystreams_new::primitives::Unit; /// /// assert!(Unit::custom("Yards").is_custom()); /// ``` pub fn is_custom(&self) -> bool { self.as_custom().is_some() } /// Fetch a custom unit /// /// ```rust /// use activitystreams_new::primitives::{Unit, XsdString}; /// /// assert!(Unit::custom("Yards").as_custom() == Some(&XsdString::from("Yards"))); /// ``` pub fn as_custom(&self) -> Option<&XsdString> { self.0.as_ref().right() } } impl Default for Unit { fn default() -> Self { Self::meters() } } impl std::str::FromStr for Unit { type Err = std::convert::Infallible; fn from_str(s: &str) -> Result { let unit = match s { "cm" => Self::centimeters(), "m" => Self::meters(), "km" => Self::kilometers(), "inches" => Self::inches(), "feet" => Self::feet(), other => Self::custom(other), }; Ok(unit) } } impl From for Unit { fn from(s: String) -> Self { match s.parse() { Ok(u) => u, Err(e) => match e {}, } } } impl From<&str> for Unit { fn from(s: &str) -> Self { match s.parse() { Ok(u) => u, Err(e) => match e {}, } } } impl From for OneOrMany { fn from(t: T) -> Self { OneOrMany::from_one(t) } } impl From> for OneOrMany { fn from(t: Vec) -> Self { OneOrMany::from_many(t) } } impl From for AnyString { fn from(s: XsdString) -> Self { AnyString::from_xsd_string(s) } } impl From for AnyString { fn from(s: RdfLangString) -> Self { AnyString::from_rdf_lang_string(s) } } impl From for OneOrMany { fn from(s: XsdString) -> Self { OneOrMany::::from_xsd_string(s) } } impl From for OneOrMany { fn from(s: RdfLangString) -> Self { OneOrMany::::from_rdf_lang_string(s) } }