Add orderedItems field to collections

This commit is contained in:
asonix 2020-11-18 10:41:33 -06:00
parent fb79b944b6
commit 49b7b35be5
3 changed files with 148 additions and 3 deletions

View file

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

View file

@ -119,7 +119,7 @@ pub trait CollectionExt<Kind>: AsCollection<Kind> {
self self
} }
/// Set many itemss for the current activity /// Set many items for the current activity
/// ///
/// This overwrites the contents of items /// This overwrites the contents of items
/// ///
@ -146,7 +146,7 @@ pub trait CollectionExt<Kind>: AsCollection<Kind> {
self 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 /// This does not overwrite the contents of items, only appends an item
/// ///
@ -208,6 +208,135 @@ pub trait CollectionExt<Kind>: AsCollection<Kind> {
self 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<AnyBase>>
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<T>(&mut self, item: T) -> &mut Self
where
T: Into<AnyBase>,
{
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<I, T>(&mut self, items: I) -> &mut Self
where
I: IntoIterator<Item = T>,
T: Into<AnyBase>,
{
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<T>(&mut self, item: T) -> &mut Self
where
T: Into<AnyBase>,
{
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<OneOrMany<AnyBase>> {
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 /// Fetch the total_items of the current object
/// ///
/// ```rust /// ```rust
@ -846,6 +975,13 @@ pub struct Collection<Kind> {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
items: Option<OneOrMany<AnyBase>>, items: Option<OneOrMany<AnyBase>>,
/// 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<OneOrMany<AnyBase>>,
/// A non-negative integer specifying the total number of objects contained by the logical view /// A non-negative integer specifying the total number of objects contained by the logical view
/// of the collection. /// of the collection.
/// ///
@ -949,6 +1085,7 @@ impl<Kind> Collection<Kind> {
{ {
Collection { Collection {
items: None, items: None,
ordered_items: None,
total_items: None, total_items: None,
current: None, current: None,
first: None, first: None,
@ -976,6 +1113,7 @@ impl<Kind> Collection<Kind> {
pub fn new_none_type() -> Self { pub fn new_none_type() -> Self {
Collection { Collection {
items: None, items: None,
ordered_items: None,
total_items: None, total_items: None,
current: None, current: None,
first: None, first: None,
@ -986,6 +1124,7 @@ impl<Kind> Collection<Kind> {
fn extending(mut inner: Object<Kind>) -> Result<Self, serde_json::Error> { fn extending(mut inner: Object<Kind>) -> Result<Self, serde_json::Error> {
let items = inner.remove("items")?; let items = inner.remove("items")?;
let ordered_items = inner.remove("orderedItems")?;
let total_items = inner.remove("totalItems")?; let total_items = inner.remove("totalItems")?;
let current = inner.remove("current")?; let current = inner.remove("current")?;
let first = inner.remove("first")?; let first = inner.remove("first")?;
@ -993,6 +1132,7 @@ impl<Kind> Collection<Kind> {
Ok(Collection { Ok(Collection {
items, items,
ordered_items,
total_items, total_items,
current, current,
first, first,
@ -1004,6 +1144,7 @@ impl<Kind> Collection<Kind> {
fn retracting(self) -> Result<Object<Kind>, serde_json::Error> { fn retracting(self) -> Result<Object<Kind>, serde_json::Error> {
let Collection { let Collection {
items, items,
ordered_items,
total_items, total_items,
current, current,
first, first,
@ -1016,6 +1157,7 @@ impl<Kind> Collection<Kind> {
.insert("first", first)? .insert("first", first)?
.insert("current", current)? .insert("current", current)?
.insert("totalItems", total_items)? .insert("totalItems", total_items)?
.insert("orderedItems", ordered_items)?
.insert("items", items)?; .insert("items", items)?;
Ok(inner) Ok(inner)

View file

@ -312,16 +312,19 @@ pub extern crate chrono;
pub extern crate mime; pub extern crate mime;
pub extern crate url; pub extern crate url;
/// Returns the `https://www.w3.org/ns/activitystreams` Url
pub fn context() -> url::Url { pub fn context() -> url::Url {
"https://www.w3.org/ns/activitystreams".parse().unwrap() "https://www.w3.org/ns/activitystreams".parse().unwrap()
} }
/// Returns the `https://www.w3.org/ns/activitystreams#Public` Url
pub fn public() -> url::Url { pub fn public() -> url::Url {
"https://www.w3.org/ns/activitystreams#Public" "https://www.w3.org/ns/activitystreams#Public"
.parse() .parse()
.unwrap() .unwrap()
} }
/// Returns the `https://w3id.org/security/v1` Url
pub fn security() -> url::Url { pub fn security() -> url::Url {
"https://w3id.org/security/v1".parse().unwrap() "https://w3id.org/security/v1".parse().unwrap()
} }