activitystreams-new/src/collection.rs
2020-05-15 17:01:29 -05:00

698 lines
18 KiB
Rust

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<Kind>: markers::Collection {
fn collection_ref(&self) -> &Collection<Kind>;
fn collection_mut(&mut self) -> &mut Collection<Kind>;
}
pub trait AsCollectionPage<Kind>: markers::CollectionPage {
fn collection_page_ref(&self) -> &CollectionPage<Kind>;
fn collection_page_mut(&mut self) -> &mut CollectionPage<Kind>;
}
pub trait AsOrderedCollectionPage: markers::CollectionPage {
fn ordered_collection_page_ref(&self) -> &OrderedCollectionPage;
fn ordered_collection_page_mut(&mut self) -> &mut OrderedCollectionPage;
}
pub trait CollectionExt<Kind>: AsCollection<Kind> {
fn items<'a>(&'a self) -> &'a OneOrMany<AnyBase>
where
Kind: 'a,
{
&self.collection_ref().items
}
fn set_items<T>(&mut self, item: T) -> &mut Self
where
T: Into<AnyBase>,
{
self.collection_mut().items = item.into().into();
self
}
fn set_many_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().items = v.into();
self
}
fn add_item<T>(&mut self, item: T) -> &mut Self
where
T: Into<AnyBase>,
{
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<T>(&mut self, total_items: T) -> &mut Self
where
T: Into<XsdNonNegativeInteger>,
{
self.collection_mut().total_items = Some(total_items.into());
self
}
fn take_total_items(&mut self) -> Option<XsdNonNegativeInteger> {
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<T>(&mut self, current: T) -> &mut Self
where
T: Into<AnyBase>,
{
self.collection_mut().current = Some(current.into());
self
}
fn take_current(&mut self) -> Option<AnyBase> {
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<T>(&mut self, first: T) -> &mut Self
where
T: Into<AnyBase>,
{
self.collection_mut().first = Some(first.into());
self
}
fn take_first(&mut self) -> Option<AnyBase> {
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<T>(&mut self, last: T) -> &mut Self
where
T: Into<AnyBase>,
{
self.collection_mut().last = Some(last.into());
self
}
fn take_last(&mut self) -> Option<AnyBase> {
self.collection_mut().last.take()
}
fn delete_last(&mut self) -> &mut Self {
self.collection_mut().last = None;
self
}
}
pub trait CollectionPageExt<Kind>: AsCollectionPage<Kind> {
fn part_of<'a>(&'a self) -> Option<&'a AnyBase>
where
Kind: 'a,
{
self.collection_page_ref().part_of.as_ref()
}
fn set_part_of<T>(&mut self, part_of: T) -> &mut Self
where
T: Into<AnyBase>,
{
self.collection_page_mut().part_of = Some(part_of.into());
self
}
fn take_part_of(&mut self) -> Option<AnyBase> {
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<T>(&mut self, next: T) -> &mut Self
where
T: Into<AnyBase>,
{
self.collection_page_mut().next = Some(next.into());
self
}
fn take_next(&mut self) -> Option<AnyBase> {
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<T>(&mut self, prev: T) -> &mut Self
where
T: Into<AnyBase>,
{
self.collection_page_mut().prev = Some(prev.into());
self
}
fn take_prev(&mut self) -> Option<AnyBase> {
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<T>(&mut self, start_index: T) -> &mut Self
where
T: Into<XsdNonNegativeInteger>,
{
self.ordered_collection_page_mut().start_index = Some(start_index.into());
self
}
fn take_start_index(&mut self) -> Option<XsdNonNegativeInteger> {
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<OrderedCollectionType>;
pub type UnorderedCollection = Collection<CollectionType>;
pub type UnorderedCollectionPage = CollectionPage<CollectionPageType>;
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, TypedBuilder)]
#[serde(rename_all = "camelCase")]
#[builder(doc)]
pub struct Collection<Kind> {
#[builder(setter(into))]
pub items: OneOrMany<AnyBase>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))]
pub total_items: Option<XsdNonNegativeInteger>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))]
pub current: Option<AnyBase>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))]
pub first: Option<AnyBase>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))]
pub last: Option<AnyBase>,
#[serde(flatten)]
pub inner: Object<Kind>,
}
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, TypedBuilder)]
#[serde(rename_all = "camelCase")]
#[builder(doc)]
pub struct CollectionPage<Kind> {
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))]
pub part_of: Option<AnyBase>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))]
pub next: Option<AnyBase>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))]
pub prev: Option<AnyBase>,
#[serde(flatten)]
pub inner: Collection<Kind>,
}
#[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<XsdNonNegativeInteger>,
#[serde(flatten)]
pub inner: CollectionPage<OrderedCollectionPageType>,
}
impl<Kind> Collection<Kind> {
fn extending(mut inner: Object<Kind>) -> Result<Self, serde_json::Error> {
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<Object<Kind>, 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<Kind> CollectionPage<Kind> {
fn extending(object: Object<Kind>) -> Result<Self, serde_json::Error> {
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<Object<Kind>, 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<OrderedCollectionPageType>) -> Result<Self, serde_json::Error> {
let mut inner = CollectionPage::extending(object)?;
let start_index = inner.remove("startIndex")?;
Ok(OrderedCollectionPage { start_index, inner })
}
fn retracting(self) -> Result<Object<OrderedCollectionPageType>, serde_json::Error> {
let OrderedCollectionPage {
start_index,
mut inner,
} = self;
inner.insert("startIndex", start_index)?;
inner.retracting()
}
}
impl<Kind> markers::Base for Collection<Kind> {}
impl<Kind> markers::Object for Collection<Kind> {}
impl<Kind> markers::Collection for Collection<Kind> {}
impl<Kind> markers::Base for CollectionPage<Kind> {}
impl<Kind> markers::Object for CollectionPage<Kind> {}
impl<Kind> markers::Collection for CollectionPage<Kind> {}
impl<Kind> markers::CollectionPage for CollectionPage<Kind> {}
impl markers::Base for OrderedCollectionPage {}
impl markers::Object for OrderedCollectionPage {}
impl markers::Collection for OrderedCollectionPage {}
impl markers::CollectionPage for OrderedCollectionPage {}
impl<Kind> Extends<Kind> for Collection<Kind>
where
Kind: serde::de::DeserializeOwned + serde::ser::Serialize,
{
type Error = serde_json::Error;
fn extends(base: Base<Kind>) -> Result<Self, Self::Error> {
let inner = Object::extends(base)?;
Self::extending(inner)
}
fn retracts(self) -> Result<Base<Kind>, Self::Error> {
let inner = self.retracting()?;
inner.retracts()
}
}
impl<Kind> TryFrom<Collection<Kind>> for Object<Kind> {
type Error = serde_json::Error;
fn try_from(collection: Collection<Kind>) -> Result<Self, Self::Error> {
collection.retracting()
}
}
impl<Kind> TryFrom<Object<Kind>> for Collection<Kind> {
type Error = serde_json::Error;
fn try_from(object: Object<Kind>) -> Result<Self, Self::Error> {
Self::extending(object)
}
}
impl<Kind> Extends<Kind> for CollectionPage<Kind>
where
Kind: serde::de::DeserializeOwned + serde::ser::Serialize,
{
type Error = serde_json::Error;
fn extends(base: Base<Kind>) -> Result<Self, Self::Error> {
let inner = Object::extends(base)?;
Self::extending(inner)
}
fn retracts(self) -> Result<Base<Kind>, Self::Error> {
let inner = self.retracting()?;
inner.retracts()
}
}
impl<Kind> TryFrom<Object<Kind>> for CollectionPage<Kind> {
type Error = serde_json::Error;
fn try_from(object: Object<Kind>) -> Result<Self, Self::Error> {
Self::extending(object)
}
}
impl<Kind> TryFrom<CollectionPage<Kind>> for Object<Kind> {
type Error = serde_json::Error;
fn try_from(collection_page: CollectionPage<Kind>) -> Result<Self, Self::Error> {
collection_page.retracting()
}
}
impl Extends<OrderedCollectionPageType> for OrderedCollectionPage {
type Error = serde_json::Error;
fn extends(base: Base<OrderedCollectionPageType>) -> Result<Self, Self::Error> {
let inner = Object::extends(base)?;
Self::extending(inner)
}
fn retracts(self) -> Result<Base<OrderedCollectionPageType>, Self::Error> {
let inner = self.retracting()?;
inner.retracts()
}
}
impl TryFrom<Object<OrderedCollectionPageType>> for OrderedCollectionPage {
type Error = serde_json::Error;
fn try_from(object: Object<OrderedCollectionPageType>) -> Result<Self, Self::Error> {
Self::extending(object)
}
}
impl TryFrom<OrderedCollectionPage> for Object<OrderedCollectionPageType> {
type Error = serde_json::Error;
fn try_from(collection_page: OrderedCollectionPage) -> Result<Self, Self::Error> {
collection_page.retracting()
}
}
impl<Kind> UnparsedMut for Collection<Kind> {
fn unparsed_mut(&mut self) -> &mut Unparsed {
self.inner.unparsed_mut()
}
}
impl<Kind> UnparsedMut for CollectionPage<Kind> {
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<Kind> AsBase<Kind> for Collection<Kind> {
fn base_ref(&self) -> &Base<Kind> {
self.inner.base_ref()
}
fn base_mut(&mut self) -> &mut Base<Kind> {
self.inner.base_mut()
}
}
impl<Kind> AsObject<Kind> for Collection<Kind> {
fn object_ref(&self) -> &Object<Kind> {
&self.inner
}
fn object_mut(&mut self) -> &mut Object<Kind> {
&mut self.inner
}
}
impl<Kind> AsCollection<Kind> for Collection<Kind> {
fn collection_ref(&self) -> &Collection<Kind> {
self
}
fn collection_mut(&mut self) -> &mut Collection<Kind> {
self
}
}
impl<Kind> AsBase<Kind> for CollectionPage<Kind> {
fn base_ref(&self) -> &Base<Kind> {
self.inner.base_ref()
}
fn base_mut(&mut self) -> &mut Base<Kind> {
self.inner.base_mut()
}
}
impl<Kind> AsObject<Kind> for CollectionPage<Kind> {
fn object_ref(&self) -> &Object<Kind> {
self.inner.object_ref()
}
fn object_mut(&mut self) -> &mut Object<Kind> {
self.inner.object_mut()
}
}
impl<Kind> AsCollection<Kind> for CollectionPage<Kind> {
fn collection_ref(&self) -> &Collection<Kind> {
&self.inner
}
fn collection_mut(&mut self) -> &mut Collection<Kind> {
&mut self.inner
}
}
impl<Kind> AsCollectionPage<Kind> for CollectionPage<Kind> {
fn collection_page_ref(&self) -> &CollectionPage<Kind> {
self
}
fn collection_page_mut(&mut self) -> &mut CollectionPage<Kind> {
self
}
}
impl AsBase<OrderedCollectionPageType> for OrderedCollectionPage {
fn base_ref(&self) -> &Base<OrderedCollectionPageType> {
self.inner.base_ref()
}
fn base_mut(&mut self) -> &mut Base<OrderedCollectionPageType> {
self.inner.base_mut()
}
}
impl AsObject<OrderedCollectionPageType> for OrderedCollectionPage {
fn object_ref(&self) -> &Object<OrderedCollectionPageType> {
self.inner.object_ref()
}
fn object_mut(&mut self) -> &mut Object<OrderedCollectionPageType> {
self.inner.object_mut()
}
}
impl AsCollection<OrderedCollectionPageType> for OrderedCollectionPage {
fn collection_ref(&self) -> &Collection<OrderedCollectionPageType> {
self.inner.collection_ref()
}
fn collection_mut(&mut self) -> &mut Collection<OrderedCollectionPageType> {
self.inner.collection_mut()
}
}
impl AsCollectionPage<OrderedCollectionPageType> for OrderedCollectionPage {
fn collection_page_ref(&self) -> &CollectionPage<OrderedCollectionPageType> {
&self.inner
}
fn collection_page_mut(&mut self) -> &mut CollectionPage<OrderedCollectionPageType> {
&mut self.inner
}
}
impl<Inner> markers::Collection for ApObject<Inner> where Inner: markers::Collection {}
impl<Inner> markers::CollectionPage for ApObject<Inner> where Inner: markers::CollectionPage {}
impl<Inner, Kind> AsCollection<Kind> for ApObject<Inner>
where
Inner: AsCollection<Kind>,
{
fn collection_ref(&self) -> &Collection<Kind> {
self.inner.collection_ref()
}
fn collection_mut(&mut self) -> &mut Collection<Kind> {
self.inner.collection_mut()
}
}
impl<Inner, Kind> AsCollectionPage<Kind> for ApObject<Inner>
where
Inner: AsCollectionPage<Kind>,
{
fn collection_page_ref(&self) -> &CollectionPage<Kind> {
self.inner.collection_page_ref()
}
fn collection_page_mut(&mut self) -> &mut CollectionPage<Kind> {
self.inner.collection_page_mut()
}
}
impl<Inner> AsOrderedCollectionPage for ApObject<Inner>
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<T, Kind> CollectionExt<Kind> for T where T: AsCollection<Kind> {}
impl<T, Kind> CollectionPageExt<Kind> for T where T: AsCollectionPage<Kind> {}
impl<T> OrderedCollectionPageExt for T where T: AsOrderedCollectionPage {}