Add more impls for primitives

This commit is contained in:
asonix 2020-03-15 12:17:02 -05:00
parent 08b291f934
commit b71c31417b
10 changed files with 120 additions and 10 deletions

View file

@ -1,7 +1,7 @@
[package] [package]
name = "activitystreams" name = "activitystreams"
description = "Activity Streams in Rust" description = "Activity Streams in Rust"
version = "0.5.0-alpha.5" version = "0.5.0-alpha.6"
license = "GPL-3.0" license = "GPL-3.0"
authors = ["asonix <asonix@asonix.dog>"] authors = ["asonix <asonix@asonix.dog>"]
repository = "https://git.asonix.dog/Aardwolf/activitystreams" repository = "https://git.asonix.dog/Aardwolf/activitystreams"

View file

@ -9,7 +9,7 @@ __A set of Traits and Types that make up the ActivityStreams and ActivityPub spe
First, add ActivityStreams to your dependencies First, add ActivityStreams to your dependencies
```toml ```toml
activitystreams = "0.5.0-alpha.5" activitystreams = "0.5.0-alpha.6"
``` ```
### Types ### Types
@ -177,7 +177,7 @@ There are a number of features that can be disabled in this crate. By default, e
enabled. enabled.
```toml ```toml
activitystreams = { version = "0.5.0-alpha.5", default-features = "false", features = ["derive"] } activitystreams = { version = "0.5.0-alpha.6", default-features = "false", features = ["derive"] }
``` ```
| feature | what you get | | feature | what you get |

View file

@ -25,7 +25,7 @@
//! //!
//! First, add ActivityStreams to your dependencies //! First, add ActivityStreams to your dependencies
//! ```toml //! ```toml
//! activitystreams = "0.5.0-alpha.5" //! activitystreams = "0.5.0-alpha.6"
//! ``` //! ```
//! //!
//! ### Types //! ### Types
@ -193,7 +193,7 @@
//! enabled. //! enabled.
//! //!
//! ```toml //! ```toml
//! activitystreams = { version = "0.5.0-alpha.5", default-features = "false", features = ["derive"] } //! activitystreams = { version = "0.5.0-alpha.6", default-features = "false", features = ["derive"] }
//! ``` //! ```
//! //!
//! | feature | what you get | //! | feature | what you get |

View file

