Add into_parts to deconstruct some objects

This commit is contained in:
asonix 2020-07-26 11:56:37 -05:00
parent 03ad448fc3
commit 394bf499f9
9 changed files with 271 additions and 10 deletions

View file

@ -1,7 +1,7 @@
[package]
name = "activitystreams"
description = "A set of core types and traits for activitystreams data"
version = "0.7.0-alpha.2"
version = "0.7.0-alpha.3"
license = "GPL-3.0"
authors = ["asonix <asonix@asonix.dog>"]
repository = "https://git.asonix.dog/Aardwolf/activitystreams"

View file

@ -11,7 +11,7 @@ _A set of Traits and Types that make up the ActivityStreams and ActivityPub spec
First, add ActivityStreams to your dependencies
```toml
[dependencies]
activitystreams = "0.7.0-alpha.2"
activitystreams = "0.7.0-alpha.3"
```
### Types

View file

@ -1,7 +1,7 @@
[package]
name = "activitystreams-ext"
description = "Extension types for the ActivityStreams crate"
version = "0.1.0-alpha.0"
version = "0.1.0-alpha.1"
license = "GPL-3.0"
authors = ["asonix <asonix@asonix.dog>"]
repository = "https://git.asonix.dog/Aardwolf/activitystreams"
@ -13,7 +13,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
activitystreams = { version = "0.7.0-alpha.0", path = ".." }
activitystreams = { version = "0.7.0-alpha.3", path = ".." }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

View file

@ -11,8 +11,8 @@ _This crate provides Ext1, Ext2, Ext3, and Ext4 for adding extensions to Activit
First, add ActivityStreams to your dependencies
```toml
[dependencies]
activitystreams = "0.7.0-alpha.0"
activitystreams-ext = "0.1.0-alpha.0"
activitystreams = "0.7.0-alpha.3"
activitystreams-ext = "0.1.0-alpha.1"
```
For an example, we'll implement a PublicKey extension and demonstrate usage with Ext1

View file

