Add Url type backing XsdAnyUri

This commit is contained in:
asonix 2020-03-09 22:54:41 -05:00
parent ac4f1cd5d1
commit 96579dc168
5 changed files with 70 additions and 15 deletions

View file

@ -15,8 +15,7 @@ activitystreams-types = { version = "0.3.0", path = "activitystreams-types" }
[dev-dependencies]
anyhow = "1.0"
serde = "1.0"
serde_derive = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
activitystreams-derive = { version = "0.3", path = "activitystreams-derive" }

View file

@ -18,6 +18,7 @@ mime = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
typetag = "0.1.4"
url = "2.1"
[dev-dependencies]
anyhow = "1.0"

View file

@ -1,16 +1,21 @@
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
#[serde(transparent)]
pub struct XsdAnyUri(String);
#[derive(Clone, Debug)]
pub struct XsdAnyUri(url::Url);
#[derive(Clone, Debug, thiserror::Error)]
#[error("Could not parse XsdAnyUri")]
pub struct XsdAnyUriError;
impl Default for XsdAnyUri {
fn default() -> Self {
"data:text/plain,uwu".parse().unwrap()
}
}
impl std::convert::TryFrom<String> for XsdAnyUri {
type Error = XsdAnyUriError;
fn try_from(s: String) -> Result<Self, Self::Error> {
Ok(XsdAnyUri(s))
s.parse()
}
}
@ -18,7 +23,7 @@ impl std::convert::TryFrom<&str> for XsdAnyUri {
type Error = XsdAnyUriError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Ok(XsdAnyUri(s.to_owned()))
s.parse()
}
}
@ -26,7 +31,7 @@ impl std::convert::TryFrom<&mut str> for XsdAnyUri {
type Error = XsdAnyUriError;
fn try_from(s: &mut str) -> Result<Self, Self::Error> {
Ok(XsdAnyUri(s.to_owned()))
s.parse()
}
}
@ -34,7 +39,7 @@ impl std::str::FromStr for XsdAnyUri {
type Err = XsdAnyUriError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(XsdAnyUri(s.to_owned()))
Ok(XsdAnyUri(s.parse().map_err(|_| XsdAnyUriError)?))
}
}
@ -43,3 +48,22 @@ impl std::fmt::Display for XsdAnyUri {
std::fmt::Display::fmt(&self.0, f)
}
}
impl serde::ser::Serialize for XsdAnyUri {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
serializer.serialize_str(&self.0.to_string())
}
}
impl<'de> serde::de::Deserialize<'de> for XsdAnyUri {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
s.parse().map_err(serde::de::Error::custom)
}
}

29
examples/basic.rs Normal file
View file

@ -0,0 +1,29 @@
use activitystreams::{
object::Video,
primitives::{MimeMediaType, XsdAnyUri, XsdDuration},
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut v = Video::default();
v.as_mut()
.set_context_xsd_any_uri("https://www.w3.org/ns/activitystreams".parse::<XsdAnyUri>()?)
.set_id("https://example.com/@example/lions".parse::<XsdAnyUri>()?)
.set_url_xsd_any_uri("https://example.com/@example/lions/video.webm".parse::<XsdAnyUri>()?)
.set_name_xsd_string("My Cool Video")
.set_summary_xsd_string("A video about some cool lions")
.set_media_type("video/webm".parse::<MimeMediaType>()?)
.set_duration("PT4M20S".parse::<XsdDuration>()?);
println!("Video, {:#?}", v);
let s = serde_json::to_string(&v)?;
println!("json, {}", s);
let v: Video = serde_json::from_str(&s)?;
println!("Video again, {:#?}", v);
Ok(())
}

View file

@ -128,9 +128,11 @@ pub mod collection;
pub mod link;
pub mod object;
pub use self::activity::{Activity, ActivityExt, IntransitiveActivity};
pub use self::actor::Actor;
pub use self::collection::{Collection, CollectionExt, CollectionPage, CollectionPageExt};
pub use self::link::{Link, LinkExt};
pub use self::object::{Object, ObjectExt};
pub use activitystreams_types::context;
pub use self::{
activity::{Activity, ActivityExt, IntransitiveActivity},
actor::Actor,
collection::{Collection, CollectionExt, CollectionPage, CollectionPageExt},
link::{Link, LinkExt},
object::{Object, ObjectExt},
};
pub use activitystreams_types::{context, primitives};