Finish trait impls

This commit is contained in:
asonix 2020-05-15 17:01:29 -05:00
parent d19406f60b
commit a86050d234
9 changed files with 2062 additions and 1736 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,7 +1,9 @@
use crate::{ use crate::{
object::{Object, ObjectMut, ObjectRef}, base::{AsBase, Base, Extends},
primitives::{OneOrMany, Unparsed, XsdAnyUri, XsdString}, markers,
traits::{self, Extends, UnparsedMut, UnparsedMutExt}, object::{ApObject, AsApObject, AsObject, Object},
primitives::{OneOrMany, XsdAnyUri, XsdString},
unparsed::{Unparsed, UnparsedMut, UnparsedMutExt},
}; };
use typed_builder::TypedBuilder; use typed_builder::TypedBuilder;
@ -11,15 +13,12 @@ pub mod kind {
use self::kind::*; use self::kind::*;
pub trait ApActorRef<Inner>: traits::Actor { pub trait AsApActor<Inner>: markers::Actor {
fn ap_actor_ref(&self) -> &ApActor<Inner>; fn ap_actor_ref(&self) -> &ApActor<Inner>;
}
pub trait ApActorMut<Inner>: traits::Actor {
fn ap_actor_mut(&mut self) -> &mut ApActor<Inner>; fn ap_actor_mut(&mut self) -> &mut ApActor<Inner>;
} }
pub trait ApActorRefExt<Inner>: ApActorRef<Inner> { pub trait ApActorExt<Inner>: AsApActor<Inner> {
fn inbox<'a>(&'a self) -> &'a XsdAnyUri fn inbox<'a>(&'a self) -> &'a XsdAnyUri
where where
Inner: 'a, Inner: 'a,
@ -27,6 +26,11 @@ pub trait ApActorRefExt<Inner>: ApActorRef<Inner> {
&self.ap_actor_ref().inbox &self.ap_actor_ref().inbox
} }
fn set_inbox(&mut self, inbox: XsdAnyUri) -> &mut Self {
self.ap_actor_mut().inbox = inbox;
self
}
fn outbox<'a>(&'a self) -> &'a XsdAnyUri fn outbox<'a>(&'a self) -> &'a XsdAnyUri
where where
Inner: 'a, Inner: 'a,
@ -34,6 +38,11 @@ pub trait ApActorRefExt<Inner>: ApActorRef<Inner> {
&self.ap_actor_ref().outbox &self.ap_actor_ref().outbox
} }
fn set_outbox(&mut self, outbox: XsdAnyUri) -> &mut Self {
self.ap_actor_mut().outbox = outbox;
self
}
fn following<'a>(&'a self) -> Option<&'a XsdAnyUri> fn following<'a>(&'a self) -> Option<&'a XsdAnyUri>
where where
Inner: 'a, Inner: 'a,
@ -41,53 +50,6 @@ pub trait ApActorRefExt<Inner>: ApActorRef<Inner> {
self.ap_actor_ref().following.as_ref() self.ap_actor_ref().following.as_ref()
} }
fn followers<'a>(&'a self) -> Option<&'a XsdAnyUri>
where
Inner: 'a,
{
self.ap_actor_ref().followers.as_ref()
}
fn liked<'a>(&'a self) -> Option<&'a XsdAnyUri>
where
Inner: 'a,
{
self.ap_actor_ref().liked.as_ref()
}
fn streams<'a>(&'a self) -> Option<&'a OneOrMany<XsdAnyUri>>
where
Inner: 'a,
{
self.ap_actor_ref().streams.as_ref()
}
fn preferred_username<'a>(&'a self) -> Option<&'a XsdString>
where
Inner: 'a,
{
self.ap_actor_ref().preferred_username.as_ref()
}
fn endpoints<'a>(&'a self) -> Option<&'a Endpoints>
where
Inner: 'a,
{
self.ap_actor_ref().endpoints.as_ref()
}
}
pub trait ApActorMutExt<Inner>: ApActorMut<Inner> {
fn set_inbox(&mut self, inbox: XsdAnyUri) -> &mut Self {
self.ap_actor_mut().inbox = inbox;
self
}
fn set_outbox(&mut self, outbox: XsdAnyUri) -> &mut Self {
self.ap_actor_mut().outbox = outbox;
self
}
fn set_following(&mut self, following: XsdAnyUri) -> &mut Self { fn set_following(&mut self, following: XsdAnyUri) -> &mut Self {
self.ap_actor_mut().following = Some(following); self.ap_actor_mut().following = Some(following);
self self
@ -102,6 +64,13 @@ pub trait ApActorMutExt<Inner>: ApActorMut<Inner> {
self self
} }
fn followers<'a>(&'a self) -> Option<&'a XsdAnyUri>
where
Inner: 'a,
{
self.ap_actor_ref().followers.as_ref()
}
fn set_followers(&mut self, followers: XsdAnyUri) -> &mut Self { fn set_followers(&mut self, followers: XsdAnyUri) -> &mut Self {
self.ap_actor_mut().followers = Some(followers); self.ap_actor_mut().followers = Some(followers);
self self
@ -116,6 +85,13 @@ pub trait ApActorMutExt<Inner>: ApActorMut<Inner> {
self self
} }
fn liked<'a>(&'a self) -> Option<&'a XsdAnyUri>
where
Inner: 'a,
{
self.ap_actor_ref().liked.as_ref()
}
fn set_liked(&mut self, liked: XsdAnyUri) -> &mut Self { fn set_liked(&mut self, liked: XsdAnyUri) -> &mut Self {
self.ap_actor_mut().liked = Some(liked); self.ap_actor_mut().liked = Some(liked);
self self
@ -125,11 +101,18 @@ pub trait ApActorMutExt<Inner>: ApActorMut<Inner> {
self.ap_actor_mut().liked.take() self.ap_actor_mut().liked.take()
} }
fn delete_likes(&mut self) -> &mut Self { fn delete_liked(&mut self) -> &mut Self {
self.ap_actor_mut().liked = None; self.ap_actor_mut().liked = None;
self self
} }
fn streams<'a>(&'a self) -> Option<&'a OneOrMany<XsdAnyUri>>
where
Inner: 'a,
{
self.ap_actor_ref().streams.as_ref()
}
fn set_streams(&mut self, streams: XsdAnyUri) -> &mut Self { fn set_streams(&mut self, streams: XsdAnyUri) -> &mut Self {
self.ap_actor_mut().streams = Some(streams.into()); self.ap_actor_mut().streams = Some(streams.into());
self self
@ -165,6 +148,13 @@ pub trait ApActorMutExt<Inner>: ApActorMut<Inner> {
self self
} }
fn preferred_username<'a>(&'a self) -> Option<&'a XsdString>
where
Inner: 'a,
{
self.ap_actor_ref().preferred_username.as_ref()
}
fn set_preferred_username(&mut self, string: XsdString) -> &mut Self { fn set_preferred_username(&mut self, string: XsdString) -> &mut Self {
self.ap_actor_mut().preferred_username = Some(string); self.ap_actor_mut().preferred_username = Some(string);
self self
@ -179,6 +169,13 @@ pub trait ApActorMutExt<Inner>: ApActorMut<Inner> {
self self
} }
fn endpoints<'a>(&'a self) -> Option<&'a Endpoints>
where
Inner: 'a,
{
self.ap_actor_ref().endpoints.as_ref()
}
fn set_endpoints(&mut self, endpoints: Endpoints) -> &mut Self { fn set_endpoints(&mut self, endpoints: Endpoints) -> &mut Self {
self.ap_actor_mut().endpoints = Some(endpoints); self.ap_actor_mut().endpoints = Some(endpoints);
self self
@ -266,7 +263,7 @@ pub struct Endpoints {
impl<Inner> ApActor<Inner> { impl<Inner> ApActor<Inner> {
fn extending(mut inner: Inner) -> Result<Self, serde_json::Error> fn extending(mut inner: Inner) -> Result<Self, serde_json::Error>
where where
Inner: UnparsedMut + traits::Actor, Inner: UnparsedMut + markers::Actor,
{ {
let inbox = inner.remove("inbox")?; let inbox = inner.remove("inbox")?;
let outbox = inner.remove("outbox")?; let outbox = inner.remove("outbox")?;
@ -292,7 +289,7 @@ impl<Inner> ApActor<Inner> {
fn retracting(self) -> Result<Inner, serde_json::Error> fn retracting(self) -> Result<Inner, serde_json::Error>
where where
Inner: UnparsedMut + traits::Actor, Inner: UnparsedMut + markers::Actor,
{ {
let ApActor { let ApActor {
inbox, inbox,
@ -320,28 +317,31 @@ impl<Inner> ApActor<Inner> {
} }
} }
impl traits::Actor for Application {} impl markers::Actor for Application {}
impl traits::Actor for Group {} impl markers::Actor for Group {}
impl traits::Actor for Organization {} impl markers::Actor for Organization {}
impl traits::Actor for Person {} impl markers::Actor for Person {}
impl traits::Actor for Service {} impl markers::Actor for Service {}
impl<Inner> traits::Base for ApActor<Inner> where Inner: traits::Base {} impl<Inner> markers::Base for ApActor<Inner> where Inner: markers::Base {}
impl<Inner> traits::Object for ApActor<Inner> where Inner: traits::Object {} impl<Inner> markers::Object for ApActor<Inner> where Inner: markers::Object {}
impl<Inner> traits::Actor for ApActor<Inner> where Inner: traits::Actor {} impl<Inner> markers::Actor for ApActor<Inner> where Inner: markers::Actor {}
impl<Inner> Extends<Inner> for ApActor<Inner> impl<Inner, Kind> Extends<Kind> for ApActor<Inner>
where where
Inner: UnparsedMut + traits::Actor, Inner: Extends<Kind, Error = serde_json::Error> + UnparsedMut + markers::Actor,
Kind: serde::de::DeserializeOwned + serde::ser::Serialize,
{ {
type Error = serde_json::Error; type Error = serde_json::Error;
fn extends(inner: Inner) -> Result<Self, Self::Error> { fn extends(base: Base<Kind>) -> Result<Self, Self::Error> {
let inner = Inner::extends(base)?;
Self::extending(inner) Self::extending(inner)
} }
fn retracts(self) -> Result<Inner, Self::Error> { fn retracts(self) -> Result<Base<Kind>, Self::Error> {
self.retracting() let inner = self.retracting()?;
inner.retracts()
} }
} }
@ -354,39 +354,56 @@ where
} }
} }
impl<Inner, Kind> ObjectRef<Kind> for ApActor<Inner> impl<Inner, Kind> AsBase<Kind> for ApActor<Inner>
where where
Inner: ObjectRef<Kind>, Inner: AsBase<Kind>,
{
fn base_ref(&self) -> &Base<Kind> {
self.inner.base_ref()
}
fn base_mut(&mut self) -> &mut Base<Kind> {
self.inner.base_mut()
}
}
impl<Inner, Kind> AsObject<Kind> for ApActor<Inner>
where
Inner: AsObject<Kind>,
{ {
fn object_ref(&self) -> &Object<Kind> { fn object_ref(&self) -> &Object<Kind> {
self.inner.object_ref() self.inner.object_ref()
} }
}
impl<Inner, Kind> ObjectMut<Kind> for ApActor<Inner>
where
Inner: ObjectMut<Kind>,
{
fn object_mut(&mut self) -> &mut Object<Kind> { fn object_mut(&mut self) -> &mut Object<Kind> {
self.inner.object_mut() self.inner.object_mut()
} }
} }
impl<Inner> ApActorRef<Inner> for ApActor<Inner> impl<Inner> AsApActor<Inner> for ApActor<Inner>
where where
Inner: traits::Actor, Inner: markers::Actor,
{ {
fn ap_actor_ref(&self) -> &ApActor<Inner> { fn ap_actor_ref(&self) -> &ApActor<Inner> {
self self
} }
}
impl<Inner> ApActorMut<Inner> for ApActor<Inner>
where
Inner: traits::Actor,
{
fn ap_actor_mut(&mut self) -> &mut ApActor<Inner> { fn ap_actor_mut(&mut self) -> &mut ApActor<Inner> {
self self
} }
} }
impl<T, Inner> ApActorRefExt<Inner> for T where T: ApActorRef<Inner> {} impl<Inner1, Inner2> AsApObject<Inner2> for ApActor<Inner1>
impl<T, Inner> ApActorMutExt<Inner> for T where T: ApActorMut<Inner> {} where
Inner1: AsApObject<Inner2>,
{
fn ap_object_ref(&self) -> &ApObject<Inner2> {
self.inner.ap_object_ref()
}
fn ap_object_mut(&mut self) -> &mut ApObject<Inner2> {
self.inner.ap_object_mut()
}
}
impl<T, Inner> ApActorExt<Inner> for T where T: AsApActor<Inner> {}

534
src/base.rs Normal file
View file

@ -0,0 +1,534 @@
use crate::{
either::Either,
markers,
primitives::{AnyString, MimeMediaType, OneOrMany, XsdAnyUri, XsdString},
unparsed::{Unparsed, UnparsedMut},
};
use typed_builder::TypedBuilder;
pub trait Extends<Kind>: Sized {
type Error: std::error::Error;
fn extends(base: Base<Kind>) -> Result<Self, Self::Error>;
fn retracts(self) -> Result<Base<Kind>, Self::Error>;
}
pub trait AsBase<Kind>: markers::Base {
fn base_ref(&self) -> &Base<Kind>;
fn base_mut(&mut self) -> &mut Base<Kind>;
}
pub trait BaseExt<Kind>: AsBase<Kind> {
fn context<'a>(&'a self) -> Option<&'a OneOrMany<AnyBase>>
where
Kind: 'a,
{
self.base_ref().context.as_ref()
}
fn set_context<T>(&mut self, context: T) -> &mut Self
where
T: Into<AnyBase>,
{
self.base_mut().context = Some(context.into().into());
self
}
fn set_many_contexts<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.base_mut().context = Some(v.into());
self
}
fn add_context<T>(&mut self, context: T) -> &mut Self
where
T: Into<AnyBase>,
{
let c = match self.base_mut().context.take() {
Some(mut c) => {
c.add(context.into());
c
}
None => vec![context.into()].into(),
};
self.base_mut().context = Some(c);
self
}
fn take_context(&mut self) -> Option<OneOrMany<AnyBase>> {
self.base_mut().context.take()
}
fn delete_context(&mut self) -> &mut Self {
self.base_mut().context = None;
self
}
fn id<'a>(&'a self) -> Option<&'a XsdAnyUri>
where
Kind: 'a,
{
self.base_ref().id.as_ref()
}
fn set_id(&mut self, id: XsdAnyUri) -> &mut Self {
self.base_mut().id = Some(id);
self
}
fn take_id(&mut self) -> Option<XsdAnyUri> {
self.base_mut().id.take()
}
fn delete_id(&mut self) -> &mut Self {
self.base_mut().id = None;
self
}
fn kind<'a>(&'a self) -> Option<&'a Kind>
where
Kind: 'a,
{
self.base_ref().kind.as_ref()
}
fn set_kind(&mut self, kind: Kind) -> &mut Self {
self.base_mut().kind = Some(kind);
self
}
fn take_kind(&mut self) -> Option<Kind> {
self.base_mut().kind.take()
}
fn delete_kind(&mut self) -> &mut Self {
self.base_mut().kind = None;
self
}
fn name<'a>(&'a self) -> Option<&'a OneOrMany<AnyString>>
where
Kind: 'a,
{
self.base_ref().name.as_ref()
}
fn set_name<T>(&mut self, name: T) -> &mut Self
where
T: Into<AnyString>,
{
self.base_mut().name = Some(name.into().into());
self
}
fn set_many_names<I, T>(&mut self, items: I) -> &mut Self
where
I: IntoIterator<Item = T>,
T: Into<AnyString>,
{
let v: Vec<_> = items.into_iter().map(Into::into).collect();
self.base_mut().name = Some(v.into());
self
}
fn add_name<T>(&mut self, name: T) -> &mut Self
where
T: Into<AnyString>,
{
let a = match self.base_mut().name.take() {
Some(mut a) => {
a.add(name.into());
a
}
None => vec![name.into()].into(),
};
self.base_mut().name = Some(a);
self
}
fn take_name(&mut self) -> Option<OneOrMany<AnyString>> {
self.base_mut().name.take()
}
fn delete_name(&mut self) -> &mut Self {
self.base_mut().name = None;
self
}
fn media_type<'a>(&'a self) -> Option<&'a MimeMediaType>
where
Kind: 'a,
{
self.base_ref().media_type.as_ref()
}
fn set_media_type(&mut self, media_type: MimeMediaType) -> &mut Self {
self.base_mut().media_type = Some(media_type);
self
}
fn take_media_type(&mut self) -> Option<MimeMediaType> {
self.base_mut().media_type.take()
}
fn delete_media_type(&mut self) -> &mut Self {
self.base_mut().media_type = None;
self
}
fn preview<'a>(&'a self) -> Option<&'a OneOrMany<AnyBase>>
where
Kind: 'a,
{
self.base_ref().preview.as_ref()
}
fn set_preview<T>(&mut self, preview: T) -> &mut Self
where
T: Into<AnyBase>,
{
self.base_mut().preview = Some(preview.into().into());
self
}
fn set_many_previews<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.base_mut().preview = Some(v.into());
self
}
fn add_preview<T>(&mut self, preview: T) -> &mut Self
where
T: Into<AnyBase>,
{
let a = match self.base_mut().preview.take() {
Some(mut a) => {
a.add(preview.into());
a
}
None => vec![preview.into()].into(),
};
self.base_mut().preview = Some(a);
self
}
fn take_preview(&mut self) -> Option<OneOrMany<AnyBase>> {
self.base_mut().preview.take()
}
fn delete_preview(&mut self) -> &mut Self {
self.base_mut().preview = None;
self
}
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(transparent)]
struct IdOrBase(Either<XsdAnyUri, Box<Base<serde_json::Value>>>);
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(transparent)]
pub struct AnyBase(Either<IdOrBase, XsdString>);
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize, TypedBuilder)]
#[serde(rename_all = "camelCase")]
#[builder(doc)]
pub struct Base<Kind> {
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))]
pub context: Option<OneOrMany<AnyBase>>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))]
pub id: Option<XsdAnyUri>,
#[serde(rename = "type")]
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option))]
pub kind: Option<Kind>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))]
pub name: Option<OneOrMany<AnyString>>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option))]
pub media_type: Option<MimeMediaType>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))]
pub preview: Option<OneOrMany<AnyBase>>,
#[serde(flatten)]
#[builder(default)]
pub unparsed: Unparsed,
}
impl Base<serde_json::Value> {
pub fn solidify<Kind>(self) -> Result<Base<Kind>, serde_json::Error>
where
Kind: serde::de::DeserializeOwned,
{
self.try_map_kind(serde_json::from_value)
}
}
impl<Kind> Base<Kind> {
pub fn extend<T>(self) -> Result<T, T::Error>
where
T: Extends<Kind>,
{
T::extends(self)
}
pub fn retract<T>(t: T) -> Result<Self, T::Error>
where
T: Extends<Kind>,
{
t.retracts()
}
pub fn into_generic(self) -> Result<Base<serde_json::Value>, serde_json::Error>
where
Kind: serde::ser::Serialize,
{
self.try_map_kind(serde_json::to_value)
}
pub fn map_kind<NewKind>(self, f: impl Fn(Kind) -> NewKind) -> Base<NewKind> {
Base {
kind: self.kind.map(f),
context: self.context,
id: self.id,
name: self.name,
media_type: self.media_type,
preview: self.preview,
unparsed: self.unparsed,
}
}
pub fn try_map_kind<NewKind, E>(
self,
f: impl Fn(Kind) -> Result<NewKind, E>,
) -> Result<Base<NewKind>, E> {
Ok(Base {
kind: if let Some(kind) = self.kind {
Some((f)(kind)?)
} else {
None
},
context: self.context,
id: self.id,
name: self.name,
media_type: self.media_type,
preview: self.preview,
unparsed: self.unparsed,
})
}
}
impl AnyBase {
pub fn as_xsd_any_uri(&self) -> Option<&XsdAnyUri> {
self.0.as_ref().left().and_then(|l| l.as_xsd_any_uri())
}
pub fn as_xsd_string(&self) -> Option<&XsdString> {
self.0.as_ref().right()
}
pub fn as_base(&self) -> Option<&Base<serde_json::Value>> {
self.0.as_ref().left().and_then(|l| l.as_base())
}
pub fn id(self) -> Option<XsdAnyUri> {
self.0.left().and_then(|l| l.id())
}
pub fn xsd_string(self) -> Option<XsdString> {
self.0.right()
}
pub fn base(self) -> Option<Base<serde_json::Value>> {
self.0.left().and_then(|l| l.base())
}
pub fn set_xsd_any_uri(&mut self, id: XsdAnyUri) {
self.0 = Either::Left(IdOrBase::from_xsd_any_uri(id));
}
pub fn set_base(&mut self, base: Base<serde_json::Value>) {
self.0 = Either::Left(IdOrBase::from_base(base));
}
pub fn set_xsd_string(&mut self, xsd_string: XsdString) {
self.0 = Either::Right(xsd_string);
}
pub fn from_xsd_any_uri(id: XsdAnyUri) -> Self {
AnyBase(Either::Left(IdOrBase::from_xsd_any_uri(id)))
}
pub fn from_base(base: Base<serde_json::Value>) -> Self {
AnyBase(Either::Left(IdOrBase::from_base(base)))
}
pub fn from_xsd_string(xsd_string: XsdString) -> Self {
AnyBase(Either::Right(xsd_string))
}
}
impl IdOrBase {
fn as_xsd_any_uri(&self) -> Option<&XsdAnyUri> {
self.0.as_ref().left()
}
fn as_base(&self) -> Option<&Base<serde_json::Value>> {
self.0.as_ref().right().map(|b| b.as_ref())
}
fn id(self) -> Option<XsdAnyUri> {
self.0.left()
}
fn base(self) -> Option<Base<serde_json::Value>> {
self.0.right().map(|b| *b)
}
fn from_xsd_any_uri(id: XsdAnyUri) -> Self {
IdOrBase(Either::Left(id))
}
fn from_base(base: Base<serde_json::Value>) -> Self {
IdOrBase(Either::Right(Box::new(base)))
}
}
impl OneOrMany<AnyBase> {
pub fn as_single_xsd_any_uri(&self) -> Option<&XsdAnyUri> {
self.as_one().and_then(|inner| inner.as_xsd_any_uri())
}
pub fn as_single_xsd_string(&self) -> Option<&XsdString> {
self.as_one().and_then(|inner| inner.as_xsd_string())
}
pub fn as_single_base(&self) -> Option<&Base<serde_json::Value>> {
self.as_one().and_then(|inner| inner.as_base())
}
pub fn single_xsd_any_uri(self) -> Option<XsdAnyUri> {
self.one().and_then(|inner| inner.id())
}
pub fn single_xsd_string(self) -> Option<XsdString> {
self.one().and_then(|inner| inner.xsd_string())
}
pub fn single_base(self) -> Option<Base<serde_json::Value>> {
self.one().and_then(|inner| inner.base())
}
pub fn from_xsd_any_uri(id: XsdAnyUri) -> Self {
OneOrMany(Either::Left(AnyBase::from_xsd_any_uri(id)))
}
pub fn from_xsd_string(xsd_string: XsdString) -> Self {
OneOrMany(Either::Left(AnyBase::from_xsd_string(xsd_string)))
}
pub fn from_base(base: Base<serde_json::Value>) -> Self {
OneOrMany(Either::Left(AnyBase::from_base(base)))
}
pub fn set_single_xsd_any_uri(&mut self, id: XsdAnyUri) -> &mut Self {
self.0 = Either::Left(AnyBase::from_xsd_any_uri(id));
self
}
pub fn set_single_xsd_string(&mut self, xsd_string: XsdString) -> &mut Self {
self.0 = Either::Left(AnyBase::from_xsd_string(xsd_string));
self
}
pub fn set_single_base(&mut self, base: Base<serde_json::Value>) -> &mut Self {
self.0 = Either::Left(AnyBase::from_base(base));
self
}
pub fn add_xsd_any_uri(&mut self, id: XsdAnyUri) -> &mut Self {
self.add(AnyBase::from_xsd_any_uri(id))
}
pub fn add_xsd_string(&mut self, xsd_string: XsdString) -> &mut Self {
self.add(AnyBase::from_xsd_string(xsd_string))
}
pub fn add_base(&mut self, base: Base<serde_json::Value>) -> &mut Self {
self.add(AnyBase::from_base(base))
}
}
impl<Kind> markers::Base for Base<Kind> {}
impl<Kind> UnparsedMut for Base<Kind> {
fn unparsed_mut(&mut self) -> &mut Unparsed {
&mut self.unparsed
}
}
impl<Kind> AsBase<Kind> for Base<Kind> {
fn base_ref(&self) -> &Base<Kind> {
self
}
fn base_mut(&mut self) -> &mut Base<Kind> {
self
}
}
impl<T, Kind> BaseExt<Kind> for T where T: AsBase<Kind> {}
impl From<Base<serde_json::Value>> for AnyBase {
fn from(o: Base<serde_json::Value>) -> Self {
Self::from_base(o)
}
}
impl From<XsdAnyUri> for AnyBase {
fn from(id: XsdAnyUri) -> Self {
Self::from_xsd_any_uri(id)
}
}
impl From<XsdString> for AnyBase {
fn from(xsd_string: XsdString) -> Self {
Self::from_xsd_string(xsd_string)
}
}
impl From<Base<serde_json::Value>> for OneOrMany<AnyBase> {
fn from(object: Base<serde_json::Value>) -> Self {
Self::from_base(object)
}
}
impl From<XsdAnyUri> for OneOrMany<AnyBase> {
fn from(xsd_any_uri: XsdAnyUri) -> Self {
Self::from_xsd_any_uri(xsd_any_uri)
}
}
impl From<XsdString> for OneOrMany<AnyBase> {
fn from(xsd_string: XsdString) -> Self {
Self::from_xsd_string(xsd_string)
}
}

