From 60716a6d2ad18672e08be7564c5691b77536533c Mon Sep 17 00:00:00 2001 From: asonix Date: Sat, 27 Jun 2020 17:21:35 -0500 Subject: [PATCH] Use Url's native Serde support --- Cargo.toml | 2 +- src/actor.rs | 85 +++++++++++++++++-------------------------- src/base.rs | 44 +++++++++++----------- src/link.rs | 10 ++--- src/object.rs | 34 ++++++++--------- src/primitives/mod.rs | 3 -- src/unparsed.rs | 16 ++++---- 7 files changed, 85 insertions(+), 109 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 36beb72..b186d8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ mime = "0.3" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" thiserror = "1.0" -url = "2.1" +url = { version = "2.1", features = ["serde"] } [dev-dependencies] anyhow = "1.0" diff --git a/src/actor.rs b/src/actor.rs index d35b704..f0c2b64 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -26,7 +26,7 @@ use crate::{ error::DomainError, markers, object::{ApObject, AsApObject, AsObject, Object}, - primitives::{OneOrMany, XsdAnyUri}, + primitives::OneOrMany, unparsed::{Unparsed, UnparsedMut, UnparsedMutExt}, }; use url::Url; @@ -108,7 +108,7 @@ pub trait ApActorExt: AsApActor { where Inner: 'a, { - self.ap_actor_ref().inbox.as_ref() + &self.ap_actor_ref().inbox } /// Fetch a mutable referece to the current actor's inbox @@ -125,7 +125,7 @@ pub trait ApActorExt: AsApActor { where Inner: 'a, { - self.ap_actor_mut().inbox.as_mut() + &mut self.ap_actor_mut().inbox } /// Set the inbox for the current actor @@ -188,7 +188,7 @@ pub trait ApActorExt: AsApActor { where Inner: 'a, { - self.ap_actor_ref().outbox.as_ref().map(|u| u.as_ref()) + self.ap_actor_ref().outbox.as_ref() } /// Mutably fetch the outbox for the current actor @@ -207,7 +207,7 @@ pub trait ApActorExt: AsApActor { where Inner: 'a, { - self.ap_actor_mut().outbox.as_mut().map(|u| u.as_mut()) + self.ap_actor_mut().outbox.as_mut() } /// Set the outbox for the current actor @@ -239,7 +239,7 @@ pub trait ApActorExt: AsApActor { /// } /// ``` fn take_outbox(&mut self) -> Option { - self.ap_actor_mut().outbox.take().map(|u| u.into_inner()) + self.ap_actor_mut().outbox.take() } /// Delete the outbox from the current object @@ -309,7 +309,7 @@ pub trait ApActorExt: AsApActor { where Inner: 'a, { - self.ap_actor_ref().following.as_ref().map(|f| f.as_ref()) + self.ap_actor_ref().following.as_ref() } /// Mutably fetch the following link for the current actor @@ -328,7 +328,7 @@ pub trait ApActorExt: AsApActor { where Inner: 'a, { - self.ap_actor_mut().following.as_mut().map(|f| f.as_mut()) + self.ap_actor_mut().following.as_mut() } /// Set the following link for the current actor @@ -360,7 +360,7 @@ pub trait ApActorExt: AsApActor { /// } /// ``` fn take_following(&mut self) -> Option { - self.ap_actor_mut().following.take().map(|u| u.into_inner()) + self.ap_actor_mut().following.take() } /// Delete the following link from the current object @@ -430,7 +430,7 @@ pub trait ApActorExt: AsApActor { where Inner: 'a, { - self.ap_actor_ref().followers.as_ref().map(|u| u.as_ref()) + self.ap_actor_ref().followers.as_ref() } /// Mutably fetch the followers link for the current actor @@ -449,7 +449,7 @@ pub trait ApActorExt: AsApActor { where Inner: 'a, { - self.ap_actor_mut().followers.as_mut().map(|u| u.as_mut()) + self.ap_actor_mut().followers.as_mut() } /// Set the followers link for the current actor @@ -481,7 +481,7 @@ pub trait ApActorExt: AsApActor { /// } /// ``` fn take_followers(&mut self) -> Option { - self.ap_actor_mut().followers.take().map(|u| u.into_inner()) + self.ap_actor_mut().followers.take() } /// Delete the followers link from the current object @@ -551,7 +551,7 @@ pub trait ApActorExt: AsApActor { where Inner: 'a, { - self.ap_actor_ref().liked.as_ref().map(|u| u.as_ref()) + self.ap_actor_ref().liked.as_ref() } /// Mutably fetch the liked link for the current actor @@ -570,7 +570,7 @@ pub trait ApActorExt: AsApActor { where Inner: 'a, { - self.ap_actor_mut().liked.as_mut().map(|u| u.as_mut()) + self.ap_actor_mut().liked.as_mut() } /// Set the liked link for the current actor @@ -602,7 +602,7 @@ pub trait ApActorExt: AsApActor { /// } /// ``` fn take_liked(&mut self) -> Option { - self.ap_actor_mut().liked.take().map(|u| u.into_inner()) + self.ap_actor_mut().liked.take() } /// Delete the liked link from the current object @@ -684,10 +684,7 @@ pub trait ApActorExt: AsApActor { where Inner: 'a, { - self.ap_actor_ref() - .streams - .as_ref() - .map(|o| o.as_ref().map(|u| u.as_ref())) + self.ap_actor_ref().streams.as_ref().map(|o| o.as_ref()) } /// Mutably fetch the streams links for the current actor @@ -711,10 +708,7 @@ pub trait ApActorExt: AsApActor { where Inner: 'a, { - self.ap_actor_mut() - .streams - .as_mut() - .map(|o| o.as_mut().map(|u| u.as_mut())) + self.ap_actor_mut().streams.as_mut().map(|o| o.as_mut()) } /// Set the streams links for the current actor @@ -730,7 +724,7 @@ pub trait ApActorExt: AsApActor { /// # } /// ``` fn set_streams(&mut self, streams: Url) -> &mut Self { - self.ap_actor_mut().streams = Some(XsdAnyUri::from(streams).into()); + self.ap_actor_mut().streams = Some(streams.into()); self } @@ -749,11 +743,12 @@ pub trait ApActorExt: AsApActor { /// # Ok(()) /// # } /// ``` - fn set_many_streams(&mut self, items: I) -> &mut Self + fn set_many_streams(&mut self, items: I) -> &mut Self where - I: IntoIterator, + I: IntoIterator, + U: Into, { - let v: Vec = items.into_iter().map(|u| u.into()).collect(); + let v: Vec = items.into_iter().map(|u| u.into()).collect(); self.ap_actor_mut().streams = Some(v.into()); self } @@ -775,7 +770,7 @@ pub trait ApActorExt: AsApActor { fn add_streams(&mut self, stream: Url) -> &mut Self { let v = match self.ap_actor_mut().streams.take() { Some(mut v) => { - v.add(XsdAnyUri::from(stream)); + v.add(stream); v } None => vec![stream.into()].into(), @@ -796,10 +791,7 @@ pub trait ApActorExt: AsApActor { /// } /// ``` fn take_streams(&mut self) -> Option> { - self.ap_actor_mut() - .streams - .take() - .map(|o| o.map(|u| u.into_inner())) + self.ap_actor_mut().streams.take() } /// Delete the streams links from the current object @@ -975,10 +967,7 @@ pub trait ApActorExt: AsApActor { where Inner: 'a, { - self.ap_actor_ref() - .endpoints - .as_ref() - .map(|e| e.as_ref().map(|u| u.as_ref())) + self.ap_actor_ref().endpoints.as_ref().map(|e| e.as_ref()) } /// Mutably fetch the endpoints for the current actor @@ -997,10 +986,7 @@ pub trait ApActorExt: AsApActor { where Inner: 'a, { - self.ap_actor_mut() - .endpoints - .as_mut() - .map(|e| e.as_mut().map(|u| u.as_mut())) + self.ap_actor_mut().endpoints.as_mut().map(|e| e.as_mut()) } /// Set the endpoints for the current actor @@ -1035,10 +1021,7 @@ pub trait ApActorExt: AsApActor { /// } /// ``` fn take_endpoints(&mut self) -> Option> { - self.ap_actor_mut() - .endpoints - .take() - .map(|e| e.map(|u| u.into_inner())) + self.ap_actor_mut().endpoints.take() } /// Delete the endpoints from the current actor @@ -1114,42 +1097,42 @@ pub struct ApActor { /// /// - Range: xsd:anyUri /// - Functional: true - inbox: XsdAnyUri, + inbox: Url, /// An ActivityStreams] OrderedCollection comprised of all the messages produced by the actor. /// /// - Range: xsd:anyUri /// - Functional: true #[serde(skip_serializing_if = "Option::is_none")] - outbox: Option, + outbox: Option, /// A link to an [ActivityStreams] collection of the actors that this actor is following. /// /// - Range: xsd:anyUri /// - Functional: true #[serde(skip_serializing_if = "Option::is_none")] - following: Option, + following: Option, /// A link to an [ActivityStreams] collection of the actors that follow this actor. /// /// - Range: xsd:anyUri /// - Functional: true #[serde(skip_serializing_if = "Option::is_none")] - followers: Option, + followers: Option, /// A link to an [ActivityStreams] collection of objects this actor has liked. /// /// - Range: xsd:anyUri /// - Functional: true #[serde(skip_serializing_if = "Option::is_none")] - liked: Option, + liked: Option, /// A list of supplementary Collections which may be of interest. /// /// - Range: xsd:anyUri /// - Functional: false #[serde(skip_serializing_if = "Option::is_none")] - streams: Option>, + streams: Option>, /// A short username which may be used to refer to the actor, with no uniqueness guarantees. /// @@ -1167,7 +1150,7 @@ pub struct ApActor { /// - Range: Endpoint /// - Functional: true #[serde(skip_serializing_if = "Option::is_none")] - endpoints: Option>, + endpoints: Option>, /// base fields and unparsed json ends up here #[serde(flatten)] diff --git a/src/base.rs b/src/base.rs index 2abe3ca..8642169 100644 --- a/src/base.rs +++ b/src/base.rs @@ -31,7 +31,7 @@ use crate::{ either::Either, error::DomainError, markers, - primitives::{AnyString, MimeMediaType, OneOrMany, XsdAnyUri}, + primitives::{AnyString, MimeMediaType, OneOrMany}, unparsed::{Unparsed, UnparsedMut}, }; use mime::Mime; @@ -290,7 +290,7 @@ pub trait BaseExt: AsBase { where Kind: 'a, { - self.base_ref().id.as_ref().map(|i| i.as_ref()) + self.base_ref().id.as_ref() } /// Mutably borrow the ID from the current object @@ -310,7 +310,7 @@ pub trait BaseExt: AsBase { where Kind: 'a, { - self.base_mut().id.as_mut().map(|i| i.as_mut()) + self.base_mut().id.as_mut() } /// Check if the provided id is equal to the object's id @@ -362,7 +362,7 @@ pub trait BaseExt: AsBase { /// } /// ``` fn take_id(&mut self) -> Option { - self.base_mut().id.take().map(|u| u.into_inner()) + self.base_mut().id.take() } /// Delete the id from the current object @@ -812,7 +812,7 @@ pub trait BaseExt: AsBase { #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[serde(transparent)] -struct IdOrBase(Either>>); +struct IdOrBase(Either>>); /// A type that can represent Any ActivityStreams type /// @@ -859,7 +859,7 @@ pub struct Base { /// When processing Activity Streams 1.0 documents and converting those to 2.0, implementations /// ought to treat id as an alias for the JSON-LD @id key word[.] #[serde(skip_serializing_if = "Option::is_none")] - id: Option, + id: Option, /// The `type` field /// @@ -1089,7 +1089,7 @@ impl AnyBase { Ok(Base::retract(extended)?.into_generic()?.into()) } - /// Check if this object is an XsdAnyUri + /// Check if this object is a Url /// /// ```rust /// # use activitystreams_new::{base::AnyBase, uri}; @@ -1168,10 +1168,8 @@ impl AnyBase { /// # } /// ``` pub fn id(&self) -> Option<&Url> { - self.as_xsd_any_uri().or_else(|| { - self.as_base() - .and_then(|base| base.id.as_ref().map(|i| i.as_ref())) - }) + self.as_xsd_any_uri() + .or_else(|| self.as_base().and_then(|base| base.id.as_ref())) } /// Check if the current object's id matches the provided id @@ -1269,7 +1267,7 @@ impl AnyBase { self.kind_str() == Some(kind) } - /// Get the object as an XsdAnyUri + /// Get the object as a Url /// /// ```rust /// # fn main() -> Result<(), anyhow::Error> { @@ -1317,7 +1315,7 @@ impl AnyBase { self.0.as_ref().left().and_then(|l| l.as_base()) } - /// Take the XsdAnyUri from the Object + /// Take the Url from the Object /// /// ```rust /// # fn main() -> Result<(), anyhow::Error> { @@ -1365,7 +1363,7 @@ impl AnyBase { self.0.left().and_then(|l| l.base()) } - /// Replace the object with the provided XsdAnyUri + /// Replace the object with the provided Url /// /// ```rust /// # fn main() -> Result<(), anyhow::Error> { @@ -1426,7 +1424,7 @@ impl AnyBase { self.0 = Either::Left(IdOrBase::from_base(base)); } - /// Create an AnyBase from an XsdAnyUri + /// Create an AnyBase from a Url /// /// ```rust /// # fn main() -> Result<(), anyhow::Error> { @@ -1469,7 +1467,7 @@ impl AnyBase { impl IdOrBase { fn as_xsd_any_uri(&self) -> Option<&Url> { - self.0.as_ref().left().map(|u| u.as_ref()) + self.0.as_ref().left() } fn as_base(&self) -> Option<&Base> { @@ -1477,7 +1475,7 @@ impl IdOrBase { } fn id(self) -> Option { - self.0.left().map(|u| u.into_inner()) + self.0.left() } fn base(self) -> Option> { @@ -1485,7 +1483,7 @@ impl IdOrBase { } fn from_xsd_any_uri(id: Url) -> Self { - IdOrBase(Either::Left(id.into())) + IdOrBase(Either::Left(id)) } fn from_base(base: Base) -> Self { @@ -1584,7 +1582,7 @@ impl OneOrMany { self.as_single_kind_str() == Some(kind) } - /// Get a single XsdAnyUri from the object, if that is what is contained + /// Get a single Url from the object, if that is what is contained /// /// ```rust /// # fn main() -> Result<(), anyhow::Error> { @@ -1628,7 +1626,7 @@ impl OneOrMany { self.as_one().and_then(|inner| inner.as_base()) } - /// Take a single XsdAnyUri from the object, if that is what is contained + /// Take a single Url from the object, if that is what is contained /// /// ```rust /// # fn main() -> Result<(), anyhow::Error> { @@ -1672,7 +1670,7 @@ impl OneOrMany { self.one().and_then(|inner| inner.take_base()) } - /// Create a `OneOrMany` from an XsdAnyUri + /// Create a `OneOrMany` from a Url /// /// ```rust /// # fn main() -> Result<(), anyhow::Error> { @@ -1711,7 +1709,7 @@ impl OneOrMany { OneOrMany(Either::Left(AnyBase::from_base(base))) } - /// Overwrite the current object with an XsdAnyUri + /// Overwrite the current object with a Url /// /// ```rust /// # use activitystreams_new::{base::Base, context, primitives::OneOrMany}; @@ -1762,7 +1760,7 @@ impl OneOrMany { self } - /// Append an XsdAnyUri to the current object + /// Append a Url to the current object /// /// ```rust /// use activitystreams_new::{base::AnyBase, context, primitives::OneOrMany, security}; diff --git a/src/link.rs b/src/link.rs index b8700a6..33e3f41 100644 --- a/src/link.rs +++ b/src/link.rs @@ -23,7 +23,7 @@ use crate::{ base::{AsBase, Base, Extends}, markers, - primitives::{OneOrMany, XsdAnyUri}, + primitives::OneOrMany, unparsed::{Unparsed, UnparsedMut, UnparsedMutExt}, }; use std::convert::TryFrom; @@ -73,7 +73,7 @@ pub trait LinkExt: AsLink { where Kind: 'a, { - self.link_ref().href.as_ref().map(|u| u.as_ref()) + self.link_ref().href.as_ref() } /// Set the href for the current object @@ -92,7 +92,7 @@ pub trait LinkExt: AsLink { /// # } /// ``` fn set_href(&mut self, href: Url) -> &mut Self { - self.link_mut().href = Some(href.into()); + self.link_mut().href = Some(href); self } @@ -109,7 +109,7 @@ pub trait LinkExt: AsLink { /// } /// ``` fn take_href(&mut self) -> Option { - self.link_mut().href.take().map(|u| u.into_inner()) + self.link_mut().href.take() } /// Delete the href from the current object @@ -502,7 +502,7 @@ pub struct Link { /// - Range: xsd:anyUri /// - Functional: true #[serde(skip_serializing_if = "Option::is_none")] - href: Option, + href: Option, /// Hints as to the language used by the target resource. /// diff --git a/src/object.rs b/src/object.rs index eb1924b..47031d3 100644 --- a/src/object.rs +++ b/src/object.rs @@ -22,7 +22,7 @@ use crate::{ base::{AnyBase, AsBase, Base, Extends}, markers, - primitives::{AnyString, OneOrMany, Unit, XsdAnyUri, XsdDateTime, XsdDuration}, + primitives::{AnyString, OneOrMany, Unit, XsdDateTime, XsdDuration}, unparsed::{Unparsed, UnparsedMut, UnparsedMutExt}, }; use chrono::{DateTime, Duration, FixedOffset}; @@ -2821,7 +2821,7 @@ pub trait ApObjectExt: AsApObject { where Inner: 'a, { - self.ap_object_ref().shares.as_ref().map(|u| u.as_ref()) + self.ap_object_ref().shares.as_ref() } /// Set the shares for the current object @@ -2857,7 +2857,7 @@ pub trait ApObjectExt: AsApObject { /// } /// ``` fn take_shares(&mut self) -> Option { - self.ap_object_mut().shares.take().map(|u| u.into_inner()) + self.ap_object_mut().shares.take() } /// Delete the shares from the current object @@ -2894,7 +2894,7 @@ pub trait ApObjectExt: AsApObject { where Inner: 'a, { - self.ap_object_ref().likes.as_ref().map(|u| u.as_ref()) + self.ap_object_ref().likes.as_ref() } /// Set the likes for the current object @@ -2930,7 +2930,7 @@ pub trait ApObjectExt: AsApObject { /// } /// ``` fn take_likes(&mut self) -> Option { - self.ap_object_mut().likes.take().map(|u| u.into_inner()) + self.ap_object_mut().likes.take() } /// Delete the likes from the current object @@ -3046,7 +3046,7 @@ pub trait ApObjectExt: AsApObject { self.ap_object_ref() .upload_media .as_ref() - .map(|o| o.as_ref().map(|u| u.as_ref())) + .map(|o| o.as_ref()) } /// Set the upload_media for the current object @@ -3065,7 +3065,7 @@ pub trait ApObjectExt: AsApObject { /// # } /// ``` fn set_upload_media(&mut self, upload_media: Url) -> &mut Self { - self.ap_object_mut().upload_media = Some(XsdAnyUri::from(upload_media).into()); + self.ap_object_mut().upload_media = Some(upload_media.into()); self } @@ -3087,11 +3087,12 @@ pub trait ApObjectExt: AsApObject { /// # Ok(()) /// # } /// ``` - fn set_many_upload_medias(&mut self, items: I) -> &mut Self + fn set_many_upload_medias(&mut self, items: I) -> &mut Self where - I: IntoIterator, + I: IntoIterator, + U: Into, { - let v: Vec = items.into_iter().map(|u| u.into()).collect(); + let v: Vec = items.into_iter().map(|u| u.into()).collect(); self.ap_object_mut().upload_media = Some(v.into()); self } @@ -3116,7 +3117,7 @@ pub trait ApObjectExt: AsApObject { fn add_upload_media(&mut self, upload_media: Url) -> &mut Self { let v = match self.ap_object_mut().upload_media.take() { Some(mut v) => { - v.add(XsdAnyUri::from(upload_media)); + v.add(upload_media); v } None => vec![upload_media.into()].into(), @@ -3138,10 +3139,7 @@ pub trait ApObjectExt: AsApObject { /// } /// ``` fn take_upload_media(&mut self) -> Option> { - self.ap_object_mut() - .upload_media - .take() - .map(|o| o.map(|u| u.into_inner())) + self.ap_object_mut().upload_media.take() } /// Delete the upload_media from the current object @@ -4494,7 +4492,7 @@ pub struct ApObject { /// - Range: anyUri /// - Functional: true #[serde(skip_serializing_if = "Option::is_none")] - shares: Option, + shares: Option, /// This is a list of all Like activities with this object as the object property, added as a /// side effect. @@ -4506,7 +4504,7 @@ pub struct ApObject { /// - Range: anyUri /// - Functional: true #[serde(skip_serializing_if = "Option::is_none")] - likes: Option, + likes: Option, /// The source property is intended to convey some sort of source from which the content markup /// was derived, as a form of provenance, or to support future editing by clients. @@ -4531,7 +4529,7 @@ pub struct ApObject { /// - Range: anyUri /// - Functional: false #[serde(skip_serializing_if = "Option::is_none")] - upload_media: Option>, + upload_media: Option>, /// The ActivityStreams object being extended #[serde(flatten)] diff --git a/src/primitives/mod.rs b/src/primitives/mod.rs index 37e682d..41dad9a 100644 --- a/src/primitives/mod.rs +++ b/src/primitives/mod.rs @@ -32,8 +32,5 @@ pub use self::{ use self::serde_parse::SerdeParse; -/// An alias for the url::Url struct with serde compatibility -pub type XsdAnyUri = SerdeParse; - /// An alias for the mime::Mime struct with serde compatibility pub(crate) type MimeMediaType = SerdeParse; diff --git a/src/unparsed.rs b/src/unparsed.rs index 76e42c6..dbbfe66 100644 --- a/src/unparsed.rs +++ b/src/unparsed.rs @@ -31,8 +31,8 @@ //! #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] //! #[serde(rename_all = "camelCase")] //! pub struct PublicKeyValues { -//! pub id: XsdAnyUri, -//! pub owner: XsdAnyUri, +//! pub id: Url, +//! pub owner: Url, //! pub public_key_pem: String, //! } //! @@ -172,12 +172,12 @@ //! where //! Inner: 'a, //! { -//! &self.public_key_ref().public_key.id.as_ref() +//! &self.public_key_ref().public_key.id //! } //! //! /// Set the public key's ID //! fn set_key_id(&mut self, id: Url) -> &mut Self { -//! self.public_key_mut().public_key.id = id.into(); +//! self.public_key_mut().public_key.id = id; //! self //! } //! @@ -186,12 +186,12 @@ //! where //! Inner: 'a, //! { -//! &self.public_key_ref().public_key.owner.as_ref() +//! &self.public_key_ref().public_key.owner //! } //! //! /// Set the public key's Owner //! fn set_key_owner(&mut self, owner: Url) -> &mut Self { -//! self.public_key_mut().public_key.owner = owner.into(); +//! self.public_key_mut().public_key.owner = owner; //! self //! } //! @@ -231,8 +231,8 @@ //! owner.set_fragment(Some("main-key")); //! PublicKey { //! public_key: PublicKeyValues { -//! id: id.into(), -//! owner: owner.into(), +//! id, +//! owner, //! public_key_pem: String::new(), //! }, //! inner: ApActor::new(inbox, Person::new()),