From 49b7b35be51c5037fcf42d66959c1d0b6efa5ab7 Mon Sep 17 00:00:00 2001 From: asonix Date: Wed, 18 Nov 2020 10:41:33 -0600 Subject: [PATCH] Add orderedItems field to collections --- Cargo.toml | 2 +- src/collection.rs | 146 +++++++++++++++++++++++++++++++++++++++++++++- src/lib.rs | 3 + 3 files changed, 148 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2041bbc..81da331 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "activitystreams" description = "A set of core types and traits for activitystreams data" -version = "0.7.0-alpha.4" +version = "0.7.0-alpha.5" license = "GPL-3.0" authors = ["asonix "] repository = "https://git.asonix.dog/Aardwolf/activitystreams" diff --git a/src/collection.rs b/src/collection.rs index 4a12740..0d60b00 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -119,7 +119,7 @@ pub trait CollectionExt: AsCollection { self } - /// Set many itemss for the current activity + /// Set many items for the current activity /// /// This overwrites the contents of items /// @@ -146,7 +146,7 @@ pub trait CollectionExt: AsCollection { self } - /// Add a items to the current activity + /// Add an item to the current activity /// /// This does not overwrite the contents of items, only appends an item /// @@ -208,6 +208,135 @@ pub trait CollectionExt: AsCollection { self } + /// Fetch the ordered_items for the current activity + /// + /// ```rust + /// # use activitystreams::{collection::OrderedCollection}; + /// # let mut collection = OrderedCollection::new(); + /// # + /// use activitystreams::prelude::*; + /// + /// let items_ref = collection.ordered_items(); + /// println!("{:?}", items_ref); + /// ``` + fn ordered_items<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.collection_ref().ordered_items.as_ref() + } + + /// Set the ordered_items for the current activity + /// + /// This overwrites the contents of ordered_items + /// + /// ```rust + /// # fn main() -> Result<(), anyhow::Error> { + /// use activitystreams::prelude::*; + /// # use activitystreams::{collection::OrderedCollection, uri}; + /// # let mut collection = OrderedCollection::new(); + /// + /// collection.set_ordered_item(uri!("https://example.com")); + /// # Ok(()) + /// # } + /// ``` + fn set_ordered_item(&mut self, item: T) -> &mut Self + where + T: Into, + { + self.collection_mut().ordered_items = Some(item.into().into()); + self + } + + /// Set many ordered_items for the current activity + /// + /// This overwrites the contents of ordered_items + /// + /// ```rust + /// # fn main() -> Result<(), anyhow::Error> { + /// use activitystreams::prelude::*; + /// # use activitystreams::{collection::OrderedCollection, uri}; + /// # let mut collection = OrderedCollection::new(); + /// + /// collection.set_many_ordered_items(vec![ + /// uri!("https://example.com/one"), + /// uri!("https://example.com/two"), + /// ]); + /// # Ok(()) + /// # } + /// ``` + fn set_many_ordered_items(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.collection_mut().ordered_items = Some(v.into()); + self + } + + /// Add an ordered_item to the current activity + /// + /// This does not overwrite the contents of ordered_items, only appends an item + /// + /// ```rust + /// # fn main() -> Result<(), anyhow::Error> { + /// use activitystreams::prelude::*; + /// # use activitystreams::{collection::OrderedCollection, uri}; + /// # let mut collection = OrderedCollection::new(); + /// + /// collection + /// .add_ordered_item(uri!("https://example.com/one")) + /// .add_ordered_item(uri!("https://example.com/two")); + /// # Ok(()) + /// # } + /// ``` + fn add_ordered_item(&mut self, item: T) -> &mut Self + where + T: Into, + { + match self.collection_mut().ordered_items { + Some(ref mut items) => { + items.add(item.into()); + } + None => self.collection_mut().ordered_items = Some(item.into().into()), + } + + self + } + + /// Take the ordered_items of the current object, leaving nothing + /// + /// ```rust + /// # use activitystreams::{collection::OrderedCollection}; + /// # let mut collection = OrderedCollection::new(); + /// use activitystreams::prelude::*; + /// + /// if let Some(items) = collection.take_ordered_items() { + /// println!("{:?}", items); + /// } + /// ``` + fn take_ordered_items(&mut self) -> Option> { + self.collection_mut().ordered_items.take() + } + + /// Delete the ordered_items from the current object + /// + /// ```rust + /// # use activitystreams::{context, collection::OrderedCollection}; + /// # let mut collection = OrderedCollection::new(); + /// # collection.set_ordered_item(context()); + /// use activitystreams::prelude::*; + /// + /// assert!(collection.ordered_items().is_some()); + /// collection.delete_ordered_items(); + /// assert!(collection.ordered_items().is_none()); + /// ``` + fn delete_ordered_items(&mut self) -> &mut Self { + self.collection_mut().ordered_items = None; + self + } + /// Fetch the total_items of the current object /// /// ```rust @@ -846,6 +975,13 @@ pub struct Collection { #[serde(skip_serializing_if = "Option::is_none")] items: Option>, + /// Identifies ordered items contained in a collection. + /// + /// - Range: Object | Link | Ordered List of [ Object | Link ] + /// - Functional: false + #[serde(skip_serializing_if = "Option::is_none")] + ordered_items: Option>, + /// A non-negative integer specifying the total number of objects contained by the logical view /// of the collection. /// @@ -949,6 +1085,7 @@ impl Collection { { Collection { items: None, + ordered_items: None, total_items: None, current: None, first: None, @@ -976,6 +1113,7 @@ impl Collection { pub fn new_none_type() -> Self { Collection { items: None, + ordered_items: None, total_items: None, current: None, first: None, @@ -986,6 +1124,7 @@ impl Collection { fn extending(mut inner: Object) -> Result { let items = inner.remove("items")?; + let ordered_items = inner.remove("orderedItems")?; let total_items = inner.remove("totalItems")?; let current = inner.remove("current")?; let first = inner.remove("first")?; @@ -993,6 +1132,7 @@ impl Collection { Ok(Collection { items, + ordered_items, total_items, current, first, @@ -1004,6 +1144,7 @@ impl Collection { fn retracting(self) -> Result, serde_json::Error> { let Collection { items, + ordered_items, total_items, current, first, @@ -1016,6 +1157,7 @@ impl Collection { .insert("first", first)? .insert("current", current)? .insert("totalItems", total_items)? + .insert("orderedItems", ordered_items)? .insert("items", items)?; Ok(inner) diff --git a/src/lib.rs b/src/lib.rs index 9a25514..5725ab6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -312,16 +312,19 @@ pub extern crate chrono; pub extern crate mime; pub extern crate url; +/// Returns the `https://www.w3.org/ns/activitystreams` Url pub fn context() -> url::Url { "https://www.w3.org/ns/activitystreams".parse().unwrap() } +/// Returns the `https://www.w3.org/ns/activitystreams#Public` Url pub fn public() -> url::Url { "https://www.w3.org/ns/activitystreams#Public" .parse() .unwrap() } +/// Returns the `https://w3id.org/security/v1` Url pub fn security() -> url::Url { "https://w3id.org/security/v1".parse().unwrap() }