View file

@ -1,7 +1,9 @@
use crate::{ use crate::{
object::{AnyObject, ApObject, Object, ObjectMut, ObjectRef}, base::{AnyBase, AsBase, Base, Extends},
primitives::{OneOrMany, Unparsed, XsdNonNegativeInteger}, markers,
traits::{self, Extends, UnparsedMut, UnparsedMutExt}, object::{ApObject, AsObject, Object},
primitives::{OneOrMany, XsdNonNegativeInteger},
unparsed::{Unparsed, UnparsedMut, UnparsedMutExt},
}; };
use std::convert::TryFrom; use std::convert::TryFrom;
use typed_builder::TypedBuilder; use typed_builder::TypedBuilder;
@ -12,71 +14,32 @@ pub mod kind {
use self::kind::*; use self::kind::*;
pub trait CollectionRef<Kind>: traits::Collection { pub trait AsCollection<Kind>: markers::Collection {
fn collection_ref(&self) -> &Collection<Kind>; fn collection_ref(&self) -> &Collection<Kind>;
}
pub trait CollectionMut<Kind>: traits::Collection {
fn collection_mut(&mut self) -> &mut Collection<Kind>; fn collection_mut(&mut self) -> &mut Collection<Kind>;
} }
pub trait CollectionPageRef<Kind>: traits::CollectionPage { pub trait AsCollectionPage<Kind>: markers::CollectionPage {
fn collection_page_ref(&self) -> &CollectionPage<Kind>; fn collection_page_ref(&self) -> &CollectionPage<Kind>;
}
pub trait CollectionPageMut<Kind>: traits::CollectionPage {
fn collection_page_mut(&mut self) -> &mut CollectionPage<Kind>; fn collection_page_mut(&mut self) -> &mut CollectionPage<Kind>;
} }
pub trait OrderedCollectionPageRef: traits::CollectionPage { pub trait AsOrderedCollectionPage: markers::CollectionPage {
fn ordered_collection_page_ref(&self) -> &OrderedCollectionPage; fn ordered_collection_page_ref(&self) -> &OrderedCollectionPage;
}
pub trait OrderedCollectionPageMut: traits::CollectionPage {
fn ordered_collection_page_mut(&mut self) -> &mut OrderedCollectionPage; fn ordered_collection_page_mut(&mut self) -> &mut OrderedCollectionPage;
} }
pub trait CollectionRefExt<Kind>: CollectionRef<Kind> { pub trait CollectionExt<Kind>: AsCollection<Kind> {
fn items<'a>(&'a self) -> &'a OneOrMany<AnyObject> fn items<'a>(&'a self) -> &'a OneOrMany<AnyBase>
where where
Kind: 'a, Kind: 'a,
{ {
&self.collection_ref().items &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<Kind>: CollectionMut<Kind> {
fn set_items<T>(&mut self, item: T) -> &mut Self fn set_items<T>(&mut self, item: T) -> &mut Self
where where
T: Into<AnyObject>, T: Into<AnyBase>,
{ {
self.collection_mut().items = item.into().into(); self.collection_mut().items = item.into().into();
self self
@ -85,7 +48,7 @@ pub trait CollectionMutExt<Kind>: CollectionMut<Kind> {
fn set_many_items<I, T>(&mut self, items: I) -> &mut Self fn set_many_items<I, T>(&mut self, items: I) -> &mut Self
where where
I: IntoIterator<Item = T>, I: IntoIterator<Item = T>,
T: Into<AnyObject>, T: Into<AnyBase>,
{ {
let v: Vec<_> = items.into_iter().map(Into::into).collect(); let v: Vec<_> = items.into_iter().map(Into::into).collect();
self.collection_mut().items = v.into(); self.collection_mut().items = v.into();
@ -94,12 +57,19 @@ pub trait CollectionMutExt<Kind>: CollectionMut<Kind> {
fn add_item<T>(&mut self, item: T) -> &mut Self fn add_item<T>(&mut self, item: T) -> &mut Self
where where
T: Into<AnyObject>, T: Into<AnyBase>,
{ {
self.collection_mut().items.add(item.into()); self.collection_mut().items.add(item.into());
self 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 fn set_total_items<T>(&mut self, total_items: T) -> &mut Self
where where
T: Into<XsdNonNegativeInteger>, T: Into<XsdNonNegativeInteger>,
@ -117,15 +87,22 @@ pub trait CollectionMutExt<Kind>: CollectionMut<Kind> {
self 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 fn set_current<T>(&mut self, current: T) -> &mut Self
where where
T: Into<AnyObject>, T: Into<AnyBase>,
{ {
self.collection_mut().current = Some(current.into()); self.collection_mut().current = Some(current.into());
self self
} }
fn take_current(&mut self) -> Option<AnyObject> { fn take_current(&mut self) -> Option<AnyBase> {
self.collection_mut().current.take() self.collection_mut().current.take()
} }
@ -134,15 +111,22 @@ pub trait CollectionMutExt<Kind>: CollectionMut<Kind> {
self 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 fn set_first<T>(&mut self, first: T) -> &mut Self
where where
T: Into<AnyObject>, T: Into<AnyBase>,
{ {
self.collection_mut().first = Some(first.into()); self.collection_mut().first = Some(first.into());
self self
} }
fn take_first(&mut self) -> Option<AnyObject> { fn take_first(&mut self) -> Option<AnyBase> {
self.collection_mut().first.take() self.collection_mut().first.take()
} }
@ -151,15 +135,22 @@ pub trait CollectionMutExt<Kind>: CollectionMut<Kind> {
self 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 fn set_last<T>(&mut self, last: T) -> &mut Self
where where
T: Into<AnyObject>, T: Into<AnyBase>,
{ {
self.collection_mut().last = Some(last.into()); self.collection_mut().last = Some(last.into());
self self
} }
fn take_last(&mut self) -> Option<AnyObject> { fn take_last(&mut self) -> Option<AnyBase> {
self.collection_mut().last.take() self.collection_mut().last.take()
} }
@ -169,39 +160,23 @@ pub trait CollectionMutExt<Kind>: CollectionMut<Kind> {
} }
} }
pub trait CollectionPageRefExt<Kind>: CollectionPageRef<Kind> { pub trait CollectionPageExt<Kind>: AsCollectionPage<Kind> {
fn part_of<'a>(&'a self) -> Option<&'a AnyObject> fn part_of<'a>(&'a self) -> Option<&'a AnyBase>
where where
Kind: 'a, Kind: 'a,
{ {
self.collection_page_ref().part_of.as_ref() 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<Kind>: CollectionPageMut<Kind> {
fn set_part_of<T>(&mut self, part_of: T) -> &mut Self fn set_part_of<T>(&mut self, part_of: T) -> &mut Self
where where
T: Into<AnyObject>, T: Into<AnyBase>,
{ {
self.collection_page_mut().part_of = Some(part_of.into()); self.collection_page_mut().part_of = Some(part_of.into());
self self
} }
fn take_part_of(&mut self) -> Option<AnyObject> { fn take_part_of(&mut self) -> Option<AnyBase> {
self.collection_page_mut().part_of.take() self.collection_page_mut().part_of.take()
} }
@ -210,15 +185,22 @@ pub trait CollectionPageMutExt<Kind>: CollectionPageMut<Kind> {
self 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 fn set_next<T>(&mut self, next: T) -> &mut Self
where where
T: Into<AnyObject>, T: Into<AnyBase>,
{ {
self.collection_page_mut().next = Some(next.into()); self.collection_page_mut().next = Some(next.into());
self self
} }
fn take_next(&mut self) -> Option<AnyObject> { fn take_next(&mut self) -> Option<AnyBase> {
self.collection_page_mut().next.take() self.collection_page_mut().next.take()
} }
@ -227,15 +209,22 @@ pub trait CollectionPageMutExt<Kind>: CollectionPageMut<Kind> {
self 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 fn set_prev<T>(&mut self, prev: T) -> &mut Self
where where
T: Into<AnyObject>, T: Into<AnyBase>,
{ {
self.collection_page_mut().prev = Some(prev.into()); self.collection_page_mut().prev = Some(prev.into());
self self
} }
fn take_prev(&mut self) -> Option<AnyObject> { fn take_prev(&mut self) -> Option<AnyBase> {
self.collection_page_mut().prev.take() self.collection_page_mut().prev.take()
} }
@ -245,13 +234,11 @@ pub trait CollectionPageMutExt<Kind>: CollectionPageMut<Kind> {
} }
} }
pub trait OrderedCollectionPageRefExt: OrderedCollectionPageRef { pub trait OrderedCollectionPageExt: AsOrderedCollectionPage {
fn start_index(&self) -> Option<&XsdNonNegativeInteger> { fn start_index(&self) -> Option<&XsdNonNegativeInteger> {
self.ordered_collection_page_ref().start_index.as_ref() self.ordered_collection_page_ref().start_index.as_ref()
} }
}
pub trait OrderedCollectionPageMutExt: OrderedCollectionPageMut {
fn set_start_index<T>(&mut self, start_index: T) -> &mut Self fn set_start_index<T>(&mut self, start_index: T) -> &mut Self
where where
T: Into<XsdNonNegativeInteger>, T: Into<XsdNonNegativeInteger>,
@ -279,7 +266,7 @@ pub type UnorderedCollectionPage = CollectionPage<CollectionPageType>;
#[builder(doc)] #[builder(doc)]
pub struct Collection<Kind> { pub struct Collection<Kind> {
#[builder(setter(into))] #[builder(setter(into))]
pub items: OneOrMany<AnyObject>, pub items: OneOrMany<AnyBase>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))] #[builder(default, setter(strip_option, into))]
@ -287,15 +274,15 @@ pub struct Collection<Kind> {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))] #[builder(default, setter(strip_option, into))]
pub current: Option<AnyObject>, pub current: Option<AnyBase>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))] #[builder(default, setter(strip_option, into))]
pub first: Option<AnyObject>, pub first: Option<AnyBase>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))] #[builder(default, setter(strip_option, into))]
pub last: Option<AnyObject>, pub last: Option<AnyBase>,
#[serde(flatten)] #[serde(flatten)]
pub inner: Object<Kind>, pub inner: Object<Kind>,
@ -307,15 +294,15 @@ pub struct Collection<Kind> {
pub struct CollectionPage<Kind> { pub struct CollectionPage<Kind> {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))] #[builder(default, setter(strip_option, into))]
pub part_of: Option<AnyObject>, pub part_of: Option<AnyBase>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))] #[builder(default, setter(strip_option, into))]
pub next: Option<AnyObject>, pub next: Option<AnyBase>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))] #[builder(default, setter(strip_option, into))]
pub prev: Option<AnyObject>, pub prev: Option<AnyBase>,
#[serde(flatten)] #[serde(flatten)]
pub inner: Collection<Kind>, pub inner: Collection<Kind>,
@ -426,29 +413,34 @@ impl OrderedCollectionPage {
} }
} }
impl<Kind> traits::Base for Collection<Kind> {} impl<Kind> markers::Base for Collection<Kind> {}
impl<Kind> traits::Object for Collection<Kind> {} impl<Kind> markers::Object for Collection<Kind> {}
impl<Kind> traits::Collection for Collection<Kind> {} impl<Kind> markers::Collection for Collection<Kind> {}
impl<Kind> traits::Base for CollectionPage<Kind> {} impl<Kind> markers::Base for CollectionPage<Kind> {}
impl<Kind> traits::Object for CollectionPage<Kind> {} impl<Kind> markers::Object for CollectionPage<Kind> {}
impl<Kind> traits::Collection for CollectionPage<Kind> {} impl<Kind> markers::Collection for CollectionPage<Kind> {}
impl<Kind> traits::CollectionPage for CollectionPage<Kind> {} impl<Kind> markers::CollectionPage for CollectionPage<Kind> {}
impl traits::Base for OrderedCollectionPage {} impl markers::Base for OrderedCollectionPage {}
impl traits::Object for OrderedCollectionPage {} impl markers::Object for OrderedCollectionPage {}
impl traits::Collection for OrderedCollectionPage {} impl markers::Collection for OrderedCollectionPage {}
impl traits::CollectionPage for OrderedCollectionPage {} impl markers::CollectionPage for OrderedCollectionPage {}
impl<Kind> Extends<Object<Kind>> for Collection<Kind> { impl<Kind> Extends<Kind> for Collection<Kind>
where
Kind: serde::de::DeserializeOwned + serde::ser::Serialize,
{
type Error = serde_json::Error; type Error = serde_json::Error;
fn extends(object: Object<Kind>) -> Result<Self, Self::Error> { fn extends(base: Base<Kind>) -> Result<Self, Self::Error> {
Self::extending(object) let inner = Object::extends(base)?;
Self::extending(inner)
} }
fn retracts(self) -> Result<Object<Kind>, Self::Error> { fn retracts(self) -> Result<Base<Kind>, Self::Error> {
self.retracting() let inner = self.retracting()?;
inner.retracts()
} }
} }
@ -468,15 +460,20 @@ impl<Kind> TryFrom<Object<Kind>> for Collection<Kind> {
} }
} }
impl<Kind> Extends<Object<Kind>> for CollectionPage<Kind> { impl<Kind> Extends<Kind> for CollectionPage<Kind>
where
Kind: serde::de::DeserializeOwned + serde::ser::Serialize,
{
type Error = serde_json::Error; type Error = serde_json::Error;
fn extends(object: Object<Kind>) -> Result<Self, Self::Error> { fn extends(base: Base<Kind>) -> Result<Self, Self::Error> {
Self::extending(object) let inner = Object::extends(base)?;
Self::extending(inner)
} }
fn retracts(self) -> Result<Object<Kind>, Self::Error> { fn retracts(self) -> Result<Base<Kind>, Self::Error> {
self.retracting() let inner = self.retracting()?;
inner.retracts()
} }
} }
@ -496,15 +493,17 @@ impl<Kind> TryFrom<CollectionPage<Kind>> for Object<Kind> {
} }
} }
impl Extends<Object<OrderedCollectionPageType>> for OrderedCollectionPage { impl Extends<OrderedCollectionPageType> for OrderedCollectionPage {
type Error = serde_json::Error; type Error = serde_json::Error;
fn extends(object: Object<OrderedCollectionPageType>) -> Result<Self, Self::Error> { fn extends(base: Base<OrderedCollectionPageType>) -> Result<Self, Self::Error> {
Self::extending(object) let inner = Object::extends(base)?;
Self::extending(inner)
} }
fn retracts(self) -> Result<Object<OrderedCollectionPageType>, Self::Error> { fn retracts(self) -> Result<Base<OrderedCollectionPageType>, Self::Error> {
self.retracting() let inner = self.retracting()?;
inner.retracts()
} }
} }
@ -542,153 +541,158 @@ impl UnparsedMut for OrderedCollectionPage {
} }
} }
impl<Kind> ObjectRef<Kind> for Collection<Kind> { 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> { fn object_ref(&self) -> &Object<Kind> {
&self.inner &self.inner
} }
}
impl<Kind> ObjectMut<Kind> for Collection<Kind> {
fn object_mut(&mut self) -> &mut Object<Kind> { fn object_mut(&mut self) -> &mut Object<Kind> {
&mut self.inner &mut self.inner
} }
} }
impl<Kind> CollectionRef<Kind> for Collection<Kind> { impl<Kind> AsCollection<Kind> for Collection<Kind> {
fn collection_ref(&self) -> &Collection<Kind> { fn collection_ref(&self) -> &Collection<Kind> {
self self
} }
}
impl<Kind> CollectionMut<Kind> for Collection<Kind> {
fn collection_mut(&mut self) -> &mut Collection<Kind> { fn collection_mut(&mut self) -> &mut Collection<Kind> {
self self
} }
} }
impl<Kind> ObjectRef<Kind> for CollectionPage<Kind> { 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> { fn object_ref(&self) -> &Object<Kind> {
self.inner.object_ref() self.inner.object_ref()
} }
}
impl<Kind> ObjectMut<Kind> for CollectionPage<Kind> {
fn object_mut(&mut self) -> &mut Object<Kind> { fn object_mut(&mut self) -> &mut Object<Kind> {
self.inner.object_mut() self.inner.object_mut()
} }
} }
impl<Kind> CollectionRef<Kind> for CollectionPage<Kind> { impl<Kind> AsCollection<Kind> for CollectionPage<Kind> {
fn collection_ref(&self) -> &Collection<Kind> { fn collection_ref(&self) -> &Collection<Kind> {
&self.inner &self.inner
} }
}
impl<Kind> CollectionMut<Kind> for CollectionPage<Kind> {
fn collection_mut(&mut self) -> &mut Collection<Kind> { fn collection_mut(&mut self) -> &mut Collection<Kind> {
&mut self.inner &mut self.inner
} }
} }
impl<Kind> CollectionPageRef<Kind> for CollectionPage<Kind> { impl<Kind> AsCollectionPage<Kind> for CollectionPage<Kind> {
fn collection_page_ref(&self) -> &CollectionPage<Kind> { fn collection_page_ref(&self) -> &CollectionPage<Kind> {
self self
} }
}
impl<Kind> CollectionPageMut<Kind> for CollectionPage<Kind> {
fn collection_page_mut(&mut self) -> &mut CollectionPage<Kind> { fn collection_page_mut(&mut self) -> &mut CollectionPage<Kind> {
self self
} }
} }
impl ObjectRef<OrderedCollectionPageType> for OrderedCollectionPage { 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> { fn object_ref(&self) -> &Object<OrderedCollectionPageType> {
self.inner.object_ref() self.inner.object_ref()
} }
}
impl ObjectMut<OrderedCollectionPageType> for OrderedCollectionPage {
fn object_mut(&mut self) -> &mut Object<OrderedCollectionPageType> { fn object_mut(&mut self) -> &mut Object<OrderedCollectionPageType> {
self.inner.object_mut() self.inner.object_mut()
} }
} }
impl CollectionRef<OrderedCollectionPageType> for OrderedCollectionPage { impl AsCollection<OrderedCollectionPageType> for OrderedCollectionPage {
fn collection_ref(&self) -> &Collection<OrderedCollectionPageType> { fn collection_ref(&self) -> &Collection<OrderedCollectionPageType> {
self.inner.collection_ref() self.inner.collection_ref()
} }
}
impl CollectionMut<OrderedCollectionPageType> for OrderedCollectionPage {
fn collection_mut(&mut self) -> &mut Collection<OrderedCollectionPageType> { fn collection_mut(&mut self) -> &mut Collection<OrderedCollectionPageType> {
self.inner.collection_mut() self.inner.collection_mut()
} }
} }
impl CollectionPageRef<OrderedCollectionPageType> for OrderedCollectionPage { impl AsCollectionPage<OrderedCollectionPageType> for OrderedCollectionPage {
fn collection_page_ref(&self) -> &CollectionPage<OrderedCollectionPageType> { fn collection_page_ref(&self) -> &CollectionPage<OrderedCollectionPageType> {
&self.inner &self.inner
} }
}
impl CollectionPageMut<OrderedCollectionPageType> for OrderedCollectionPage {
fn collection_page_mut(&mut self) -> &mut CollectionPage<OrderedCollectionPageType> { fn collection_page_mut(&mut self) -> &mut CollectionPage<OrderedCollectionPageType> {
&mut self.inner &mut self.inner
} }
} }
impl<Inner> traits::Collection for ApObject<Inner> where Inner: traits::Collection {} impl<Inner> markers::Collection for ApObject<Inner> where Inner: markers::Collection {}
impl<Inner> traits::CollectionPage for ApObject<Inner> where Inner: traits::CollectionPage {} impl<Inner> markers::CollectionPage for ApObject<Inner> where Inner: markers::CollectionPage {}
impl<Inner, Kind> CollectionRef<Kind> for ApObject<Inner> impl<Inner, Kind> AsCollection<Kind> for ApObject<Inner>
where where
Inner: CollectionRef<Kind>, Inner: AsCollection<Kind>,
{ {
fn collection_ref(&self) -> &Collection<Kind> { fn collection_ref(&self) -> &Collection<Kind> {
self.inner.collection_ref() self.inner.collection_ref()
} }
}
impl<Inner, Kind> CollectionMut<Kind> for ApObject<Inner>
where
Inner: CollectionMut<Kind>,
{
fn collection_mut(&mut self) -> &mut Collection<Kind> { fn collection_mut(&mut self) -> &mut Collection<Kind> {
self.inner.collection_mut() self.inner.collection_mut()
} }
} }
impl<Inner, Kind> CollectionPageRef<Kind> for ApObject<Inner> impl<Inner, Kind> AsCollectionPage<Kind> for ApObject<Inner>
where where
Inner: CollectionPageRef<Kind>, Inner: AsCollectionPage<Kind>,
{ {
fn collection_page_ref(&self) -> &CollectionPage<Kind> { fn collection_page_ref(&self) -> &CollectionPage<Kind> {
self.inner.collection_page_ref() self.inner.collection_page_ref()
} }
}
impl<Inner, Kind> CollectionPageMut<Kind> for ApObject<Inner>
where
Inner: CollectionPageMut<Kind>,
{
fn collection_page_mut(&mut self) -> &mut CollectionPage<Kind> { fn collection_page_mut(&mut self) -> &mut CollectionPage<Kind> {
self.inner.collection_page_mut() self.inner.collection_page_mut()
} }
} }
impl<Inner> OrderedCollectionPageRef for ApObject<Inner> impl<Inner> AsOrderedCollectionPage for ApObject<Inner>
where where
Inner: OrderedCollectionPageRef, Inner: AsOrderedCollectionPage,
{ {
fn ordered_collection_page_ref(&self) -> &OrderedCollectionPage { fn ordered_collection_page_ref(&self) -> &OrderedCollectionPage {
self.inner.ordered_collection_page_ref() self.inner.ordered_collection_page_ref()
} }
}
impl<Inner> OrderedCollectionPageMut for ApObject<Inner>
where
Inner: OrderedCollectionPageMut,
{
fn ordered_collection_page_mut(&mut self) -> &mut OrderedCollectionPage { fn ordered_collection_page_mut(&mut self) -> &mut OrderedCollectionPage {
self.inner.ordered_collection_page_mut() self.inner.ordered_collection_page_mut()
} }
} }
impl<T, Kind> CollectionRefExt<Kind> for T where T: CollectionRef<Kind> {} impl<T, Kind> CollectionExt<Kind> for T where T: AsCollection<Kind> {}
impl<T, Kind> CollectionMutExt<Kind> for T where T: CollectionMut<Kind> {} impl<T, Kind> CollectionPageExt<Kind> for T where T: AsCollectionPage<Kind> {}
impl<T> OrderedCollectionPageExt for T where T: AsOrderedCollectionPage {}
impl<T, Kind> CollectionPageRefExt<Kind> for T where T: CollectionPageRef<Kind> {}
impl<T, Kind> CollectionPageMutExt<Kind> for T where T: CollectionPageMut<Kind> {}
impl<T> OrderedCollectionPageRefExt for T where T: OrderedCollectionPageRef {}
impl<T> OrderedCollectionPageMutExt for T where T: OrderedCollectionPageMut {}