@ -58,6 +58,23 @@ pub struct XsdAnyUri(url::Url);
#[error("Could not parse XsdAnyUri")] #[error("Could not parse XsdAnyUri")]
pub struct XsdAnyUriError; pub struct XsdAnyUriError;
impl XsdAnyUri {
/// Borrow the underlying string from the XsdAnyUri
pub fn as_str(&self) -> &str {
self.as_ref()
}
/// Borrow a `url::Url` from the XsdAnyUri
pub fn as_url(&self) -> &url::Url {
self.as_ref()
}
/// Mutably borrow a `url::Url` from the XsdAnyUri
pub fn as_url_mut(&mut self) -> &mut url::Url {
self.as_mut()
}
}
impl From<url::Url> for XsdAnyUri { impl From<url::Url> for XsdAnyUri {
fn from(u: url::Url) -> Self { fn from(u: url::Url) -> Self {
XsdAnyUri(u) XsdAnyUri(u)
@ -82,6 +99,18 @@ impl AsRef<str> for XsdAnyUri {
} }
} }
impl AsRef<url::Url> for XsdAnyUri {
fn as_ref(&self) -> &url::Url {
&self.0
}
}
impl AsMut<url::Url> for XsdAnyUri {
fn as_mut(&mut self) -> &mut url::Url {
&mut self.0
}
}
impl std::convert::TryFrom<String> for XsdAnyUri { impl std::convert::TryFrom<String> for XsdAnyUri {
type Error = XsdAnyUriError; type Error = XsdAnyUriError;

View file

@ -38,6 +38,18 @@ pub struct XsdDateTime(chrono::DateTime<chrono::FixedOffset>);
#[error("Error parsing DateTime")] #[error("Error parsing DateTime")]
pub struct XsdDateTimeError; pub struct XsdDateTimeError;
impl XsdDateTime {
/// Borrow the underlying `chrono::DateTime<chrono::FixedOffset>`
pub fn as_datetime(&self) -> &chrono::DateTime<chrono::FixedOffset> {
self.as_ref()
}
/// Mutably borrow the underlying `chrono::DateTime<chrono::FixedOffset>`
pub fn as_datetime_mut(&mut self) -> &mut chrono::DateTime<chrono::FixedOffset> {
self.as_mut()
}
}
impl From<chrono::DateTime<chrono::FixedOffset>> for XsdDateTime { impl From<chrono::DateTime<chrono::FixedOffset>> for XsdDateTime {
fn from(d: chrono::DateTime<chrono::FixedOffset>) -> Self { fn from(d: chrono::DateTime<chrono::FixedOffset>) -> Self {
XsdDateTime(d) XsdDateTime(d)

View file

@ -49,6 +49,18 @@ pub struct XsdDuration(chrono::Duration);
#[error("Error parsing Duration")] #[error("Error parsing Duration")]
pub struct XsdDurationError; pub struct XsdDurationError;
impl XsdDuration {
/// Borrow the underlying `chrono::Duration`
pub fn as_duration(&self) -> &chrono::Duration {
self.as_ref()
}
/// Mutably borrow the underlying `chrono::Duration`
pub fn as_duration_mut(&mut self) -> &mut chrono::Duration {
self.as_mut()
}
}
impl From<chrono::Duration> for XsdDuration { impl From<chrono::Duration> for XsdDuration {
fn from(d: chrono::Duration) -> Self { fn from(d: chrono::Duration) -> Self {
XsdDuration(d) XsdDuration(d)

View file

@ -39,6 +39,18 @@ pub struct XsdFloat(f64);
#[error("Error parsing Float")] #[error("Error parsing Float")]
pub struct XsdFloatError; pub struct XsdFloatError;
impl XsdFloat {
/// Get an f64 from the XsdFloat
pub fn to_f64(&self) -> f64 {
self.0
}
/// Get an XsdFloat from an f64
pub fn from_f64(f: f64) -> Self {
f.into()
}
}
impl AsRef<f64> for XsdFloat { impl AsRef<f64> for XsdFloat {
fn as_ref(&self) -> &f64 { fn as_ref(&self) -> &f64 {
&self.0 &self.0

View file

@ -41,6 +41,19 @@ pub struct XsdNonNegativeFloat(f64);
#[error("Error parsing NonNegativeFloat")] #[error("Error parsing NonNegativeFloat")]
pub struct XsdNonNegativeFloatError; pub struct XsdNonNegativeFloatError;
impl XsdNonNegativeFloat {
/// Get an f64 from this XsdNonNegativeFloat
pub fn to_float(&self) -> f64 {
self.0
}
/// Try to get an XsdNonNegativeFloat from an f64
pub fn from_float(f: f64) -> Result<Self, XsdNonNegativeFloatError> {
use std::convert::TryInto;
f.try_into()
}
}
impl AsRef<f64> for XsdNonNegativeFloat { impl AsRef<f64> for XsdNonNegativeFloat {
fn as_ref(&self) -> &f64 { fn as_ref(&self) -> &f64 {
&self.0 &self.0

View file

@ -41,6 +41,18 @@ pub struct XsdNonNegativeInteger(u64);
#[error("Error parsing NonNegativeInteger")] #[error("Error parsing NonNegativeInteger")]
pub struct XsdNonNegativeIntegerError; pub struct XsdNonNegativeIntegerError;
impl XsdNonNegativeInteger {
/// Get a u64 from this XsdNonNegativeInteger
pub fn to_u64(&self) -> u64 {
self.0
}
/// Get an XsdNonNegativeInteger from a u64
pub fn from_u64(u: u64) -> Self {
u.into()
}
}
impl AsRef<u64> for XsdNonNegativeInteger { impl AsRef<u64> for XsdNonNegativeInteger {
fn as_ref(&self) -> &u64 { fn as_ref(&self) -> &u64 {
&self.0 &self.0
@ -53,11 +65,9 @@ impl From<XsdNonNegativeInteger> for u64 {
} }
} }
impl std::convert::TryFrom<u64> for XsdNonNegativeInteger { impl From<u64> for XsdNonNegativeInteger {
type Error = XsdNonNegativeIntegerError; fn from(f: u64) -> Self {
XsdNonNegativeInteger(f)
fn try_from(f: u64) -> Result<Self, Self::Error> {
Ok(XsdNonNegativeInteger(f))
} }
} }

View file

@ -44,6 +44,28 @@
#[serde(transparent)] #[serde(transparent)]
pub struct XsdString(String); pub struct XsdString(String);
impl XsdString {
/// Get an XsdString from an &str
pub fn from_str(s: &str) -> Self {
s.into()
}
/// Get an XsdString from a String
pub fn from_string(s: String) -> Self {
s.into()
}
/// Borrow an &str from an XsdString
pub fn as_str(&self) -> &str {
self.as_ref()
}
/// Consume the XsdString and get a String
pub fn to_string(self) -> String {
self.into()
}
}
impl From<String> for XsdString { impl From<String> for XsdString {
fn from(s: String) -> Self { fn from(s: String) -> Self {
XsdString(s) XsdString(s)