diff --git a/Cargo.toml b/Cargo.toml index 299ca5c..b3a30ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/activitystreams-types/Cargo.toml b/activitystreams-types/Cargo.toml index 998bd5d..3c7303e 100644 --- a/activitystreams-types/Cargo.toml +++ b/activitystreams-types/Cargo.toml @@ -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" diff --git a/activitystreams-types/src/primitives/xsd_any_uri.rs b/activitystreams-types/src/primitives/xsd_any_uri.rs index 7752044..72a1e2d 100644 --- a/activitystreams-types/src/primitives/xsd_any_uri.rs +++ b/activitystreams-types/src/primitives/xsd_any_uri.rs @@ -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 for XsdAnyUri { type Error = XsdAnyUriError; fn try_from(s: String) -> Result { - 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 { - 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 { - 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 { - 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(&self, serializer: S) -> Result + where + S: serde::ser::Serializer, + { + serializer.serialize_str(&self.0.to_string()) + } +} + +impl<'de> serde::de::Deserialize<'de> for XsdAnyUri { + fn deserialize(deserializer: D) -> Result + where + D: serde::de::Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + s.parse().map_err(serde::de::Error::custom) + } +} diff --git a/examples/basic.rs b/examples/basic.rs new file mode 100644 index 0000000..790936a --- /dev/null +++ b/examples/basic.rs @@ -0,0 +1,29 @@ +use activitystreams::{ + object::Video, + primitives::{MimeMediaType, XsdAnyUri, XsdDuration}, +}; + +fn main() -> Result<(), Box> { + let mut v = Video::default(); + + v.as_mut() + .set_context_xsd_any_uri("https://www.w3.org/ns/activitystreams".parse::()?) + .set_id("https://example.com/@example/lions".parse::()?) + .set_url_xsd_any_uri("https://example.com/@example/lions/video.webm".parse::()?) + .set_name_xsd_string("My Cool Video") + .set_summary_xsd_string("A video about some cool lions") + .set_media_type("video/webm".parse::()?) + .set_duration("PT4M20S".parse::()?); + + 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(()) +} diff --git a/src/lib.rs b/src/lib.rs index b7682ca..6fbea61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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};