View file

@ -1,29 +1,31 @@
pub mod activity; pub mod activity;
pub mod actor; pub mod actor;
pub mod base;
pub mod collection; pub mod collection;
pub mod either; pub mod either;
pub mod link;
pub mod object; pub mod object;
pub mod primitives; pub mod primitives;
pub mod traits; pub mod unparsed;
pub use activitystreams::{context, public, security}; pub use activitystreams::{context, public, security};
pub mod markers {
pub use activitystreams::{
Activity, Actor, Base, Collection, CollectionPage, IntransitiveActivity, Link, Object,
};
}
pub mod prelude { pub mod prelude {
pub use crate::{ pub use crate::{
activity::{ activity::{
ActivityMutExt, ActivityRefExt, ActorAndObjectMutExt, ActorAndObjectRefExt, ActivityExt, ActorAndObjectRefExt, OptOriginRefExt, OptTargetRefExt, OriginRefExt,
OptOriginMutExt, OptOriginRefExt, OptTargetMutExt, OptTargetRefExt, OriginMutExt, QuestionExt, TargetRefExt,
OriginRefExt, QuestionMutExt, QuestionRefExt, TargetMutExt, TargetRefExt,
},
actor::{ApActorMutExt, ApActorRefExt},
collection::{
CollectionMutExt, CollectionPageMutExt, CollectionPageRefExt, CollectionRefExt,
OrderedCollectionPageMutExt, OrderedCollectionPageRefExt,
},
object::{
ApObjectMutExt, ApObjectRefExt, ObjectMutExt, ObjectRefExt, PlaceRefExt, ProfileMutExt,
ProfileRefExt, RelationshipMutExt, RelationshipRefExt, TombstoneMutExt,
TombstoneRefExt,
}, },
actor::ApActorExt,
base::BaseExt,
collection::{CollectionExt, CollectionPageExt, OrderedCollectionPageExt},
link::LinkExt,
object::{ApObjectExt, ObjectExt, PlaceExt, ProfileExt, RelationshipExt, TombstoneExt},
}; };
} }

