use crate::{ object::{AnyObject, ApObject, Object, ObjectMut, ObjectRef}, primitives::{OneOrMany, Unparsed, XsdNonNegativeInteger}, traits::{self, Extends, UnparsedMut, UnparsedMutExt}, }; use std::convert::TryFrom; use typed_builder::TypedBuilder; pub mod kind { pub use activitystreams::collection::kind::*; } use self::kind::*; pub trait CollectionRef: traits::Collection { fn collection_ref(&self) -> &Collection; } pub trait CollectionMut: traits::Collection { fn collection_mut(&mut self) -> &mut Collection; } pub trait CollectionPageRef: traits::CollectionPage { fn collection_page_ref(&self) -> &CollectionPage; } pub trait CollectionPageMut: traits::CollectionPage { fn collection_page_mut(&mut self) -> &mut CollectionPage; } pub trait OrderedCollectionPageRef: traits::CollectionPage { fn ordered_collection_page_ref(&self) -> &OrderedCollectionPage; } pub trait OrderedCollectionPageMut: traits::CollectionPage { fn ordered_collection_page_mut(&mut self) -> &mut OrderedCollectionPage; } pub trait CollectionRefExt: CollectionRef { fn items<'a>(&'a self) -> &'a OneOrMany where Kind: 'a, { &self.collection_ref().items } fn total_items<'a>(&'a self) -> Option<&'a XsdNonNegativeInteger> where Kind: 'a, { self.collection_ref().total_items.as_ref() } fn current<'a>(&'a self) -> Option<&'a AnyObject> where Kind: 'a, { self.collection_ref().current.as_ref() } fn first<'a>(&'a self) -> Option<&'a AnyObject> where Kind: 'a, { self.collection_ref().first.as_ref() } fn last<'a>(&'a self) -> Option<&'a AnyObject> where Kind: 'a, { self.collection_ref().last.as_ref() } } pub trait CollectionMutExt: CollectionMut { fn set_items(&mut self, item: T) -> &mut Self where T: Into, { self.collection_mut().items = item.into().into(); self } fn set_many_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().items = v.into(); self } fn add_item(&mut self, item: T) -> &mut Self where T: Into, { self.collection_mut().items.add(item.into()); self } fn set_total_items(&mut self, total_items: T) -> &mut Self where T: Into, { self.collection_mut().total_items = Some(total_items.into()); self } fn take_total_items(&mut self) -> Option { self.collection_mut().total_items.take() } fn delete_total_items(&mut self) -> &mut Self { self.collection_mut().total_items = None; self } fn set_current(&mut self, current: T) -> &mut Self where T: Into, { self.collection_mut().current = Some(current.into()); self } fn take_current(&mut self) -> Option { self.collection_mut().current.take() } fn delete_current(&mut self) -> &mut Self { self.collection_mut().current = None; self } fn set_first(&mut self, first: T) -> &mut Self where T: Into, { self.collection_mut().first = Some(first.into()); self } fn take_first(&mut self) -> Option { self.collection_mut().first.take() } fn delete_first(&mut self) -> &mut Self { self.collection_mut().first = None; self } fn set_last(&mut self, last: T) -> &mut Self where T: Into, { self.collection_mut().last = Some(last.into()); self } fn take_last(&mut self) -> Option { self.collection_mut().last.take() } fn delete_last(&mut self) -> &mut Self { self.collection_mut().last = None; self } } pub trait CollectionPageRefExt: CollectionPageRef { fn part_of<'a>(&'a self) -> Option<&'a AnyObject> where Kind: 'a, { self.collection_page_ref().part_of.as_ref() } fn next<'a>(&'a self) -> Option<&'a AnyObject> where Kind: 'a, { self.collection_page_ref().next.as_ref() } fn prev<'a>(&'a self) -> Option<&'a AnyObject> where Kind: 'a, { self.collection_page_ref().prev.as_ref() } } pub trait CollectionPageMutExt: CollectionPageMut { fn set_part_of(&mut self, part_of: T) -> &mut Self where T: Into, { self.collection_page_mut().part_of = Some(part_of.into()); self } fn take_part_of(&mut self) -> Option { self.collection_page_mut().part_of.take() } fn delete_part_of(&mut self) -> &mut Self { self.collection_page_mut().part_of = None; self } fn set_next(&mut self, next: T) -> &mut Self where T: Into, { self.collection_page_mut().next = Some(next.into()); self } fn take_next(&mut self) -> Option { self.collection_page_mut().next.take() } fn delete_next(&mut self) -> &mut Self { self.collection_page_mut().next = None; self } fn set_prev(&mut self, prev: T) -> &mut Self where T: Into, { self.collection_page_mut().prev = Some(prev.into()); self } fn take_prev(&mut self) -> Option { self.collection_page_mut().prev.take() } fn delete_prev(&mut self) -> &mut Self { self.collection_page_mut().prev = None; self } } pub trait OrderedCollectionPageRefExt: OrderedCollectionPageRef { fn start_index(&self) -> Option<&XsdNonNegativeInteger> { self.ordered_collection_page_ref().start_index.as_ref() } } pub trait OrderedCollectionPageMutExt: OrderedCollectionPageMut { fn set_start_index(&mut self, start_index: T) -> &mut Self where T: Into, { self.ordered_collection_page_mut().start_index = Some(start_index.into()); self } fn take_start_index(&mut self) -> Option { self.ordered_collection_page_mut().start_index.take() } fn delete_start_index(&mut self) -> &mut Self { self.ordered_collection_page_mut().start_index = None; self } } pub type OrderedCollection = Collection; pub type UnorderedCollection = Collection; pub type UnorderedCollectionPage = CollectionPage; #[derive(Debug, Clone, serde::Deserialize, serde::Serialize, TypedBuilder)] #[serde(rename_all = "camelCase")] #[builder(doc)] pub struct Collection { #[builder(setter(into))] pub items: OneOrMany, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] pub total_items: Option, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] pub current: Option, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] pub first: Option, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] pub last: Option, #[serde(flatten)] pub inner: Object, } #[derive(Debug, Clone, serde::Deserialize, serde::Serialize, TypedBuilder)] #[serde(rename_all = "camelCase")] #[builder(doc)] pub struct CollectionPage { #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] pub part_of: Option, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] pub next: Option, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] pub prev: Option, #[serde(flatten)] pub inner: Collection, } #[derive(Debug, Clone, serde::Deserialize, serde::Serialize, TypedBuilder)] #[serde(rename_all = "camelCase")] #[builder(doc)] pub struct OrderedCollectionPage { #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] pub start_index: Option, #[serde(flatten)] pub inner: CollectionPage, } impl Collection { fn extending(mut inner: Object) -> Result { let items = inner.remove("items")?; let total_items = inner.remove("totalItems")?; let current = inner.remove("current")?; let first = inner.remove("first")?; let last = inner.remove("last")?; Ok(Collection { items, total_items, current, first, last, inner, }) } fn retracting(self) -> Result, serde_json::Error> { let Collection { items, total_items, current, first, last, mut inner, } = self; inner .insert("last", last)? .insert("first", first)? .insert("current", current)? .insert("totalItems", total_items)? .insert("items", items)?; Ok(inner) } } impl CollectionPage { fn extending(object: Object) -> Result { let mut inner = Collection::extending(object)?; let part_of = inner.remove("partOf")?; let next = inner.remove("next")?; let prev = inner.remove("prev")?; Ok(CollectionPage { part_of, next, prev, inner, }) } fn retracting(self) -> Result, serde_json::Error> { let CollectionPage { part_of, next, prev, mut inner, } = self; inner .insert("prev", prev)? .insert("next", next)? .insert("partOf", part_of)?; inner.retracting() } } impl OrderedCollectionPage { fn extending(object: Object) -> Result { let mut inner = CollectionPage::extending(object)?; let start_index = inner.remove("startIndex")?; Ok(OrderedCollectionPage { start_index, inner }) } fn retracting(self) -> Result, serde_json::Error> { let OrderedCollectionPage { start_index, mut inner, } = self; inner.insert("startIndex", start_index)?; inner.retracting() } } impl traits::Base for Collection {} impl traits::Object for Collection {} impl traits::Collection for Collection {} impl traits::Base for CollectionPage {} impl traits::Object for CollectionPage {} impl traits::Collection for CollectionPage {} impl traits::CollectionPage for CollectionPage {} impl traits::Base for OrderedCollectionPage {} impl traits::Object for OrderedCollectionPage {} impl traits::Collection for OrderedCollectionPage {} impl traits::CollectionPage for OrderedCollectionPage {} impl Extends> for Collection { type Error = serde_json::Error; fn extends(object: Object) -> Result { Self::extending(object) } fn retracts(self) -> Result, Self::Error> { self.retracting() } } impl TryFrom> for Object { type Error = serde_json::Error; fn try_from(collection: Collection) -> Result { collection.retracting() } } impl TryFrom> for Collection { type Error = serde_json::Error; fn try_from(object: Object) -> Result { Self::extending(object) } } impl Extends> for CollectionPage { type Error = serde_json::Error; fn extends(object: Object) -> Result { Self::extending(object) } fn retracts(self) -> Result, Self::Error> { self.retracting() } } impl TryFrom> for CollectionPage { type Error = serde_json::Error; fn try_from(object: Object) -> Result { Self::extending(object) } } impl TryFrom> for Object { type Error = serde_json::Error; fn try_from(collection_page: CollectionPage) -> Result { collection_page.retracting() } } impl Extends> for OrderedCollectionPage { type Error = serde_json::Error; fn extends(object: Object) -> Result { Self::extending(object) } fn retracts(self) -> Result, Self::Error> { self.retracting() } } impl TryFrom> for OrderedCollectionPage { type Error = serde_json::Error; fn try_from(object: Object) -> Result { Self::extending(object) } } impl TryFrom for Object { type Error = serde_json::Error; fn try_from(collection_page: OrderedCollectionPage) -> Result { collection_page.retracting() } } impl UnparsedMut for Collection { fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } impl UnparsedMut for CollectionPage { fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } impl UnparsedMut for OrderedCollectionPage { fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } impl ObjectRef for Collection { fn object_ref(&self) -> &Object { &self.inner } } impl ObjectMut for Collection { fn object_mut(&mut self) -> &mut Object { &mut self.inner } } impl CollectionRef for Collection { fn collection_ref(&self) -> &Collection { self } } impl CollectionMut for Collection { fn collection_mut(&mut self) -> &mut Collection { self } } impl ObjectRef for CollectionPage { fn object_ref(&self) -> &Object { self.inner.object_ref() } } impl ObjectMut for CollectionPage { fn object_mut(&mut self) -> &mut Object { self.inner.object_mut() } } impl CollectionRef for CollectionPage { fn collection_ref(&self) -> &Collection { &self.inner } } impl CollectionMut for CollectionPage { fn collection_mut(&mut self) -> &mut Collection { &mut self.inner } } impl CollectionPageRef for CollectionPage { fn collection_page_ref(&self) -> &CollectionPage { self } } impl CollectionPageMut for CollectionPage { fn collection_page_mut(&mut self) -> &mut CollectionPage { self } } impl ObjectRef for OrderedCollectionPage { fn object_ref(&self) -> &Object { self.inner.object_ref() } } impl ObjectMut for OrderedCollectionPage { fn object_mut(&mut self) -> &mut Object { self.inner.object_mut() } } impl CollectionRef for OrderedCollectionPage { fn collection_ref(&self) -> &Collection { self.inner.collection_ref() } } impl CollectionMut for OrderedCollectionPage { fn collection_mut(&mut self) -> &mut Collection { self.inner.collection_mut() } } impl CollectionPageRef for OrderedCollectionPage { fn collection_page_ref(&self) -> &CollectionPage { &self.inner } } impl CollectionPageMut for OrderedCollectionPage { fn collection_page_mut(&mut self) -> &mut CollectionPage { &mut self.inner } } impl traits::Collection for ApObject where Inner: traits::Collection {} impl traits::CollectionPage for ApObject where Inner: traits::CollectionPage {} impl CollectionRef for ApObject where Inner: CollectionRef, { fn collection_ref(&self) -> &Collection { self.inner.collection_ref() } } impl CollectionMut for ApObject where Inner: CollectionMut, { fn collection_mut(&mut self) -> &mut Collection { self.inner.collection_mut() } } impl CollectionPageRef for ApObject where Inner: CollectionPageRef, { fn collection_page_ref(&self) -> &CollectionPage { self.inner.collection_page_ref() } } impl CollectionPageMut for ApObject where Inner: CollectionPageMut, { fn collection_page_mut(&mut self) -> &mut CollectionPage { self.inner.collection_page_mut() } } impl OrderedCollectionPageRef for ApObject where Inner: OrderedCollectionPageRef, { fn ordered_collection_page_ref(&self) -> &OrderedCollectionPage { self.inner.ordered_collection_page_ref() } } impl OrderedCollectionPageMut for ApObject where Inner: OrderedCollectionPageMut, { fn ordered_collection_page_mut(&mut self) -> &mut OrderedCollectionPage { self.inner.ordered_collection_page_mut() } } impl CollectionRefExt for T where T: CollectionRef {} impl CollectionMutExt for T where T: CollectionMut {} impl CollectionPageRefExt for T where T: CollectionPageRef {} impl CollectionPageMutExt for T where T: CollectionPageMut {} impl OrderedCollectionPageRefExt for T where T: OrderedCollectionPageRef {} impl OrderedCollectionPageMutExt for T where T: OrderedCollectionPageMut {}