@ -11,8 +11,8 @@
//! First, add ActivityStreams to your dependencies
//! ```toml
//! [dependencies]
//! activitystreams = "0.7.0-alpha.0"
//! activitystreams-ext = "0.1.0-alpha.0"
//! activitystreams = "0.7.0-alpha.3"
//! activitystreams-ext = "0.1.0-alpha.1"
//! ```
//!
//! For an example, we'll implement a PublicKey extension and demonstrate usage with Ext1
@ -185,6 +185,10 @@ impl<Inner, A> Ext1<Inner, A> {
Ext1 { inner, ext_one }
}
pub fn into_parts(self) -> (Inner, A) {
(self.inner, self.ext_one)
}
pub fn extend<B>(self, ext_two: B) -> Ext2<Inner, A, B> {
Ext2 {
inner: self.inner,
@ -203,6 +207,10 @@ impl<Inner, A, B> Ext2<Inner, A, B> {
}
}
pub fn into_parts(self) -> (Inner, A, B) {
(self.inner, self.ext_one, self.ext_two)
}
pub fn extend<C>(self, ext_three: C) -> Ext3<Inner, A, B, C> {
Ext3 {
inner: self.inner,
@ -223,6 +231,10 @@ impl<Inner, A, B, C> Ext3<Inner, A, B, C> {
}
}
pub fn into_parts(self) -> (Inner, A, B, C) {
(self.inner, self.ext_one, self.ext_two, self.ext_three)
}
pub fn extend<D>(self, ext_four: D) -> Ext4<Inner, A, B, C, D> {
Ext4 {
inner: self.inner,
@ -234,6 +246,28 @@ impl<Inner, A, B, C> Ext3<Inner, A, B, C> {
}
}
impl<Inner, A, B, C, D> Ext4<Inner, A, B, C, D> {
pub fn new(inner: Inner, ext_one: A, ext_two: B, ext_three: C, ext_four: D) -> Self {
Ext4 {
inner,
ext_one,
ext_two,
ext_three,
ext_four,
}
}
pub fn into_parts(self) -> (Inner, A, B, C, D) {
(
self.inner,
self.ext_one,
self.ext_two,
self.ext_three,
self.ext_four,
)
}
}
impl<Inner, A, Kind, Error> Extends<Kind> for Ext1<Inner, A>
where
Inner: Extends<Kind, Error = Error> + UnparsedMut,

View file

@ -2024,6 +2024,19 @@ impl<Kind> ActorAndObject<Kind> {
}
}
/// Deconstruct the ActorAndObject into its parts
///
/// ```rust
/// use activitystreams::activity::ActorAndObject;
///
/// let activity = ActorAndObject::<String>::new(vec![], vec![]);
///
/// let (actor, object, activity) = activity.into_parts();
/// ```
pub fn into_parts(self) -> (OneOrMany<AnyBase>, OneOrMany<AnyBase>, Activity<Kind>) {
(self.actor, self.object, self.inner)
}
fn extending(object: Object<Kind>) -> Result<Self, serde_json::Error> {
let mut inner = Activity::extending(object)?;
@ -2070,6 +2083,19 @@ impl Arrive {
}
}
/// Deconstruct the Arrive into its parts
///
/// ```rust
/// use activitystreams::activity::Arrive;
///
/// let activity = Arrive::new(vec![], vec![]);
///
/// let (actor, origin, activity) = activity.into_parts();
/// ```
pub fn into_parts(self) -> (OneOrMany<AnyBase>, OneOrMany<AnyBase>, Activity<ArriveType>) {
(self.actor, self.origin, self.inner)
}
fn extending(object: Object<ArriveType>) -> Result<Self, serde_json::Error> {
let mut inner = Activity::extending(object)?;
@ -2118,6 +2144,26 @@ impl Invite {
}
}
/// Deconstruct the Invite into its parts
///
/// ```rust
/// use activitystreams::activity::Invite;
///
/// let activity = Invite::new(vec![], vec![], vec![]);
///
/// let (actor, object, target, activity) = activity.into_parts();
/// ```
pub fn into_parts(
self,
) -> (
OneOrMany<AnyBase>,
OneOrMany<AnyBase>,
OneOrMany<AnyBase>,
Activity<InviteType>,
) {
(self.actor, self.object, self.target, self.inner)
}
fn extending(object: Object<InviteType>) -> Result<Self, serde_json::Error> {
let mut inner = Activity::extending(object)?;
@ -2171,6 +2217,26 @@ impl Delete {
}
}
/// Deconstruct the Delete into its parts
///
/// ```rust
/// use activitystreams::activity::Delete;
///
/// let activity = Delete::new(vec![], vec![]);
///
/// let (actor, object, origin, activity) = activity.into_parts();
/// ```
pub fn into_parts(
self,
) -> (
OneOrMany<AnyBase>,
OneOrMany<AnyBase>,
Option<OneOrMany<AnyBase>>,
Activity<DeleteType>,
) {
(self.actor, self.object, self.origin, self.inner)
}
fn extending(object: Object<DeleteType>) -> Result<Self, serde_json::Error> {
let mut inner = Activity::extending(object)?;
@ -2229,6 +2295,33 @@ impl<Kind> ActorAndObjectOptOriginAndTarget<Kind> {
}
}
/// Deconstruct the ActorAndObjectOptOriginAndTarget into its parts
///
/// ```rust
/// use activitystreams::activity::ActorAndObjectOptOriginAndTarget;
///
/// let activity = ActorAndObjectOptOriginAndTarget::<String>::new(vec![], vec![]);
///
/// let (actor, object, origin, target, activity) = activity.into_parts();
/// ```
pub fn into_parts(
self,
) -> (
OneOrMany<AnyBase>,
OneOrMany<AnyBase>,
Option<OneOrMany<AnyBase>>,
Option<OneOrMany<AnyBase>>,
Activity<Kind>,
) {
(
self.actor,
self.object,
self.origin,
self.target,
self.inner,
)
}
fn extending(object: Object<Kind>) -> Result<Self, serde_json::Error> {
let mut inner = Activity::extending(object)?;
@ -2290,6 +2383,26 @@ impl<Kind> ActorAndObjectOptTarget<Kind> {
}
}
/// Deconstruct the ActorAndObjectOptTarget into its parts
///
/// ```rust
/// use activitystreams::activity::ActorAndObjectOptTarget;
///
/// let activity = ActorAndObjectOptTarget::<String>::new(vec![], vec![]);
///
/// let (actor, object, target, activity) = activity.into_parts();
/// ```
pub fn into_parts(
self,
) -> (
OneOrMany<AnyBase>,
OneOrMany<AnyBase>,
Option<OneOrMany<AnyBase>>,
Activity<Kind>,
) {
(self.actor, self.object, self.target, self.inner)
}
fn extending(object: Object<Kind>) -> Result<Self, serde_json::Error> {
let mut inner = Activity::extending(object)?;
@ -2342,6 +2455,26 @@ impl Travel {
}
}
/// Deconstruct the Travel into its parts
///
/// ```rust
/// use activitystreams::activity::Travel;
///
/// let activity = Travel::new(vec![]);
///
/// let (actor, origin, target, activity) = activity.into_parts();
/// ```
pub fn into_parts(
self,
) -> (
OneOrMany<AnyBase>,
Option<OneOrMany<AnyBase>>,
Option<OneOrMany<AnyBase>>,
Activity<TravelType>,
) {
(self.actor, self.origin, self.target, self.inner)
}
fn extending(object: Object<TravelType>) -> Result<Self, serde_json::Error> {
let mut inner = Activity::extending(object)?;
@ -2390,6 +2523,25 @@ impl Question {
}
}
/// Deconstruct the Question into its parts
///
/// ```rust
/// use activitystreams::activity::Question;
///
/// let activity = Question::new();
///
/// let (one_of, any_of, activity) = activity.into_parts();
/// ```
pub fn into_parts(
self,
) -> (
Option<OneOrMany<AnyBase>>,
Option<OneOrMany<AnyBase>>,
Activity<QuestionType>,
) {
(self.one_of, self.any_of, self.inner)
}
fn extending(object: Object<QuestionType>) -> Result<Self, serde_json::Error> {
let mut inner = Activity::extending(object)?;

View file

@ -1287,6 +1287,54 @@ impl<Inner> ApActor<Inner> {
}
}
/// Deconstruct the ApActor into its parts
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::{actor::{ApActor, Person}, uri};
///
/// let actor = ApActor::new(uri!("https://inbox.url"), Person::new());
///
/// let (
/// inbox,
/// outbox,
/// following,
/// followers,
/// liked,
/// streams,
/// preferred_username,
/// endpoints,
/// person,
/// ) = actor.into_parts();
/// # Ok(())
/// # }
/// ```
pub fn into_parts(
self,
) -> (
Url,
Option<Url>,
Option<Url>,
Option<Url>,
Option<Url>,
Option<OneOrMany<Url>>,
Option<String>,
Option<Endpoints<Url>>,
Inner,
) {
(
self.inbox,
self.outbox,
self.following,
self.followers,
self.liked,
self.streams,
self.preferred_username,
self.endpoints,
self.inner,
)
}
fn extending(mut inner: Inner) -> Result<Self, serde_json::Error>
where
Inner: UnparsedMut + markers::Actor,

View file

@ -11,7 +11,7 @@
//! First, add ActivityStreams to your dependencies
//! ```toml
//! [dependencies]
//! activitystreams = "0.7.0-alpha.2"
//! activitystreams = "0.7.0-alpha.3"
//! ```
//!
//! ### Types
@ -293,7 +293,7 @@
//!
//! You should have received a copy of the GNU General Public License along with ActivityStreams. If not, see [http://www.gnu.org/licenses/](http://www.gnu.org/licenses/).
#![doc(html_root_url = "https://docs.rs/activitystreams/0.7.0-alpha.2/activitystreams")]
#![doc(html_root_url = "https://docs.rs/activitystreams/0.7.0-alpha.3/activitystreams")]
pub mod activity;
pub mod actor;

View file

@ -4846,6 +4846,33 @@ impl<Inner> ApObject<Inner> {
}
}
/// Deconstruct the ApObject into its parts
///
/// ```rust
/// use activitystreams::object::{ApObject, Image};
///
/// let object = ApObject::new(Image::new());
///
/// let (shares, likes, source, upload_media, image) = object.into_parts();
/// ```
pub fn into_parts(
self,
) -> (
Option<Url>,
Option<Url>,
Option<AnyBase>,
Option<OneOrMany<Url>>,
Inner,
) {
(
self.shares,
self.likes,
self.source,
self.upload_media,
self.inner,
)
}
fn extending(mut inner: Inner) -> Result<Self, serde_json::Error>
where
Inner: UnparsedMut + markers::Object,