284
src/link.rs Normal file
View file

@ -0,0 +1,284 @@
use crate::{
base::{AsBase, Base, Extends},
markers,
primitives::{OneOrMany, XsdAnyUri, XsdNonNegativeInteger, XsdString},
unparsed::{Unparsed, UnparsedMut, UnparsedMutExt},
};
use std::convert::TryFrom;
use typed_builder::TypedBuilder;
pub mod kind {
pub use activitystreams::link::kind::MentionType;
}
use self::kind::MentionType;
pub trait AsLink<Kind>: markers::Link {
fn link_ref(&self) -> &Link<Kind>;
fn link_mut(&mut self) -> &mut Link<Kind>;
}
pub trait LinkExt<Kind>: AsLink<Kind> {
fn href<'a>(&'a self) -> Option<&'a XsdAnyUri>
where
Kind: 'a,
{
self.link_ref().href.as_ref()
}
fn set_href(&mut self, href: XsdAnyUri) -> &mut Self {
self.link_mut().href = Some(href);
self
}
fn take_href(&mut self) -> Option<XsdAnyUri> {
self.link_mut().href.take()
}
fn delete_href(&mut self) -> &mut Self {
self.link_mut().href = None;
self
}
fn hreflang<'a>(&'a self) -> Option<&'a XsdString>
where
Kind: 'a,
{
self.link_ref().hreflang.as_ref()
}
fn set_hreflang(&mut self, hreflang: XsdString) -> &mut Self {
self.link_mut().hreflang = Some(hreflang);
self
}
fn take_hreflang(&mut self) -> Option<XsdString> {
self.link_mut().hreflang.take()
}
fn delete_hreflang(&mut self) -> &mut Self {
self.link_mut().hreflang = None;
self
}
fn rel<'a>(&'a self) -> Option<&'a OneOrMany<XsdString>>
where
Kind: 'a,
{
self.link_ref().rel.as_ref()
}
fn set_rel(&mut self, rel: XsdString) -> &mut Self {
self.link_mut().rel = Some(rel.into());
self
}
fn set_many_rels<I>(&mut self, items: I) -> &mut Self
where
I: IntoIterator<Item = XsdString>,
{
let v: Vec<_> = items.into_iter().collect();
self.link_mut().rel = Some(v.into());
self
}
fn add_rel(&mut self, rel: XsdString) -> &mut Self {
let v = match self.link_mut().rel.take() {
Some(mut v) => {
v.add(rel);
v
}
None => vec![rel].into(),
};
self.link_mut().rel = Some(v);
self
}
fn take_rel(&mut self) -> Option<OneOrMany<XsdString>> {
self.link_mut().rel.take()
}
fn delete_rel(&mut self) -> &mut Self {
self.link_mut().rel = None;
self
}
fn height<'a>(&'a self) -> Option<&'a XsdNonNegativeInteger>
where
Kind: 'a,
{
self.link_ref().height.as_ref()
}
fn set_height<T>(&mut self, height: T) -> &mut Self
where
T: Into<XsdNonNegativeInteger>,
{
self.link_mut().height = Some(height.into());
self
}
fn take_height(&mut self) -> Option<XsdNonNegativeInteger> {
self.link_mut().height.take()
}
fn delete_height(&mut self) -> &mut Self {
self.link_mut().height = None;
self
}
fn width<'a>(&'a self) -> Option<&'a XsdNonNegativeInteger>
where
Kind: 'a,
{
self.link_ref().width.as_ref()
}
fn set_width<T>(&mut self, width: T) -> &mut Self
where
T: Into<XsdNonNegativeInteger>,
{
self.link_mut().width = Some(width.into());
self
}
fn take_width(&mut self) -> Option<XsdNonNegativeInteger> {
self.link_mut().width.take()
}
fn delete_width(&mut self) -> &mut Self {
self.link_mut().width = None;
self
}
}
pub type Mention = Link<MentionType>;
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize, TypedBuilder)]
#[serde(rename_all = "camelCase")]
#[builder(doc)]
pub struct Link<Kind> {
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option))]
href: Option<XsdAnyUri>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option))]
hreflang: Option<XsdString>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))]
rel: Option<OneOrMany<XsdString>>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))]
height: Option<XsdNonNegativeInteger>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))]
width: Option<XsdNonNegativeInteger>,
#[serde(flatten)]
inner: Base<Kind>,
}
impl<Kind> Link<Kind> {
fn extending(mut inner: Base<Kind>) -> Result<Self, serde_json::Error> {
Ok(Link {
href: inner.remove("href")?,
hreflang: inner.remove("hreflang")?,
rel: inner.remove("rel")?,
height: inner.remove("height")?,
width: inner.remove("width")?,
inner,
})
}
fn retracting(self) -> Result<Base<Kind>, serde_json::Error> {
let Link {
href,
hreflang,
rel,
height,
width,
mut inner,
} = self;
inner
.insert("href", href)?
.insert("hreflang", hreflang)?
.insert("rel", rel)?
.insert("height", height)?
.insert("width", width)?;
Ok(inner)
}
}
impl<Kind> markers::Base for Link<Kind> {}
impl<Kind> markers::Link for Link<Kind> {}
impl<Kind> Extends<Kind> for Link<Kind>
where
Kind: serde::de::DeserializeOwned + serde::ser::Serialize,
{
type Error = serde_json::Error;
fn extends(base: Base<Kind>) -> Result<Self, Self::Error> {
Self::extending(base)
}
fn retracts(self) -> Result<Base<Kind>, Self::Error> {
self.retracting()
}
}
impl<Kind> TryFrom<Base<Kind>> for Link<Kind>
where
Kind: serde::de::DeserializeOwned,
{
type Error = serde_json::Error;
fn try_from(base: Base<Kind>) -> Result<Self, Self::Error> {
Self::extending(base)
}
}
impl<Kind> TryFrom<Link<Kind>> for Base<Kind>
where
Kind: serde::ser::Serialize,
{
type Error = serde_json::Error;
fn try_from(link: Link<Kind>) -> Result<Self, Self::Error> {
link.retracting()
}
}
impl<Kind> UnparsedMut for Link<Kind> {
fn unparsed_mut(&mut self) -> &mut Unparsed {
self.inner.unparsed_mut()
}
}
impl<Kind> AsBase<Kind> for Link<Kind> {
fn base_ref(&self) -> &Base<Kind> {
&self.inner
}
fn base_mut(&mut self) -> &mut Base<Kind> {
&mut self.inner
}
}
impl<Kind> AsLink<Kind> for Link<Kind> {
fn link_ref(&self) -> &Link<Kind> {
self
}
fn link_mut(&mut self) -> &mut Link<Kind> {
self
}
}
impl<T, Kind> LinkExt<Kind> for T where T: AsLink<Kind> {}

