use crate::{ base::{AnyBase, AsBase, Base, Extends}, markers, object::{ApObject, AsObject, Object}, primitives::{OneOrMany, XsdNonNegativeInteger}, unparsed::{Unparsed, UnparsedMut, UnparsedMutExt}, }; use std::convert::TryFrom; use typed_builder::TypedBuilder; pub mod kind { pub use activitystreams::collection::kind::*; } use self::kind::*; pub trait AsCollection: markers::Collection { fn collection_ref(&self) -> &Collection; fn collection_mut(&mut self) -> &mut Collection; } pub trait AsCollectionPage: markers::CollectionPage { fn collection_page_ref(&self) -> &CollectionPage; fn collection_page_mut(&mut self) -> &mut CollectionPage; } pub trait AsOrderedCollectionPage: markers::CollectionPage { fn ordered_collection_page_ref(&self) -> &OrderedCollectionPage; fn ordered_collection_page_mut(&mut self) -> &mut OrderedCollectionPage; } pub trait CollectionExt: AsCollection { fn items<'a>(&'a self) -> &'a OneOrMany where Kind: 'a, { &self.collection_ref().items } 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 total_items<'a>(&'a self) -> Option<&'a XsdNonNegativeInteger> where Kind: 'a, { self.collection_ref().total_items.as_ref() } 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 current<'a>(&'a self) -> Option<&'a AnyBase> where Kind: 'a, { self.collection_ref().current.as_ref() } 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 first<'a>(&'a self) -> Option<&'a AnyBase> where Kind: 'a, { self.collection_ref().first.as_ref() } 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 last<'a>(&'a self) -> Option<&'a AnyBase> where Kind: 'a, { self.collection_ref().last.as_ref() } 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 CollectionPageExt: AsCollectionPage { fn part_of<'a>(&'a self) -> Option<&'a AnyBase> where Kind: 'a, { self.collection_page_ref().part_of.as_ref() } 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 next<'a>(&'a self) -> Option<&'a AnyBase> where Kind: 'a, { self.collection_page_ref().next.as_ref() } 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 prev<'a>(&'a self) -> Option<&'a AnyBase> where Kind: 'a, { self.collection_page_ref().prev.as_ref() } 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 OrderedCollectionPageExt: AsOrderedCollectionPage { fn start_index(&self) -> Option<&XsdNonNegativeInteger> { self.ordered_collection_page_ref().start_index.as_ref() } 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 markers::Base for Collection {} impl markers::Object for Collection {} impl markers::Collection for Collection {} impl markers::Base for CollectionPage {} impl markers::Object for CollectionPage {} impl markers::Collection for CollectionPage {} impl markers::CollectionPage for CollectionPage {} impl markers::Base for OrderedCollectionPage {} impl markers::Object for OrderedCollectionPage {} impl markers::Collection for OrderedCollectionPage {} impl markers::CollectionPage for OrderedCollectionPage {} impl Extends for Collection where Kind: serde::de::DeserializeOwned + serde::ser::Serialize, { type Error = serde_json::Error; fn extends(base: Base) -> Result { let inner = Object::extends(base)?; Self::extending(inner) } fn retracts(self) -> Result, Self::Error> { let inner = self.retracting()?; inner.retracts() } } 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 where Kind: serde::de::DeserializeOwned + serde::ser::Serialize, { type Error = serde_json::Error; fn extends(base: Base) -> Result { let inner = Object::extends(base)?; Self::extending(inner) } fn retracts(self) -> Result, Self::Error> { let inner = self.retracting()?; inner.retracts() } } 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(base: Base) -> Result { let inner = Object::extends(base)?; Self::extending(inner) } fn retracts(self) -> Result, Self::Error> { let inner = self.retracting()?; inner.retracts() } } 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 AsBase for Collection { fn base_ref(&self) -> &Base { self.inner.base_ref() } fn base_mut(&mut self) -> &mut Base { self.inner.base_mut() } } impl AsObject for Collection { fn object_ref(&self) -> &Object { &self.inner } fn object_mut(&mut self) -> &mut Object { &mut self.inner } } impl AsCollection for Collection { fn collection_ref(&self) -> &Collection { self } fn collection_mut(&mut self) -> &mut Collection { self } } impl AsBase for CollectionPage { fn base_ref(&self) -> &Base { self.inner.base_ref() } fn base_mut(&mut self) -> &mut Base { self.inner.base_mut() } } impl AsObject for CollectionPage { fn object_ref(&self) -> &Object { self.inner.object_ref() } fn object_mut(&mut self) -> &mut Object { self.inner.object_mut() } } impl AsCollection for CollectionPage { fn collection_ref(&self) -> &Collection { &self.inner } fn collection_mut(&mut self) -> &mut Collection { &mut self.inner } } impl AsCollectionPage for CollectionPage { fn collection_page_ref(&self) -> &CollectionPage { self } fn collection_page_mut(&mut self) -> &mut CollectionPage { self } } impl AsBase for OrderedCollectionPage { fn base_ref(&self) -> &Base { self.inner.base_ref() } fn base_mut(&mut self) -> &mut Base { self.inner.base_mut() } } impl AsObject for OrderedCollectionPage { fn object_ref(&self) -> &Object { self.inner.object_ref() } fn object_mut(&mut self) -> &mut Object { self.inner.object_mut() } } impl AsCollection for OrderedCollectionPage { fn collection_ref(&self) -> &Collection { self.inner.collection_ref() } fn collection_mut(&mut self) -> &mut Collection { self.inner.collection_mut() } } impl AsCollectionPage for OrderedCollectionPage { fn collection_page_ref(&self) -> &CollectionPage { &self.inner } fn collection_page_mut(&mut self) -> &mut CollectionPage { &mut self.inner } } impl markers::Collection for ApObject where Inner: markers::Collection {} impl markers::CollectionPage for ApObject where Inner: markers::CollectionPage {} impl AsCollection for ApObject where Inner: AsCollection, { fn collection_ref(&self) -> &Collection { self.inner.collection_ref() } fn collection_mut(&mut self) -> &mut Collection { self.inner.collection_mut() } } impl AsCollectionPage for ApObject where Inner: AsCollectionPage, { fn collection_page_ref(&self) -> &CollectionPage { self.inner.collection_page_ref() } fn collection_page_mut(&mut self) -> &mut CollectionPage { self.inner.collection_page_mut() } } impl AsOrderedCollectionPage for ApObject where Inner: AsOrderedCollectionPage, { fn ordered_collection_page_ref(&self) -> &OrderedCollectionPage { self.inner.ordered_collection_page_ref() } fn ordered_collection_page_mut(&mut self) -> &mut OrderedCollectionPage { self.inner.ordered_collection_page_mut() } } impl CollectionExt for T where T: AsCollection {} impl CollectionPageExt for T where T: AsCollectionPage {} impl OrderedCollectionPageExt for T where T: AsOrderedCollectionPage {}