activitystreams-new/src/collection.rs

296 lines
8.3 KiB
Rust
Raw Normal View History

2020-05-14 03:54:50 +00:00
use crate::{
object::{AnyObject, Object},
primitives::{OneOrMany, Unparsed},
traits::{self, Extends, WithUnparsed, WithUnparsedExt},
};
use activitystreams::primitives::XsdNonNegativeInteger;
use std::convert::TryFrom;
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub enum OrderedCollectionType {
OrderedCollection,
}
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub enum UnorderedCollectionType {
UnorderedCollection,
}
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub enum OrderedCollectionPageType {
OrderedCollectionPage,
}
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub enum UnorderedCollectionPageType {
UnorderedCollectionPage,
}
pub type OrderedCollection = Collection<OrderedCollectionType>;
pub type UnorderedCollection = Collection<UnorderedCollectionType>;
pub type UnorderedCollectionPage = CollectionPage<UnorderedCollectionPageType>;
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Collection<Kind> {
pub items: OneOrMany<AnyObject>,
#[serde(skip_serializing_if = "Option::is_none")]
pub total_items: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub current: Option<Object<serde_json::Value>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub first: Option<Object<serde_json::Value>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub last: Option<Object<serde_json::Value>>,
#[serde(flatten)]
pub inner: Object<Kind>,
}
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CollectionPage<Kind> {
#[serde(skip_serializing_if = "Option::is_none")]
pub part_of: Option<AnyObject>,
#[serde(skip_serializing_if = "Option::is_none")]
pub next: Option<AnyObject>,
#[serde(skip_serializing_if = "Option::is_none")]
pub prev: Option<AnyObject>,
#[serde(flatten)]
pub inner: Collection<Kind>,
}
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OrderedCollectionPage {
#[serde(skip_serializing_if = "Option::is_none")]
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> traits::Base for Collection<Kind> where Kind: std::fmt::Debug {}
impl<Kind> traits::Object for Collection<Kind> where Kind: std::fmt::Debug {}
impl<Kind> traits::Collection for Collection<Kind> where Kind: std::fmt::Debug {}
impl<Kind> traits::Base for CollectionPage<Kind> where Kind: std::fmt::Debug {}
impl<Kind> traits::Object for CollectionPage<Kind> where Kind: std::fmt::Debug {}
impl<Kind> traits::Collection for CollectionPage<Kind> where Kind: std::fmt::Debug {}
impl<Kind> traits::CollectionPage for CollectionPage<Kind> where Kind: std::fmt::Debug {}
impl traits::Base for OrderedCollectionPage {}
impl traits::Object for OrderedCollectionPage {}
impl traits::Collection for OrderedCollectionPage {}
impl traits::CollectionPage for OrderedCollectionPage {}
impl Extends<Object<UnorderedCollectionType>> for UnorderedCollection {
type Error = serde_json::Error;
fn extends(object: Object<UnorderedCollectionType>) -> Result<Self, Self::Error> {
Self::extending(object)
}
fn retracts(self) -> Result<Object<UnorderedCollectionType>, Self::Error> {
self.retracting()
}
}
impl TryFrom<Object<UnorderedCollectionType>> for UnorderedCollection {
type Error = serde_json::Error;
fn try_from(object: Object<UnorderedCollectionType>) -> Result<Self, Self::Error> {
Self::extending(object)
}
}
impl Extends<Object<OrderedCollectionType>> for OrderedCollection {
type Error = serde_json::Error;
fn extends(object: Object<OrderedCollectionType>) -> Result<Self, Self::Error> {
Self::extending(object)
}
fn retracts(self) -> Result<Object<OrderedCollectionType>, Self::Error> {
self.retracting()
}
}
impl TryFrom<Object<OrderedCollectionType>> for OrderedCollection {
type Error = serde_json::Error;
fn try_from(object: Object<OrderedCollectionType>) -> Result<Self, Self::Error> {
Self::extending(object)
}
}
impl Extends<Object<UnorderedCollectionPageType>> for UnorderedCollectionPage {
type Error = serde_json::Error;
fn extends(object: Object<UnorderedCollectionPageType>) -> Result<Self, Self::Error> {
Self::extending(object)
}
fn retracts(self) -> Result<Object<UnorderedCollectionPageType>, Self::Error> {
self.retracting()
}
}
impl TryFrom<Object<UnorderedCollectionPageType>> for UnorderedCollectionPage {
type Error = serde_json::Error;
fn try_from(object: Object<UnorderedCollectionPageType>) -> Result<Self, Self::Error> {
Self::extending(object)
}
}
impl Extends<Object<OrderedCollectionPageType>> for OrderedCollectionPage {
type Error = serde_json::Error;
fn extends(object: Object<OrderedCollectionPageType>) -> Result<Self, Self::Error> {
Self::extending(object)
}
fn retracts(self) -> Result<Object<OrderedCollectionPageType>, Self::Error> {
self.retracting()
}
}
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<Kind> WithUnparsed for Collection<Kind> {
fn unparsed(&self) -> &Unparsed {
self.inner.unparsed()
}
fn unparsed_mut(&mut self) -> &mut Unparsed {
self.inner.unparsed_mut()
}
}
impl<Kind> WithUnparsed for CollectionPage<Kind> {
fn unparsed(&self) -> &Unparsed {
self.inner.unparsed()
}
fn unparsed_mut(&mut self) -> &mut Unparsed {
self.inner.unparsed_mut()
}
}
impl WithUnparsed for OrderedCollectionPage {
fn unparsed(&self) -> &Unparsed {
self.inner.unparsed()
}
fn unparsed_mut(&mut self) -> &mut Unparsed {
self.inner.unparsed_mut()
}
}