File diff suppressed because it is too large Load diff

View file

@ -5,10 +5,6 @@ pub use activitystreams::primitives::{
XsdNonNegativeInteger, XsdString, XsdNonNegativeInteger, XsdString,
}; };
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
#[serde(transparent)]
pub struct Unparsed(std::collections::HashMap<String, serde_json::Value>);
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(transparent)] #[serde(transparent)]
pub struct AnyString(Either<XsdString, RdfLangString>); pub struct AnyString(Either<XsdString, RdfLangString>);
@ -19,16 +15,6 @@ pub type Unit = Either<Length, XsdString>;
#[serde(transparent)] #[serde(transparent)]
pub struct OneOrMany<T>(pub Either<T, Vec<T>>); pub struct OneOrMany<T>(pub Either<T, Vec<T>>);
impl Unparsed {
pub(crate) fn remove(&mut self, key: &str) -> serde_json::Value {
self.0.remove(key).unwrap_or(serde_json::Value::Null)
}
pub(crate) fn insert(&mut self, key: String, value: serde_json::Value) {
self.0.insert(key, value);
}
}
impl AnyString { impl AnyString {
pub fn as_xsd_string(&self) -> Option<&XsdString> { pub fn as_xsd_string(&self) -> Option<&XsdString> {
self.0.as_ref().left() self.0.as_ref().left()
@ -185,14 +171,6 @@ impl<T> OneOrMany<T> {
} }
} }
impl crate::traits::Base for Unparsed {}
impl crate::traits::UnparsedMut for Unparsed {
fn unparsed_mut(&mut self) -> &mut Unparsed {
self
}
}
impl<T> From<T> for OneOrMany<T> { impl<T> From<T> for OneOrMany<T> {
fn from(t: T) -> Self { fn from(t: T) -> Self {
OneOrMany::from_one(t) OneOrMany::from_one(t)

View file

@ -1,8 +1,3 @@
use crate::primitives::Unparsed;
pub use activitystreams::{
Activity, Actor, Base, Collection, CollectionPage, IntransitiveActivity, Link, Object,
};
pub trait UnparsedMut { pub trait UnparsedMut {
fn unparsed_mut(&mut self) -> &mut Unparsed; fn unparsed_mut(&mut self) -> &mut Unparsed;
} }
@ -25,12 +20,24 @@ pub trait UnparsedMutExt: UnparsedMut {
} }
} }
pub trait Extends<T>: Sized { #[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
type Error: std::error::Error; #[serde(transparent)]
pub struct Unparsed(std::collections::HashMap<String, serde_json::Value>);
fn extends(t: T) -> Result<Self, Self::Error>; impl Unparsed {
pub(crate) fn remove(&mut self, key: &str) -> serde_json::Value {
self.0.remove(key).unwrap_or(serde_json::Value::Null)
}
fn retracts(self) -> Result<T, Self::Error>; pub(crate) fn insert(&mut self, key: String, value: serde_json::Value) {
self.0.insert(key, value);
}
}
impl UnparsedMut for Unparsed {
fn unparsed_mut(&mut self) -> &mut Unparsed {
self
}
} }
impl<T> UnparsedMutExt for T where T: UnparsedMut {} impl<T> UnparsedMutExt for T where T: UnparsedMut {}