Finish documenting Object
This commit is contained in:
parent
9a3df39757
commit
9095ba4077
3 changed files with 1037 additions and 75 deletions
|
@ -1,4 +1,6 @@
|
|||
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
||||
#[derive(
|
||||
Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize,
|
||||
)]
|
||||
#[serde(untagged)]
|
||||
pub enum Either<L, R> {
|
||||
Left(L),
|
||||
|
|
897
src/object.rs
897
src/object.rs
File diff suppressed because it is too large
Load diff
|
@ -9,7 +9,11 @@ pub use activitystreams::primitives::{
|
|||
#[serde(transparent)]
|
||||
pub struct AnyString(Either<XsdString, RdfLangString>);
|
||||
|
||||
pub type Unit = Either<Length, XsdString>;
|
||||
#[derive(
|
||||
Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize,
|
||||
)]
|
||||
#[serde(transparent)]
|
||||
pub struct Unit(Either<Length, XsdString>);
|
||||
|
||||
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
||||
#[serde(transparent)]
|
||||
|
@ -185,6 +189,211 @@ impl<T> OneOrMany<T> {
|
|||
}
|
||||
}
|
||||
|
||||
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<T>(string: T) -> Self
|
||||
where
|
||||
T: Into<XsdString>,
|
||||
{
|
||||
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<Self, Self::Err> {
|
||||
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<String> 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<T> From<T> for OneOrMany<T> {
|
||||
fn from(t: T) -> Self {
|
||||
OneOrMany::from_one(t)
|
||||
|
|
Loading…
Reference in a new issue