diff --git a/Cargo.toml b/Cargo.toml index 79d3d6b..34f8b85 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -activitystreams = { version = "0.6.1", default-features = false, features = ["kinds","primitives"] } +activitystreams = { version = "0.6.2", default-features = false, features = ["kinds","primitives"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" typed-builder = "0.5.1" diff --git a/examples/de.rs b/examples/de.rs index 1c6b87e..6415402 100644 --- a/examples/de.rs +++ b/examples/de.rs @@ -1,6 +1,7 @@ use activitystreams_new::{ collection::OrderedCollection, object::{ApObject, Page}, + prelude::*, traits::Extends, }; use anyhow::Error; @@ -43,11 +44,11 @@ fn main() -> Result<(), Error> { let page: ApObject = serde_json::from_str(page_json)?; println!("{:#?}", page); - let mut collection: OrderedCollection = serde_json::from_str(collection_json)?; + let mut collection: ApObject = serde_json::from_str(collection_json)?; println!("{:#?}", collection); let v: Vec> = collection - .items + .items() .clone() .many() .into_iter() @@ -62,7 +63,7 @@ fn main() -> Result<(), Error> { .map(|o| o.retracts().and_then(|o| o.into_generic())) .collect::, _>>()?; - collection.items.set_many(v); + collection.set_many_items(v); Ok(()) } diff --git a/src/activity.rs b/src/activity.rs index ad3685a..3ced894 100644 --- a/src/activity.rs +++ b/src/activity.rs @@ -1,7 +1,7 @@ use crate::{ - object::{AnyObject, Object}, + object::{AnyObject, ApObject, Object, ObjectMut, ObjectRef}, primitives::{OneOrMany, Unparsed}, - traits::{self, Extends, WithUnparsed, WithUnparsedExt}, + traits::{self, Extends, UnparsedMut, UnparsedMutExt}, }; use std::convert::TryFrom; use typed_builder::TypedBuilder; @@ -12,6 +12,494 @@ pub mod kind { use self::kind::*; +pub trait ActivityRef: traits::Activity { + fn activity_ref(&self) -> &Activity; +} + +pub trait ActivityMut: traits::Activity { + fn activity_mut(&mut self) -> &mut Activity; +} + +pub trait ActorAndObjectRef: traits::Activity { + fn actor_field_ref(&self) -> &OneOrMany; + fn object_field_ref(&self) -> &OneOrMany; +} + +pub trait ActorAndObjectMut: traits::Activity { + fn actor_field_mut(&mut self) -> &mut OneOrMany; + fn object_field_mut(&mut self) -> &mut OneOrMany; +} + +pub trait TargetRef: traits::Activity { + fn target_field_ref(&self) -> &OneOrMany; +} + +pub trait TargetMut: traits::Activity { + fn target_field_mut(&mut self) -> &mut OneOrMany; +} + +pub trait OriginRef: traits::Activity { + fn origin_field_ref(&self) -> &OneOrMany; +} + +pub trait OriginMut: traits::Activity { + fn origin_field_mut(&mut self) -> &mut OneOrMany; +} + +pub trait OptTargetRef: traits::Activity { + fn target_field_ref(&self) -> &Option>; +} + +pub trait OptTargetMut: traits::Activity { + fn target_field_mut(&mut self) -> &mut Option>; +} + +pub trait OptOriginRef: traits::Activity { + fn origin_field_ref(&self) -> &Option>; +} + +pub trait OptOriginMut: traits::Activity { + fn origin_field_mut(&mut self) -> &mut Option>; +} + +pub trait QuestionRef: traits::Activity { + fn question_ref(&self) -> &Question; +} + +pub trait QuestionMut: traits::Activity { + fn question_mut(&mut self) -> &mut Question; +} + +pub trait ActivityRefExt: ActivityRef { + fn result<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.activity_ref().result.as_ref() + } + + fn instrument<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.activity_ref().instrument.as_ref() + } +} + +pub trait ActivityMutExt: ActivityMut { + fn set_result(&mut self, result: T) -> &mut Self + where + T: Into, + { + self.activity_mut().result = Some(result.into().into()); + self + } + + fn set_many_results(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.activity_mut().result = Some(v.into()); + self + } + + fn add_result(&mut self, result: T) -> &mut Self + where + T: Into, + { + let c = match self.activity_mut().result.take() { + Some(mut c) => { + c.add(result.into()); + c + } + None => vec![result.into()].into(), + }; + self.activity_mut().result = Some(c); + self + } + + fn take_result(&mut self) -> Option> { + self.activity_mut().result.take() + } + + fn delete_result(&mut self) -> &mut Self { + self.activity_mut().result = None; + self + } + + fn set_instrument(&mut self, instrument: T) -> &mut Self + where + T: Into, + { + self.activity_mut().instrument = Some(instrument.into().into()); + self + } + + fn set_many_instruments(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.activity_mut().instrument = Some(v.into()); + self + } + + fn add_instrument(&mut self, instrument: T) -> &mut Self + where + T: Into, + { + let c = match self.activity_mut().instrument.take() { + Some(mut c) => { + c.add(instrument.into()); + c + } + None => vec![instrument.into()].into(), + }; + self.activity_mut().instrument = Some(c); + self + } + + fn take_instrument(&mut self) -> Option> { + self.activity_mut().instrument.take() + } + + fn delete_instrument(&mut self) -> &mut Self { + self.activity_mut().instrument = None; + self + } +} + +pub trait ActorAndObjectRefExt: ActorAndObjectRef { + fn actor(&self) -> &OneOrMany { + self.actor_field_ref() + } + + fn object(&self) -> &OneOrMany { + self.object_field_ref() + } +} + +pub trait ActorAndObjectMutExt: ActorAndObjectMut { + fn set_actor(&mut self, actor: T) -> &mut Self + where + T: Into, + { + *self.actor_field_mut() = actor.into().into(); + self + } + + fn set_many_actors(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + *self.actor_field_mut() = v.into(); + self + } + + fn add_actor(&mut self, actor: T) -> &mut Self + where + T: Into, + { + self.actor_field_mut().add(actor.into()); + self + } + + fn set_object(&mut self, object: T) -> &mut Self + where + T: Into, + { + *self.object_field_mut() = object.into().into(); + self + } + + fn set_many_objects(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + *self.object_field_mut() = v.into(); + self + } + + fn add_object(&mut self, object: T) -> &mut Self + where + T: Into, + { + self.object_field_mut().add(object.into()); + self + } +} + +pub trait TargetRefExt: TargetRef { + fn target(&self) -> &OneOrMany { + self.target_field_ref() + } +} + +pub trait TargetMutExt: TargetMut { + fn set_target(&mut self, target: T) -> &mut Self + where + T: Into, + { + *self.target_field_mut() = target.into().into(); + self + } + + fn set_many_targets(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + *self.target_field_mut() = v.into(); + self + } + + fn add_target(&mut self, target: T) -> &mut Self + where + T: Into, + { + self.target_field_mut().add(target.into()); + self + } +} + +pub trait OriginRefExt: OriginRef { + fn origin(&self) -> &OneOrMany { + self.origin_field_ref() + } +} + +pub trait OriginMutExt: OriginMut { + fn set_origin(&mut self, origin: T) -> &mut Self + where + T: Into, + { + *self.origin_field_mut() = origin.into().into(); + self + } + + fn set_many_origins(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + *self.origin_field_mut() = v.into(); + self + } + + fn add_origin(&mut self, origin: T) -> &mut Self + where + T: Into, + { + self.origin_field_mut().add(origin.into()); + self + } +} + +pub trait OptTargetRefExt: OptTargetRef { + fn target(&self) -> Option<&OneOrMany> { + self.target_field_ref().as_ref() + } +} + +pub trait OptTargetMutExt: OptTargetMut { + fn set_target(&mut self, target: T) -> &mut Self + where + T: Into, + { + *self.target_field_mut() = Some(target.into().into()); + self + } + + fn set_many_targets(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + *self.target_field_mut() = Some(v.into()); + self + } + + fn add_target(&mut self, target: T) -> &mut Self + where + T: Into, + { + let c = match self.target_field_mut().take() { + Some(mut c) => { + c.add(target.into()); + c + } + None => vec![target.into()].into(), + }; + *self.target_field_mut() = Some(c); + self + } + + fn take_target(&mut self) -> Option> { + self.target_field_mut().take() + } + + fn delete_target(&mut self) -> &mut Self { + *self.target_field_mut() = None; + self + } +} + +pub trait OptOriginRefExt: OptOriginRef { + fn origin(&self) -> Option<&OneOrMany> { + self.origin_field_ref().as_ref() + } +} + +pub trait OptOriginMutExt: OptOriginMut { + fn set_origin(&mut self, origin: T) -> &mut Self + where + T: Into, + { + *self.origin_field_mut() = Some(origin.into().into()); + self + } + + fn set_many_origins(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + *self.origin_field_mut() = Some(v.into()); + self + } + + fn add_origin(&mut self, origin: T) -> &mut Self + where + T: Into, + { + let c = match self.origin_field_mut().take() { + Some(mut c) => { + c.add(origin.into()); + c + } + None => vec![origin.into()].into(), + }; + *self.origin_field_mut() = Some(c); + self + } + + fn take_origin(&mut self) -> Option> { + self.origin_field_mut().take() + } + + fn delete_origin(&mut self) -> &mut Self { + *self.origin_field_mut() = None; + self + } +} + +pub trait QuestionRefExt: QuestionRef { + fn one_of(&self) -> Option<&OneOrMany> { + self.question_ref().one_of.as_ref() + } + + fn any_of(&self) -> Option<&OneOrMany> { + self.question_ref().any_of.as_ref() + } +} + +pub trait QuestionMutExt: QuestionMut { + fn set_one_of(&mut self, one_of: T) -> &mut Self + where + T: Into, + { + self.question_mut().one_of = Some(one_of.into().into()); + self + } + + fn set_many_one_ofs(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.question_mut().one_of = Some(v.into()); + self + } + + fn add_one_of(&mut self, one_of: T) -> &mut Self + where + T: Into, + { + let v = match self.question_mut().one_of.take() { + Some(mut v) => { + v.add(one_of.into()); + v + } + None => vec![one_of.into()].into(), + }; + self.question_mut().one_of = Some(v); + self + } + + fn take_one_of(&mut self) -> Option> { + self.question_mut().one_of.take() + } + + fn delete_one_of(&mut self) -> &mut Self { + self.question_mut().one_of = None; + self + } + + fn set_any_of(&mut self, any_of: T) -> &mut Self + where + T: Into, + { + self.question_mut().any_of = Some(any_of.into().into()); + self + } + + fn set_many_any_ofs(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.question_mut().any_of = Some(v.into()); + self + } + + fn add_any_of(&mut self, any_of: T) -> &mut Self + where + T: Into, + { + let v = match self.question_mut().any_of.take() { + Some(mut v) => { + v.add(any_of.into()); + v + } + None => vec![any_of.into()].into(), + }; + self.question_mut().any_of = Some(v); + self + } + + fn take_any_of(&mut self) -> Option> { + self.question_mut().any_of.take() + } + + fn delete_any_of(&mut self) -> &mut Self { + self.question_mut().any_of = None; + self + } +} + pub type Accept = ActorAndObject; pub type Add = ActorAndObject; pub type Block = ActorAndObject; @@ -38,11 +526,6 @@ pub type Offer = ActorAndObjectOptTarget; pub type Move = ActorAndObjectOptOriginAndTarget; pub type Remove = ActorAndObjectOptOriginAndTarget; -pub trait NormalActivity: traits::Activity { - fn actor(&self) -> &OneOrMany; - fn object(&self) -> &OneOrMany; -} - #[derive(Debug, Clone, serde::Deserialize, serde::Serialize, TypedBuilder)] #[serde(rename_all = "camelCase")] #[builder(doc)] @@ -474,21 +957,21 @@ impl Question { } } -impl traits::Base for Activity where Kind: std::fmt::Debug {} -impl traits::Object for Activity where Kind: std::fmt::Debug {} -impl traits::Activity for Activity where Kind: std::fmt::Debug {} +impl traits::Base for Activity {} +impl traits::Object for Activity {} +impl traits::Activity for Activity {} -impl traits::Base for ActorAndObject where Kind: std::fmt::Debug {} -impl traits::Object for ActorAndObject where Kind: std::fmt::Debug {} -impl traits::Activity for ActorAndObject where Kind: std::fmt::Debug {} +impl traits::Base for ActorAndObject {} +impl traits::Object for ActorAndObject {} +impl traits::Activity for ActorAndObject {} -impl traits::Base for ActorAndObjectOptTarget where Kind: std::fmt::Debug {} -impl traits::Object for ActorAndObjectOptTarget where Kind: std::fmt::Debug {} -impl traits::Activity for ActorAndObjectOptTarget where Kind: std::fmt::Debug {} +impl traits::Base for ActorAndObjectOptTarget {} +impl traits::Object for ActorAndObjectOptTarget {} +impl traits::Activity for ActorAndObjectOptTarget {} -impl traits::Base for ActorAndObjectOptOriginAndTarget where Kind: std::fmt::Debug {} -impl traits::Object for ActorAndObjectOptOriginAndTarget where Kind: std::fmt::Debug {} -impl traits::Activity for ActorAndObjectOptOriginAndTarget where Kind: std::fmt::Debug {} +impl traits::Base for ActorAndObjectOptOriginAndTarget {} +impl traits::Object for ActorAndObjectOptOriginAndTarget {} +impl traits::Activity for ActorAndObjectOptOriginAndTarget {} impl traits::Base for Arrive {} impl traits::Object for Arrive {} @@ -765,151 +1248,630 @@ impl TryFrom for Object { } } -impl WithUnparsed for Activity { - fn unparsed(&self) -> &Unparsed { - self.inner.unparsed() - } - +impl UnparsedMut for Activity { fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } -impl WithUnparsed for ActorAndObject { - fn unparsed(&self) -> &Unparsed { - self.inner.unparsed() - } - +impl UnparsedMut for ActorAndObject { fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } -impl WithUnparsed for Arrive { - fn unparsed(&self) -> &Unparsed { - self.inner.unparsed() - } - +impl UnparsedMut for Arrive { fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } -impl WithUnparsed for Invite { - fn unparsed(&self) -> &Unparsed { - self.inner.unparsed() - } - +impl UnparsedMut for Invite { fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } -impl WithUnparsed for Delete { - fn unparsed(&self) -> &Unparsed { - self.inner.unparsed() - } - +impl UnparsedMut for Delete { fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } -impl WithUnparsed for ActorAndObjectOptOriginAndTarget { - fn unparsed(&self) -> &Unparsed { - self.inner.unparsed() - } - +impl UnparsedMut for ActorAndObjectOptOriginAndTarget { fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } -impl WithUnparsed for ActorAndObjectOptTarget { - fn unparsed(&self) -> &Unparsed { - self.inner.unparsed() - } - +impl UnparsedMut for ActorAndObjectOptTarget { fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } -impl WithUnparsed for Travel { - fn unparsed(&self) -> &Unparsed { - self.inner.unparsed() - } - +impl UnparsedMut for Travel { fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } -impl WithUnparsed for Question { - fn unparsed(&self) -> &Unparsed { - self.inner.unparsed() - } - +impl UnparsedMut for Question { fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } -impl NormalActivity for ActorAndObject +impl ObjectRef for Activity { + fn object_ref(&self) -> &Object { + &self.inner + } +} + +impl ObjectMut for Activity { + fn object_mut(&mut self) -> &mut Object { + &mut self.inner + } +} + +impl ActivityRef for Activity { + fn activity_ref(&self) -> &Activity { + self + } +} + +impl ActivityMut for Activity { + fn activity_mut(&mut self) -> &mut Activity { + self + } +} + +impl ObjectRef for ActorAndObject { + fn object_ref(&self) -> &Object { + self.inner.object_ref() + } +} + +impl ObjectMut for ActorAndObject { + fn object_mut(&mut self) -> &mut Object { + self.inner.object_mut() + } +} + +impl ActivityRef for ActorAndObject { + fn activity_ref(&self) -> &Activity { + &self.inner + } +} + +impl ActivityMut for ActorAndObject { + fn activity_mut(&mut self) -> &mut Activity { + &mut self.inner + } +} + +impl ActorAndObjectRef for ActorAndObject { + fn actor_field_ref(&self) -> &OneOrMany { + &self.actor + } + + fn object_field_ref(&self) -> &OneOrMany { + &self.object + } +} + +impl ActorAndObjectMut for ActorAndObject { + fn actor_field_mut(&mut self) -> &mut OneOrMany { + &mut self.actor + } + + fn object_field_mut(&mut self) -> &mut OneOrMany { + &mut self.object + } +} + +impl ObjectRef for ActorAndObjectOptTarget { + fn object_ref(&self) -> &Object { + self.inner.object_ref() + } +} + +impl ObjectMut for ActorAndObjectOptTarget { + fn object_mut(&mut self) -> &mut Object { + self.inner.object_mut() + } +} + +impl ActivityRef for ActorAndObjectOptTarget { + fn activity_ref(&self) -> &Activity { + &self.inner + } +} + +impl ActivityMut for ActorAndObjectOptTarget { + fn activity_mut(&mut self) -> &mut Activity { + &mut self.inner + } +} + +impl ActorAndObjectRef for ActorAndObjectOptTarget { + fn actor_field_ref(&self) -> &OneOrMany { + &self.actor + } + + fn object_field_ref(&self) -> &OneOrMany { + &self.object + } +} + +impl ActorAndObjectMut for ActorAndObjectOptTarget { + fn actor_field_mut(&mut self) -> &mut OneOrMany { + &mut self.actor + } + + fn object_field_mut(&mut self) -> &mut OneOrMany { + &mut self.object + } +} + +impl OptTargetRef for ActorAndObjectOptTarget { + fn target_field_ref(&self) -> &Option> { + &self.target + } +} + +impl OptTargetMut for ActorAndObjectOptTarget { + fn target_field_mut(&mut self) -> &mut Option> { + &mut self.target + } +} + +impl ObjectRef for ActorAndObjectOptOriginAndTarget { + fn object_ref(&self) -> &Object { + self.inner.object_ref() + } +} + +impl ObjectMut for ActorAndObjectOptOriginAndTarget { + fn object_mut(&mut self) -> &mut Object { + self.inner.object_mut() + } +} + +impl ActivityRef for ActorAndObjectOptOriginAndTarget { + fn activity_ref(&self) -> &Activity { + &self.inner + } +} + +impl ActivityMut for ActorAndObjectOptOriginAndTarget { + fn activity_mut(&mut self) -> &mut Activity { + &mut self.inner + } +} + +impl ActorAndObjectRef for ActorAndObjectOptOriginAndTarget { + fn actor_field_ref(&self) -> &OneOrMany { + &self.actor + } + + fn object_field_ref(&self) -> &OneOrMany { + &self.object + } +} + +impl ActorAndObjectMut for ActorAndObjectOptOriginAndTarget { + fn actor_field_mut(&mut self) -> &mut OneOrMany { + &mut self.actor + } + + fn object_field_mut(&mut self) -> &mut OneOrMany { + &mut self.object + } +} + +impl OptTargetRef for ActorAndObjectOptOriginAndTarget { + fn target_field_ref(&self) -> &Option> { + &self.target + } +} + +impl OptTargetMut for ActorAndObjectOptOriginAndTarget { + fn target_field_mut(&mut self) -> &mut Option> { + &mut self.target + } +} + +impl OptOriginRef for ActorAndObjectOptOriginAndTarget { + fn origin_field_ref(&self) -> &Option> { + &self.origin + } +} + +impl OptOriginMut for ActorAndObjectOptOriginAndTarget { + fn origin_field_mut(&mut self) -> &mut Option> { + &mut self.origin + } +} + +impl ObjectRef for Arrive { + fn object_ref(&self) -> &Object { + self.inner.object_ref() + } +} + +impl ObjectMut for Arrive { + fn object_mut(&mut self) -> &mut Object { + self.inner.object_mut() + } +} + +impl ActivityRef for Arrive { + fn activity_ref(&self) -> &Activity { + &self.inner + } +} + +impl ActivityMut for Arrive { + fn activity_mut(&mut self) -> &mut Activity { + &mut self.inner + } +} + +impl OriginRef for Arrive { + fn origin_field_ref(&self) -> &OneOrMany { + &self.origin + } +} + +impl OriginMut for Arrive { + fn origin_field_mut(&mut self) -> &mut OneOrMany { + &mut self.origin + } +} + +impl ObjectRef for Invite { + fn object_ref(&self) -> &Object { + self.inner.object_ref() + } +} + +impl ObjectMut for Invite { + fn object_mut(&mut self) -> &mut Object { + self.inner.object_mut() + } +} + +impl ActivityRef for Invite { + fn activity_ref(&self) -> &Activity { + &self.inner + } +} + +impl ActivityMut for Invite { + fn activity_mut(&mut self) -> &mut Activity { + &mut self.inner + } +} + +impl ActorAndObjectRef for Invite { + fn actor_field_ref(&self) -> &OneOrMany { + &self.actor + } + + fn object_field_ref(&self) -> &OneOrMany { + &self.object + } +} + +impl ActorAndObjectMut for Invite { + fn actor_field_mut(&mut self) -> &mut OneOrMany { + &mut self.actor + } + + fn object_field_mut(&mut self) -> &mut OneOrMany { + &mut self.object + } +} + +impl TargetRef for Invite { + fn target_field_ref(&self) -> &OneOrMany { + &self.target + } +} + +impl TargetMut for Invite { + fn target_field_mut(&mut self) -> &mut OneOrMany { + &mut self.target + } +} + +impl ObjectRef for Delete { + fn object_ref(&self) -> &Object { + self.inner.object_ref() + } +} + +impl ObjectMut for Delete { + fn object_mut(&mut self) -> &mut Object { + self.inner.object_mut() + } +} + +impl ActivityRef for Delete { + fn activity_ref(&self) -> &Activity { + &self.inner + } +} + +impl ActivityMut for Delete { + fn activity_mut(&mut self) -> &mut Activity { + &mut self.inner + } +} + +impl ActorAndObjectRef for Delete { + fn actor_field_ref(&self) -> &OneOrMany { + &self.actor + } + + fn object_field_ref(&self) -> &OneOrMany { + &self.object + } +} + +impl ActorAndObjectMut for Delete { + fn actor_field_mut(&mut self) -> &mut OneOrMany { + &mut self.actor + } + + fn object_field_mut(&mut self) -> &mut OneOrMany { + &mut self.object + } +} + +impl OptOriginRef for Delete { + fn origin_field_ref(&self) -> &Option> { + &self.origin + } +} + +impl OptOriginMut for Delete { + fn origin_field_mut(&mut self) -> &mut Option> { + &mut self.origin + } +} + +impl ObjectRef for Travel { + fn object_ref(&self) -> &Object { + self.inner.object_ref() + } +} + +impl ObjectMut for Travel { + fn object_mut(&mut self) -> &mut Object { + self.inner.object_mut() + } +} + +impl ActivityRef for Travel { + fn activity_ref(&self) -> &Activity { + self.inner.activity_ref() + } +} + +impl ActivityMut for Travel { + fn activity_mut(&mut self) -> &mut Activity { + self.inner.activity_mut() + } +} + +impl OptTargetRef for Travel { + fn target_field_ref(&self) -> &Option> { + &self.target + } +} + +impl OptTargetMut for Travel { + fn target_field_mut(&mut self) -> &mut Option> { + &mut self.target + } +} + +impl OptOriginRef for Travel { + fn origin_field_ref(&self) -> &Option> { + &self.origin + } +} + +impl OptOriginMut for Travel { + fn origin_field_mut(&mut self) -> &mut Option> { + &mut self.origin + } +} + +impl ObjectRef for Question { + fn object_ref(&self) -> &Object { + self.inner.object_ref() + } +} + +impl ObjectMut for Question { + fn object_mut(&mut self) -> &mut Object { + self.inner.object_mut() + } +} + +impl ActivityRef for Question { + fn activity_ref(&self) -> &Activity { + &self.inner + } +} + +impl ActivityMut for Question { + fn activity_mut(&mut self) -> &mut Activity { + &mut self.inner + } +} + +impl QuestionRef for Question { + fn question_ref(&self) -> &Question { + self + } +} + +impl QuestionMut for Question { + fn question_mut(&mut self) -> &mut Question { + self + } +} + +impl ActivityRefExt for T where T: ActivityRef {} +impl ActivityMutExt for T where T: ActivityMut {} + +impl ActorAndObjectRefExt for T where T: ActorAndObjectRef {} +impl ActorAndObjectMutExt for T where T: ActorAndObjectMut {} + +impl TargetRefExt for T where T: TargetRef {} +impl TargetMutExt for T where T: TargetMut {} + +impl OriginRefExt for T where T: OriginRef {} +impl OriginMutExt for T where T: OriginMut {} + +impl OptTargetRefExt for T where T: OptTargetRef {} +impl OptTargetMutExt for T where T: OptTargetMut {} + +impl OptOriginRefExt for T where T: OptOriginRef {} +impl OptOriginMutExt for T where T: OptOriginMut {} + +impl QuestionRefExt for T where T: QuestionRef {} +impl QuestionMutExt for T where T: QuestionMut {} + +impl traits::Activity for ApObject where Inner: traits::Activity {} + +impl ActivityRef for ApObject where - Kind: std::fmt::Debug, + Inner: ActivityRef, { - fn actor(&self) -> &OneOrMany { - &self.actor - } - - fn object(&self) -> &OneOrMany { - &self.object + fn activity_ref(&self) -> &Activity { + self.inner.activity_ref() } } - -impl NormalActivity for ActorAndObjectOptTarget +impl ActivityMut for ApObject where - Kind: std::fmt::Debug, + Inner: ActivityMut, { - fn actor(&self) -> &OneOrMany { - &self.actor - } - - fn object(&self) -> &OneOrMany { - &self.object + fn activity_mut(&mut self) -> &mut Activity { + self.inner.activity_mut() } } -impl NormalActivity for ActorAndObjectOptOriginAndTarget +impl ActorAndObjectRef for ApObject where - Kind: std::fmt::Debug, + Inner: ActorAndObjectRef, { - fn actor(&self) -> &OneOrMany { - &self.actor + fn actor_field_ref(&self) -> &OneOrMany { + self.inner.actor_field_ref() } - fn object(&self) -> &OneOrMany { - &self.object + fn object_field_ref(&self) -> &OneOrMany { + self.inner.object_field_ref() + } +} +impl ActorAndObjectMut for ApObject +where + Inner: ActorAndObjectMut, +{ + fn actor_field_mut(&mut self) -> &mut OneOrMany { + self.inner.actor_field_mut() + } + + fn object_field_mut(&mut self) -> &mut OneOrMany { + self.inner.object_field_mut() } } -impl NormalActivity for Invite { - fn actor(&self) -> &OneOrMany { - &self.actor +impl TargetRef for ApObject +where + Inner: TargetRef, +{ + fn target_field_ref(&self) -> &OneOrMany { + self.inner.target_field_ref() } - - fn object(&self) -> &OneOrMany { - &self.object +} +impl TargetMut for ApObject +where + Inner: TargetMut, +{ + fn target_field_mut(&mut self) -> &mut OneOrMany { + self.inner.target_field_mut() } } -impl NormalActivity for Delete { - fn actor(&self) -> &OneOrMany { - &self.actor - } - - fn object(&self) -> &OneOrMany { - &self.object +impl OriginRef for ApObject +where + Inner: OriginRef, +{ + fn origin_field_ref(&self) -> &OneOrMany { + self.inner.origin_field_ref() + } +} +impl OriginMut for ApObject +where + Inner: OriginMut, +{ + fn origin_field_mut(&mut self) -> &mut OneOrMany { + self.inner.origin_field_mut() + } +} + +impl OptTargetRef for ApObject +where + Inner: OptTargetRef, +{ + fn target_field_ref(&self) -> &Option> { + self.inner.target_field_ref() + } +} +impl OptTargetMut for ApObject +where + Inner: OptTargetMut, +{ + fn target_field_mut(&mut self) -> &mut Option> { + self.inner.target_field_mut() + } +} + +impl OptOriginRef for ApObject +where + Inner: OptOriginRef, +{ + fn origin_field_ref(&self) -> &Option> { + self.inner.origin_field_ref() + } +} +impl OptOriginMut for ApObject +where + Inner: OptOriginMut, +{ + fn origin_field_mut(&mut self) -> &mut Option> { + self.inner.origin_field_mut() + } +} + +impl QuestionRef for ApObject +where + Inner: QuestionRef, +{ + fn question_ref(&self) -> &Question { + self.inner.question_ref() + } +} +impl QuestionMut for ApObject +where + Inner: QuestionMut, +{ + fn question_mut(&mut self) -> &mut Question { + self.inner.question_mut() } } diff --git a/src/actor.rs b/src/actor.rs index 1db92d9..aac0949 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -1,7 +1,7 @@ use crate::{ - object::Object, + object::{Object, ObjectMut, ObjectRef}, primitives::{OneOrMany, Unparsed, XsdAnyUri, XsdString}, - traits::{self, Extends, WithUnparsed, WithUnparsedExt}, + traits::{self, Extends, UnparsedMut, UnparsedMutExt}, }; use typed_builder::TypedBuilder; @@ -11,6 +11,189 @@ pub mod kind { use self::kind::*; +pub trait ApActorRef: traits::Actor { + fn ap_actor_ref(&self) -> &ApActor; +} + +pub trait ApActorMut: traits::Actor { + fn ap_actor_mut(&mut self) -> &mut ApActor; +} + +pub trait ApActorRefExt: ApActorRef { + fn inbox<'a>(&'a self) -> &'a XsdAnyUri + where + Inner: 'a, + { + &self.ap_actor_ref().inbox + } + + fn outbox<'a>(&'a self) -> &'a XsdAnyUri + where + Inner: 'a, + { + &self.ap_actor_ref().outbox + } + + fn following<'a>(&'a self) -> Option<&'a XsdAnyUri> + where + Inner: 'a, + { + 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> + 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: ApActorMut { + 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 { + self.ap_actor_mut().following = Some(following); + self + } + + fn take_following(&mut self) -> Option { + self.ap_actor_mut().following.take() + } + + fn delete_following(&mut self) -> &mut Self { + self.ap_actor_mut().following = None; + self + } + + fn set_followers(&mut self, followers: XsdAnyUri) -> &mut Self { + self.ap_actor_mut().followers = Some(followers); + self + } + + fn take_followers(&mut self) -> Option { + self.ap_actor_mut().followers.take() + } + + fn delete_followers(&mut self) -> &mut Self { + self.ap_actor_mut().followers = None; + self + } + + fn set_liked(&mut self, liked: XsdAnyUri) -> &mut Self { + self.ap_actor_mut().liked = Some(liked); + self + } + + fn take_liked(&mut self) -> Option { + self.ap_actor_mut().liked.take() + } + + fn delete_likes(&mut self) -> &mut Self { + self.ap_actor_mut().liked = None; + self + } + + fn set_streams(&mut self, streams: XsdAnyUri) -> &mut Self { + self.ap_actor_mut().streams = Some(streams.into()); + self + } + + fn set_many_streams(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + { + let v: Vec<_> = items.into_iter().collect(); + self.ap_actor_mut().streams = Some(v.into()); + self + } + + fn add_stream(&mut self, stream: XsdAnyUri) -> &mut Self { + let v = match self.ap_actor_mut().streams.take() { + Some(mut v) => { + v.add(stream); + v + } + None => vec![stream].into(), + }; + self.ap_actor_mut().streams = Some(v); + self + } + + fn take_streams(&mut self) -> Option> { + self.ap_actor_mut().streams.take() + } + + fn delete_streams(&mut self) -> &mut Self { + self.ap_actor_mut().streams = None; + self + } + + fn set_preferred_username(&mut self, string: XsdString) -> &mut Self { + self.ap_actor_mut().preferred_username = Some(string); + self + } + + fn take_preferred_username(&mut self) -> Option { + self.ap_actor_mut().preferred_username.take() + } + + fn delete_preferred_username(&mut self) -> &mut Self { + self.ap_actor_mut().preferred_username = None; + self + } + + fn set_endpoints(&mut self, endpoints: Endpoints) -> &mut Self { + self.ap_actor_mut().endpoints = Some(endpoints); + self + } + + fn take_endpoints(&mut self) -> Option { + self.ap_actor_mut().endpoints.take() + } + + fn delete_endpoints(&mut self) -> &mut Self { + self.ap_actor_mut().endpoints = None; + self + } +} + pub type Application = Object; pub type Group = Object; pub type Organization = Object; @@ -83,7 +266,7 @@ pub struct Endpoints { impl ApActor { fn extending(mut inner: Inner) -> Result where - Inner: WithUnparsed + traits::Actor, + Inner: UnparsedMut + traits::Actor, { let inbox = inner.remove("inbox")?; let outbox = inner.remove("outbox")?; @@ -109,7 +292,7 @@ impl ApActor { fn retracting(self) -> Result where - Inner: WithUnparsed + traits::Actor, + Inner: UnparsedMut + traits::Actor, { let ApActor { inbox, @@ -149,7 +332,7 @@ impl traits::Actor for ApActor where Inner: traits::Actor {} impl Extends for ApActor where - Inner: WithUnparsed + traits::Actor, + Inner: UnparsedMut + traits::Actor, { type Error = serde_json::Error; @@ -162,15 +345,48 @@ where } } -impl WithUnparsed for ApActor +impl UnparsedMut for ApActor where - Inner: WithUnparsed, + Inner: UnparsedMut, { - fn unparsed(&self) -> &Unparsed { - self.inner.unparsed() - } - fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } + +impl ObjectRef for ApActor +where + Inner: ObjectRef, +{ + fn object_ref(&self) -> &Object { + self.inner.object_ref() + } +} +impl ObjectMut for ApActor +where + Inner: ObjectMut, +{ + fn object_mut(&mut self) -> &mut Object { + self.inner.object_mut() + } +} + +impl ApActorRef for ApActor +where + Inner: traits::Actor, +{ + fn ap_actor_ref(&self) -> &ApActor { + self + } +} +impl ApActorMut for ApActor +where + Inner: traits::Actor, +{ + fn ap_actor_mut(&mut self) -> &mut ApActor { + self + } +} + +impl ApActorRefExt for T where T: ApActorRef {} +impl ApActorMutExt for T where T: ApActorMut {} diff --git a/src/collection.rs b/src/collection.rs index 0430ad6..8e009c8 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -1,7 +1,7 @@ use crate::{ - object::{AnyObject, Object}, + object::{AnyObject, ApObject, Object, ObjectMut, ObjectRef}, primitives::{OneOrMany, Unparsed, XsdNonNegativeInteger}, - traits::{self, Extends, WithUnparsed, WithUnparsedExt}, + traits::{self, Extends, UnparsedMut, UnparsedMutExt}, }; use std::convert::TryFrom; use typed_builder::TypedBuilder; @@ -12,6 +12,264 @@ pub mod 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; @@ -168,100 +426,72 @@ impl OrderedCollectionPage { } } -impl traits::Base for Collection where Kind: std::fmt::Debug {} -impl traits::Object for Collection where Kind: std::fmt::Debug {} -impl traits::Collection for Collection where Kind: std::fmt::Debug {} +impl traits::Base for Collection {} +impl traits::Object for Collection {} +impl traits::Collection for Collection {} -impl traits::Base for CollectionPage where Kind: std::fmt::Debug {} -impl traits::Object for CollectionPage where Kind: std::fmt::Debug {} -impl traits::Collection for CollectionPage where Kind: std::fmt::Debug {} -impl traits::CollectionPage for CollectionPage where Kind: std::fmt::Debug {} +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 UnorderedCollection { +impl Extends> for Collection { type Error = serde_json::Error; - fn extends(object: Object) -> Result { + fn extends(object: Object) -> Result { Self::extending(object) } - fn retracts(self) -> Result, Self::Error> { + fn retracts(self) -> Result, Self::Error> { self.retracting() } } -impl TryFrom> for UnorderedCollection { +impl TryFrom> for Object { 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: UnorderedCollection) -> Result { + fn try_from(collection: Collection) -> Result { collection.retracting() } } -impl Extends> for OrderedCollection { +impl TryFrom> for Collection { type Error = serde_json::Error; - fn extends(object: Object) -> Result { + 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> { + fn retracts(self) -> Result, Self::Error> { self.retracting() } } -impl TryFrom> for OrderedCollection { +impl TryFrom> for CollectionPage { type Error = serde_json::Error; - fn try_from(object: Object) -> Result { + fn try_from(object: Object) -> Result { Self::extending(object) } } -impl TryFrom for Object { +impl TryFrom> for Object { type Error = serde_json::Error; - fn try_from(collection: OrderedCollection) -> Result { - collection.retracting() - } -} - -impl Extends> for UnorderedCollectionPage { - type Error = serde_json::Error; - - fn extends(object: Object) -> Result { - Self::extending(object) - } - - fn retracts(self) -> Result, Self::Error> { - self.retracting() - } -} - -impl TryFrom> for UnorderedCollectionPage { - 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: UnorderedCollectionPage) -> Result { + fn try_from(collection_page: CollectionPage) -> Result { collection_page.retracting() } } @@ -294,32 +524,171 @@ impl TryFrom for Object { } } -impl WithUnparsed for Collection { - fn unparsed(&self) -> &Unparsed { - self.inner.unparsed() - } - +impl UnparsedMut for Collection { fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } -impl WithUnparsed for CollectionPage { - fn unparsed(&self) -> &Unparsed { - self.inner.unparsed() - } - +impl UnparsedMut for CollectionPage { fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } -impl WithUnparsed for OrderedCollectionPage { - fn unparsed(&self) -> &Unparsed { - self.inner.unparsed() - } - +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 {} diff --git a/src/lib.rs b/src/lib.rs index a090ae1..acf94b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,3 +7,23 @@ pub mod primitives; pub mod traits; pub use activitystreams::{context, public, security}; + +pub mod prelude { + pub use crate::{ + activity::{ + ActivityMutExt, ActivityRefExt, ActorAndObjectMutExt, ActorAndObjectRefExt, + OptOriginMutExt, OptOriginRefExt, OptTargetMutExt, OptTargetRefExt, OriginMutExt, + 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, + }, + }; +} diff --git a/src/object.rs b/src/object.rs index 25cef97..467abb0 100644 --- a/src/object.rs +++ b/src/object.rs @@ -4,7 +4,7 @@ use crate::{ AnyString, MimeMediaType, OneOrMany, Unit, Unparsed, XsdAnyUri, XsdDateTime, XsdDuration, XsdFloat, XsdString, }, - traits::{self, Extends, WithUnparsed, WithUnparsedExt}, + traits::{self, Extends, UnparsedMut, UnparsedMutExt}, }; use std::convert::TryFrom; use typed_builder::TypedBuilder; @@ -15,6 +15,1559 @@ pub mod kind { use self::kind::*; +pub trait ObjectRef: traits::Object { + fn object_ref(&self) -> &Object; +} + +pub trait ObjectMut: traits::Object { + fn object_mut(&mut self) -> &mut Object; +} + +pub trait ApObjectRef: traits::Object { + fn ap_object_ref(&self) -> &ApObject; +} + +pub trait ApObjectMut: traits::Object { + fn ap_object_mut(&mut self) -> &mut ApObject; +} + +pub trait PlaceRef: traits::Object { + fn place_ref(&self) -> &Place; +} + +pub trait PlaceMut: traits::Object { + fn place_mut(&mut self) -> &mut Place; +} + +pub trait ProfileRef: traits::Object { + fn profile_ref(&self) -> &Profile; +} + +pub trait ProfileMut: traits::Object { + fn profile_mut(&mut self) -> &mut Profile; +} + +pub trait RelationshipRef: traits::Object { + fn relationship_ref(&self) -> &Relationship; +} + +pub trait RelationshipMut: traits::Object { + fn relationship_mut(&mut self) -> &mut Relationship; +} + +pub trait TombstoneRef: traits::Object { + fn tombstone_ref(&self) -> &Tombstone; +} + +pub trait TombstoneMut: traits::Object { + fn tombstone_mut(&mut self) -> &mut Tombstone; +} + +pub trait ObjectRefExt: ObjectRef { + fn context<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().context.as_ref() + } + + fn id<'a>(&'a self) -> Option<&'a XsdAnyUri> + where + Kind: 'a, + { + self.object_ref().id.as_ref() + } + + fn kind<'a>(&'a self) -> Option<&'a Kind> + where + Kind: 'a, + { + self.object_ref().kind.as_ref() + } + + fn attachment<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().attachment.as_ref() + } + + fn attributed_to<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().attributed_to.as_ref() + } + + fn audience<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().audience.as_ref() + } + + fn content<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().content.as_ref() + } + + fn name<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().name.as_ref() + } + + fn summary<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().summary.as_ref() + } + + fn url<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().url.as_ref() + } + + fn media_type<'a>(&'a self) -> Option<&'a MimeMediaType> + where + Kind: 'a, + { + self.object_ref().media_type.as_ref() + } + + fn generator<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().generator.as_ref() + } + + fn image<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().image.as_ref() + } + + fn location<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().location.as_ref() + } + + fn preview<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().preview.as_ref() + } + + fn start_time<'a>(&'a self) -> Option<&'a XsdDateTime> + where + Kind: 'a, + { + self.object_ref().start_time.as_ref() + } + + fn end_time<'a>(&'a self) -> Option<&'a XsdDateTime> + where + Kind: 'a, + { + self.object_ref().end_time.as_ref() + } + + fn duration<'a>(&'a self) -> Option<&'a XsdDuration> + where + Kind: 'a, + { + self.object_ref().duration.as_ref() + } + + fn published<'a>(&'a self) -> Option<&'a XsdDateTime> + where + Kind: 'a, + { + self.object_ref().published.as_ref() + } + + fn updated<'a>(&'a self) -> Option<&'a XsdDateTime> + where + Kind: 'a, + { + self.object_ref().updated.as_ref() + } + + fn in_reply_to<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().in_reply_to.as_ref() + } + + fn replies<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().replies.as_ref() + } + + fn to<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().to.as_ref() + } + + fn bto<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().bto.as_ref() + } + + fn cc<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().cc.as_ref() + } + + fn bcc<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().bcc.as_ref() + } +} + +pub trait ObjectMutExt: ObjectMut { + fn set_context(&mut self, context: T) -> &mut Self + where + T: Into, + { + self.object_mut().context = Some(context.into().into()); + self + } + + fn set_many_contexts(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.object_mut().context = Some(v.into()); + self + } + + fn add_context(&mut self, context: T) -> &mut Self + where + T: Into, + { + let c = match self.object_mut().context.take() { + Some(mut c) => { + c.add(context.into()); + c + } + None => vec![context.into()].into(), + }; + self.object_mut().context = Some(c); + self + } + + fn take_context(&mut self) -> Option> { + self.object_mut().context.take() + } + + fn delete_context(&mut self) -> &mut Self { + self.object_mut().context = None; + self + } + + fn set_id(&mut self, id: XsdAnyUri) -> &mut Self { + self.object_mut().id = Some(id); + self + } + + fn take_id(&mut self) -> Option { + self.object_mut().id.take() + } + + fn delete_id(&mut self) -> &mut Self { + self.object_mut().id = None; + self + } + + fn set_kind(&mut self, kind: Kind) -> &mut Self { + self.object_mut().kind = Some(kind); + self + } + + fn take_kind(&mut self) -> Option { + self.object_mut().kind.take() + } + + fn delete_kind(&mut self) -> &mut Self { + self.object_mut().kind = None; + self + } + + fn set_attachment(&mut self, attachment: T) -> &mut Self + where + T: Into, + { + self.object_mut().attachment = Some(attachment.into().into()); + self + } + + fn set_many_attachments(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.object_mut().attachment = Some(v.into()); + self + } + + fn add_attachment(&mut self, attachment: T) -> &mut Self + where + T: Into, + { + let a = match self.object_mut().attachment.take() { + Some(mut a) => { + a.add(attachment.into()); + a + } + None => vec![attachment.into()].into(), + }; + self.object_mut().attachment = Some(a); + self + } + + fn take_attachment(&mut self) -> Option> { + self.object_mut().attachment.take() + } + + fn delete_attachment(&mut self) -> &mut Self { + self.object_mut().attachment = None; + self + } + + fn set_attributed_to(&mut self, attributed_to: T) -> &mut Self + where + T: Into, + { + self.object_mut().attributed_to = Some(attributed_to.into().into()); + self + } + + fn set_many_attributed_tos(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.object_mut().attributed_to = Some(v.into()); + self + } + + fn add_attributed_to(&mut self, attributed_to: T) -> &mut Self + where + T: Into, + { + let a = match self.object_mut().attributed_to.take() { + Some(mut a) => { + a.add(attributed_to.into()); + a + } + None => vec![attributed_to.into()].into(), + }; + self.object_mut().attributed_to = Some(a); + self + } + + fn take_attributed_to(&mut self) -> Option> { + self.object_mut().attributed_to.take() + } + + fn delete_attributed_to(&mut self) -> &mut Self { + self.object_mut().attributed_to = None; + self + } + + fn set_audience(&mut self, audience: T) -> &mut Self + where + T: Into, + { + self.object_mut().audience = Some(audience.into().into()); + self + } + + fn set_many_audiences(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.object_mut().audience = Some(v.into()); + self + } + + fn add_audience(&mut self, audience: T) -> &mut Self + where + T: Into, + { + let a = match self.object_mut().audience.take() { + Some(mut a) => { + a.add(audience.into()); + a + } + None => vec![audience.into()].into(), + }; + self.object_mut().audience = Some(a); + self + } + + fn take_audience(&mut self) -> Option> { + self.object_mut().audience.take() + } + + fn delete_audience(&mut self) -> &mut Self { + self.object_mut().audience = None; + self + } + + fn set_content(&mut self, content: T) -> &mut Self + where + T: Into, + { + self.object_mut().content = Some(content.into().into()); + self + } + + fn set_many_contents(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.object_mut().content = Some(v.into()); + self + } + + fn add_content(&mut self, content: T) -> &mut Self + where + T: Into, + { + let a = match self.object_mut().content.take() { + Some(mut a) => { + a.add(content.into()); + a + } + None => vec![content.into()].into(), + }; + self.object_mut().content = Some(a); + self + } + + fn take_content(&mut self) -> Option> { + self.object_mut().content.take() + } + + fn delete_content(&mut self) -> &mut Self { + self.object_mut().content = None; + self + } + + fn set_name(&mut self, name: T) -> &mut Self + where + T: Into, + { + self.object_mut().name = Some(name.into().into()); + self + } + + fn set_many_names(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.object_mut().name = Some(v.into()); + self + } + + fn add_name(&mut self, name: T) -> &mut Self + where + T: Into, + { + let a = match self.object_mut().name.take() { + Some(mut a) => { + a.add(name.into()); + a + } + None => vec![name.into()].into(), + }; + self.object_mut().name = Some(a); + self + } + + fn take_name(&mut self) -> Option> { + self.object_mut().name.take() + } + + fn delete_name(&mut self) -> &mut Self { + self.object_mut().name = None; + self + } + + fn set_summary(&mut self, summary: T) -> &mut Self + where + T: Into, + { + self.object_mut().summary = Some(summary.into().into()); + self + } + + fn set_many_summarys(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.object_mut().summary = Some(v.into()); + self + } + + fn add_summary(&mut self, summary: T) -> &mut Self + where + T: Into, + { + let a = match self.object_mut().summary.take() { + Some(mut a) => { + a.add(summary.into()); + a + } + None => vec![summary.into()].into(), + }; + self.object_mut().summary = Some(a); + self + } + + fn take_summary(&mut self) -> Option> { + self.object_mut().summary.take() + } + + fn delete_summary(&mut self) -> &mut Self { + self.object_mut().summary = None; + self + } + + fn set_url(&mut self, url: T) -> &mut Self + where + T: Into, + { + self.object_mut().url = Some(url.into().into()); + self + } + + fn set_many_urls(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.object_mut().url = Some(v.into()); + self + } + + fn add_url(&mut self, url: T) -> &mut Self + where + T: Into, + { + let a = match self.object_mut().url.take() { + Some(mut a) => { + a.add(url.into()); + a + } + None => vec![url.into()].into(), + }; + self.object_mut().url = Some(a); + self + } + + fn take_url(&mut self) -> Option> { + self.object_mut().url.take() + } + + fn delete_url(&mut self) -> &mut Self { + self.object_mut().url = None; + self + } + + fn set_media_type(&mut self, media_type: MimeMediaType) -> &mut Self { + self.object_mut().media_type = Some(media_type); + self + } + + fn take_media_type(&mut self) -> Option { + self.object_mut().media_type.take() + } + + fn delete_media_type(&mut self) -> &mut Self { + self.object_mut().media_type = None; + self + } + + fn set_generator(&mut self, generator: T) -> &mut Self + where + T: Into, + { + self.object_mut().generator = Some(generator.into().into()); + self + } + + fn set_many_generators(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.object_mut().generator = Some(v.into()); + self + } + + fn add_generator(&mut self, generator: T) -> &mut Self + where + T: Into, + { + let a = match self.object_mut().generator.take() { + Some(mut a) => { + a.add(generator.into()); + a + } + None => vec![generator.into()].into(), + }; + self.object_mut().generator = Some(a); + self + } + + fn take_generator(&mut self) -> Option> { + self.object_mut().generator.take() + } + + fn delete_generator(&mut self) -> &mut Self { + self.object_mut().generator = None; + self + } + + fn set_image(&mut self, image: T) -> &mut Self + where + T: Into, + { + self.object_mut().image = Some(image.into().into()); + self + } + + fn set_many_images(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.object_mut().image = Some(v.into()); + self + } + + fn add_image(&mut self, image: T) -> &mut Self + where + T: Into, + { + let a = match self.object_mut().image.take() { + Some(mut a) => { + a.add(image.into()); + a + } + None => vec![image.into()].into(), + }; + self.object_mut().image = Some(a); + self + } + + fn take_image(&mut self) -> Option> { + self.object_mut().image.take() + } + + fn delete_image(&mut self) -> &mut Self { + self.object_mut().image = None; + self + } + + fn set_location(&mut self, location: T) -> &mut Self + where + T: Into, + { + self.object_mut().location = Some(location.into().into()); + self + } + + fn set_many_locations(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.object_mut().location = Some(v.into()); + self + } + + fn add_location(&mut self, location: T) -> &mut Self + where + T: Into, + { + let a = match self.object_mut().location.take() { + Some(mut a) => { + a.add(location.into()); + a + } + None => vec![location.into()].into(), + }; + self.object_mut().location = Some(a); + self + } + + fn take_location(&mut self) -> Option> { + self.object_mut().location.take() + } + + fn delete_location(&mut self) -> &mut Self { + self.object_mut().location = None; + self + } + + fn set_preview(&mut self, preview: T) -> &mut Self + where + T: Into, + { + self.object_mut().preview = Some(preview.into().into()); + self + } + + fn set_many_previews(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.object_mut().preview = Some(v.into()); + self + } + + fn add_preview(&mut self, preview: T) -> &mut Self + where + T: Into, + { + let a = match self.object_mut().preview.take() { + Some(mut a) => { + a.add(preview.into()); + a + } + None => vec![preview.into()].into(), + }; + self.object_mut().preview = Some(a); + self + } + + fn take_preview(&mut self) -> Option> { + self.object_mut().preview.take() + } + + fn delete_preview(&mut self) -> &mut Self { + self.object_mut().preview = None; + self + } + + fn set_start_time(&mut self, start_time: XsdDateTime) -> &mut Self { + self.object_mut().start_time = Some(start_time); + self + } + + fn take_start_time(&mut self) -> Option { + self.object_mut().start_time.take() + } + + fn delete_start_time(&mut self) -> &mut Self { + self.object_mut().start_time = None; + self + } + + fn set_end_time(&mut self, end_time: XsdDateTime) -> &mut Self { + self.object_mut().end_time = Some(end_time); + self + } + + fn take_end_time(&mut self) -> Option { + self.object_mut().end_time.take() + } + + fn delete_end_time(&mut self) -> &mut Self { + self.object_mut().end_time = None; + self + } + + fn set_duration(&mut self, duration: XsdDuration) -> &mut Self { + self.object_mut().duration = Some(duration); + self + } + + fn take_duration(&mut self) -> Option { + self.object_mut().duration.take() + } + + fn delete_duration(&mut self) -> &mut Self { + self.object_mut().duration = None; + self + } + + fn set_published(&mut self, published: XsdDateTime) -> &mut Self { + self.object_mut().published = Some(published); + self + } + + fn take_published(&mut self) -> Option { + self.object_mut().published.take() + } + + fn delete_published(&mut self) -> &mut Self { + self.object_mut().published = None; + self + } + + fn set_updated(&mut self, updated: XsdDateTime) -> &mut Self { + self.object_mut().updated = Some(updated); + self + } + + fn take_updated(&mut self) -> Option { + self.object_mut().updated.take() + } + + fn delete_updated(&mut self) -> &mut Self { + self.object_mut().updated = None; + self + } + + fn set_in_reply_to(&mut self, in_reply_to: T) -> &mut Self + where + T: Into, + { + self.object_mut().in_reply_to = Some(in_reply_to.into().into()); + self + } + + fn set_many_in_reply_tos(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.object_mut().in_reply_to = Some(v.into()); + self + } + + fn add_in_reply_to(&mut self, in_reply_to: T) -> &mut Self + where + T: Into, + { + let a = match self.object_mut().in_reply_to.take() { + Some(mut a) => { + a.add(in_reply_to.into()); + a + } + None => vec![in_reply_to.into()].into(), + }; + self.object_mut().in_reply_to = Some(a); + self + } + + fn take_in_reply_to(&mut self) -> Option> { + self.object_mut().in_reply_to.take() + } + + fn delete_in_reply_to(&mut self) -> &mut Self { + self.object_mut().in_reply_to = None; + self + } + + fn set_replies(&mut self, replies: T) -> &mut Self + where + T: Into, + { + self.object_mut().replies = Some(replies.into().into()); + self + } + + fn set_many_repliess(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.object_mut().replies = Some(v.into()); + self + } + + fn add_replies(&mut self, replies: T) -> &mut Self + where + T: Into, + { + let a = match self.object_mut().replies.take() { + Some(mut a) => { + a.add(replies.into()); + a + } + None => vec![replies.into()].into(), + }; + self.object_mut().replies = Some(a); + self + } + + fn take_replies(&mut self) -> Option> { + self.object_mut().replies.take() + } + + fn delete_replies(&mut self) -> &mut Self { + self.object_mut().replies = None; + self + } + + fn set_to(&mut self, to: T) -> &mut Self + where + T: Into, + { + self.object_mut().to = Some(to.into().into()); + self + } + + fn set_many_tos(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.object_mut().to = Some(v.into()); + self + } + + fn add_to(&mut self, to: T) -> &mut Self + where + T: Into, + { + let a = match self.object_mut().to.take() { + Some(mut a) => { + a.add(to.into()); + a + } + None => vec![to.into()].into(), + }; + self.object_mut().to = Some(a); + self + } + + fn take_to(&mut self) -> Option> { + self.object_mut().to.take() + } + + fn delete_to(&mut self) -> &mut Self { + self.object_mut().to = None; + self + } + + fn set_bto(&mut self, bto: T) -> &mut Self + where + T: Into, + { + self.object_mut().bto = Some(bto.into().into()); + self + } + + fn set_many_btos(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.object_mut().bto = Some(v.into()); + self + } + + fn add_bto(&mut self, bto: T) -> &mut Self + where + T: Into, + { + let a = match self.object_mut().bto.take() { + Some(mut a) => { + a.add(bto.into()); + a + } + None => vec![bto.into()].into(), + }; + self.object_mut().bto = Some(a); + self + } + + fn take_bto(&mut self) -> Option> { + self.object_mut().bto.take() + } + + fn delete_bto(&mut self) -> &mut Self { + self.object_mut().bto = None; + self + } + + fn set_cc(&mut self, cc: T) -> &mut Self + where + T: Into, + { + self.object_mut().cc = Some(cc.into().into()); + self + } + + fn set_many_ccs(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.object_mut().cc = Some(v.into()); + self + } + + fn add_cc(&mut self, cc: T) -> &mut Self + where + T: Into, + { + let a = match self.object_mut().cc.take() { + Some(mut a) => { + a.add(cc.into()); + a + } + None => vec![cc.into()].into(), + }; + self.object_mut().cc = Some(a); + self + } + + fn take_cc(&mut self) -> Option> { + self.object_mut().cc.take() + } + + fn delete_cc(&mut self) -> &mut Self { + self.object_mut().cc = None; + self + } + + fn set_bcc(&mut self, bcc: T) -> &mut Self + where + T: Into, + { + self.object_mut().bcc = Some(bcc.into().into()); + self + } + + fn set_many_bcc(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.object_mut().bcc = Some(v.into()); + self + } + + fn add_bcc(&mut self, bcc: T) -> &mut Self + where + T: Into, + { + let a = match self.object_mut().bcc.take() { + Some(mut a) => { + a.add(bcc.into()); + a + } + None => vec![bcc.into()].into(), + }; + self.object_mut().bcc = Some(a); + self + } + + fn take_bcc(&mut self) -> Option> { + self.object_mut().bcc.take() + } + + fn delete_bcc(&mut self) -> &mut Self { + self.object_mut().bcc = None; + self + } +} + +pub trait ApObjectRefExt: ApObjectRef { + fn shares<'a>(&'a self) -> Option<&'a XsdAnyUri> + where + Inner: 'a, + { + self.ap_object_ref().shares.as_ref() + } + + fn likes<'a>(&'a self) -> Option<&'a XsdAnyUri> + where + Inner: 'a, + { + self.ap_object_ref().likes.as_ref() + } + + fn source<'a>(&'a self) -> Option<&'a AnyObject> + where + Inner: 'a, + { + self.ap_object_ref().source.as_ref() + } + + fn upload_media<'a>(&'a self) -> Option<&'a OneOrMany> + where + Inner: 'a, + { + self.ap_object_ref().upload_media.as_ref() + } +} + +pub trait ApObjectMutExt: ApObjectMut { + fn set_shares(&mut self, shares: XsdAnyUri) -> &mut Self { + self.ap_object_mut().shares = Some(shares); + self + } + + fn take_shares(&mut self) -> Option { + self.ap_object_mut().shares.take() + } + + fn delete_shares(&mut self) -> &mut Self { + self.ap_object_mut().shares = None; + self + } + + fn set_likes(&mut self, likes: XsdAnyUri) -> &mut Self { + self.ap_object_mut().likes = Some(likes); + self + } + + fn take_likes(&mut self) -> Option { + self.ap_object_mut().likes.take() + } + + fn delete_likes(&mut self) -> &mut Self { + self.ap_object_mut().likes = None; + self + } + + fn set_source(&mut self, source: T) -> &mut Self + where + T: Into, + { + self.ap_object_mut().source = Some(source.into()); + self + } + + fn take_source(&mut self) -> Option { + self.ap_object_mut().source.take() + } + + fn delete_source(&mut self) -> &mut Self { + self.ap_object_mut().source = None; + self + } + + fn set_upload_media(&mut self, upload_media: XsdAnyUri) -> &mut Self { + self.ap_object_mut().upload_media = Some(upload_media.into()); + self + } + + fn set_many_upload_medias(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + { + let v: Vec<_> = items.into_iter().collect(); + self.ap_object_mut().upload_media = Some(v.into()); + self + } + + fn add_upload_media(&mut self, upload_media: XsdAnyUri) -> &mut Self { + let v = match self.ap_object_mut().upload_media.take() { + Some(mut v) => { + v.add(upload_media); + v + } + None => vec![upload_media].into(), + }; + self.ap_object_mut().upload_media = Some(v); + self + } + + fn take_upload_media(&mut self) -> Option> { + self.ap_object_mut().upload_media.take() + } + + fn delete_upload_media(&mut self) -> &mut Self { + self.ap_object_mut().upload_media = None; + self + } +} + +pub trait PlaceRefExt: PlaceRef { + fn accuracy(&self) -> Option<&XsdFloat> { + self.place_ref().accuracy.as_ref() + } + + fn altitude(&self) -> Option<&XsdFloat> { + self.place_ref().altitude.as_ref() + } + + fn latitude(&self) -> Option<&XsdFloat> { + self.place_ref().latitude.as_ref() + } + + fn longitude(&self) -> Option<&XsdFloat> { + self.place_ref().longitude.as_ref() + } + + fn units(&self) -> Option<&Unit> { + self.place_ref().units.as_ref() + } +} + +pub trait PlaceMutExt: PlaceMut { + fn set_accuracy(&mut self, float: T) -> &mut Self + where + T: Into, + { + self.place_mut().accuracy = Some(float.into()); + self + } + + fn take_accuracy(&mut self) -> Option { + self.place_mut().accuracy.take() + } + + fn delete_accuracy(&mut self) -> &mut Self { + self.place_mut().accuracy = None; + self + } + + fn set_altitude(&mut self, float: T) -> &mut Self + where + T: Into, + { + self.place_mut().altitude = Some(float.into()); + self + } + + fn take_altitude(&mut self) -> Option { + self.place_mut().altitude.take() + } + + fn delete_altitude(&mut self) -> &mut Self { + self.place_mut().altitude = None; + self + } + + fn set_latitude(&mut self, float: T) -> &mut Self + where + T: Into, + { + self.place_mut().latitude = Some(float.into()); + self + } + + fn take_latitude(&mut self) -> Option { + self.place_mut().latitude.take() + } + + fn delete_latitude(&mut self) -> &mut Self { + self.place_mut().latitude = None; + self + } + + fn set_longitude(&mut self, float: T) -> &mut Self + where + T: Into, + { + self.place_mut().longitude = Some(float.into()); + self + } + + fn take_longitude(&mut self) -> Option { + self.place_mut().longitude.take() + } + + fn delete_longitude(&mut self) -> &mut Self { + self.place_mut().longitude = None; + self + } + + fn set_radius(&mut self, float: T) -> &mut Self + where + T: Into, + { + self.place_mut().radius = Some(float.into()); + self + } + + fn take_radius(&mut self) -> Option { + self.place_mut().radius.take() + } + + fn delete_radius(&mut self) -> &mut Self { + self.place_mut().radius = None; + self + } + + fn set_units(&mut self, units: T) -> &mut Self + where + T: Into, + { + self.place_mut().units = Some(units.into()); + self + } + + fn take_units(&mut self) -> Option { + self.place_mut().units.take() + } + + fn delete_units(&mut self) -> &mut Self { + self.place_mut().units = None; + self + } +} + +pub trait ProfileRefExt: ProfileRef { + fn describes(&self) -> Option<&AnyObject> { + self.profile_ref().describes.as_ref() + } +} + +pub trait ProfileMutExt: ProfileMut { + fn set_describes(&mut self, describes: T) -> &mut Self + where + T: Into, + { + self.profile_mut().describes = Some(describes.into()); + self + } + + fn take_describes(&mut self) -> Option { + self.profile_mut().describes.take() + } + + fn delete_describes(&mut self) -> &mut Self { + self.profile_mut().describes = None; + self + } +} + +pub trait RelationshipRefExt: RelationshipRef { + fn subject(&self) -> Option<&AnyObject> { + self.relationship_ref().subject.as_ref() + } + + fn object(&self) -> Option<&OneOrMany> { + self.relationship_ref().object.as_ref() + } + + fn relationship(&self) -> Option<&OneOrMany> { + self.relationship_ref().relationship.as_ref() + } +} + +pub trait RelationshipMutExt: RelationshipMut { + fn set_subject(&mut self, subject: T) -> &mut Self + where + T: Into, + { + self.relationship_mut().subject = Some(subject.into()); + self + } + + fn take_subject(&mut self) -> Option { + self.relationship_mut().subject.take() + } + + fn delete_subject(&mut self) -> &mut Self { + self.relationship_mut().subject = None; + self + } + + fn set_object(&mut self, object: T) -> &mut Self + where + T: Into, + { + self.relationship_mut().object = Some(object.into().into()); + self + } + + fn set_many_objects(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.relationship_mut().object = Some(v.into()); + self + } + + fn add_object(&mut self, object: T) -> &mut Self + where + T: Into, + { + let v = match self.relationship_mut().object.take() { + Some(mut v) => { + v.add(object.into()); + v + } + None => vec![object.into()].into(), + }; + self.relationship_mut().object = Some(v); + self + } + + fn take_object(&mut self) -> Option> { + self.relationship_mut().object.take() + } + + fn delete_object(&mut self) -> &mut Self { + self.relationship_mut().object = None; + self + } + + fn set_relationship(&mut self, relationship: T) -> &mut Self + where + T: Into, + { + self.relationship_mut().relationship = Some(relationship.into().into()); + self + } + + fn set_many_relationships(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.relationship_mut().relationship = Some(v.into()); + self + } + + fn add_relationship(&mut self, relationship: T) -> &mut Self + where + T: Into, + { + let v = match self.relationship_mut().relationship.take() { + Some(mut v) => { + v.add(relationship.into()); + v + } + None => vec![relationship.into()].into(), + }; + self.relationship_mut().relationship = Some(v); + self + } + + fn take_relationship(&mut self) -> Option> { + self.relationship_mut().relationship.take() + } + + fn delete_relationship(&mut self) -> &mut Self { + self.relationship_mut().relationship = None; + self + } +} + +pub trait TombstoneRefExt: TombstoneRef { + fn former_type(&self) -> Option<&OneOrMany> { + self.tombstone_ref().former_type.as_ref() + } + + fn deleted(&self) -> Option<&XsdDateTime> { + self.tombstone_ref().deleted.as_ref() + } +} + +pub trait TombstoneMutExt: TombstoneMut { + fn set_former_type(&mut self, former_type: T) -> &mut Self + where + T: Into, + { + self.tombstone_mut().former_type = Some(former_type.into().into()); + self + } + + fn set_many_former_types(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + T: Into, + { + let v: Vec<_> = items.into_iter().map(Into::into).collect(); + self.tombstone_mut().former_type = Some(v.into()); + self + } + + fn add_former_type(&mut self, former_type: T) -> &mut Self + where + T: Into, + { + let v = match self.tombstone_mut().former_type.take() { + Some(mut v) => { + v.add(former_type.into()); + v + } + None => vec![former_type.into()].into(), + }; + self.tombstone_mut().former_type = Some(v); + self + } + + fn take_former_type(&mut self) -> Option> { + self.tombstone_mut().former_type.take() + } + + fn delete_former_type(&mut self) -> &mut Self { + self.tombstone_mut().former_type = None; + self + } + + fn set_deleted(&mut self, deleted: T) -> &mut Self + where + T: Into, + { + self.tombstone_mut().deleted = Some(deleted.into()); + self + } + + fn take_deleted(&mut self) -> Option { + self.tombstone_mut().deleted.take() + } + + fn delete_deleted(&mut self) -> &mut Self { + self.tombstone_mut().deleted = None; + self + } +} + pub type Article = Object; pub type Audio = Object; pub type Document = Object; @@ -42,7 +1595,7 @@ pub struct Object { #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub id: Option, + pub id: Option, #[serde(rename = "type")] #[serde(skip_serializing_if = "Option::is_none")] @@ -402,32 +1955,32 @@ impl Object { Kind: serde::de::DeserializeOwned, { Ok(Object { - kind: WithUnparsedExt::remove(&mut unparsed, "type")?, - context: WithUnparsedExt::remove(&mut unparsed, "context")?, - id: WithUnparsedExt::remove(&mut unparsed, "id")?, - attachment: WithUnparsedExt::remove(&mut unparsed, "attachment")?, - attributed_to: WithUnparsedExt::remove(&mut unparsed, "attributedTo")?, - audience: WithUnparsedExt::remove(&mut unparsed, "audience")?, - content: WithUnparsedExt::remove(&mut unparsed, "content")?, - name: WithUnparsedExt::remove(&mut unparsed, "name")?, - summary: WithUnparsedExt::remove(&mut unparsed, "summary")?, - url: WithUnparsedExt::remove(&mut unparsed, "url")?, - media_type: WithUnparsedExt::remove(&mut unparsed, "mediaType")?, - generator: WithUnparsedExt::remove(&mut unparsed, "generator")?, - image: WithUnparsedExt::remove(&mut unparsed, "image")?, - location: WithUnparsedExt::remove(&mut unparsed, "location")?, - preview: WithUnparsedExt::remove(&mut unparsed, "preview")?, - start_time: WithUnparsedExt::remove(&mut unparsed, "startTime")?, - end_time: WithUnparsedExt::remove(&mut unparsed, "endTime")?, - duration: WithUnparsedExt::remove(&mut unparsed, "duration")?, - published: WithUnparsedExt::remove(&mut unparsed, "published")?, - updated: WithUnparsedExt::remove(&mut unparsed, "updated")?, - in_reply_to: WithUnparsedExt::remove(&mut unparsed, "inReplyTo")?, - replies: WithUnparsedExt::remove(&mut unparsed, "replies")?, - to: WithUnparsedExt::remove(&mut unparsed, "to")?, - bto: WithUnparsedExt::remove(&mut unparsed, "bto")?, - cc: WithUnparsedExt::remove(&mut unparsed, "cc")?, - bcc: WithUnparsedExt::remove(&mut unparsed, "bcc")?, + kind: UnparsedMutExt::remove(&mut unparsed, "type")?, + context: UnparsedMutExt::remove(&mut unparsed, "context")?, + id: UnparsedMutExt::remove(&mut unparsed, "id")?, + attachment: UnparsedMutExt::remove(&mut unparsed, "attachment")?, + attributed_to: UnparsedMutExt::remove(&mut unparsed, "attributedTo")?, + audience: UnparsedMutExt::remove(&mut unparsed, "audience")?, + content: UnparsedMutExt::remove(&mut unparsed, "content")?, + name: UnparsedMutExt::remove(&mut unparsed, "name")?, + summary: UnparsedMutExt::remove(&mut unparsed, "summary")?, + url: UnparsedMutExt::remove(&mut unparsed, "url")?, + media_type: UnparsedMutExt::remove(&mut unparsed, "mediaType")?, + generator: UnparsedMutExt::remove(&mut unparsed, "generator")?, + image: UnparsedMutExt::remove(&mut unparsed, "image")?, + location: UnparsedMutExt::remove(&mut unparsed, "location")?, + preview: UnparsedMutExt::remove(&mut unparsed, "preview")?, + start_time: UnparsedMutExt::remove(&mut unparsed, "startTime")?, + end_time: UnparsedMutExt::remove(&mut unparsed, "endTime")?, + duration: UnparsedMutExt::remove(&mut unparsed, "duration")?, + published: UnparsedMutExt::remove(&mut unparsed, "published")?, + updated: UnparsedMutExt::remove(&mut unparsed, "updated")?, + in_reply_to: UnparsedMutExt::remove(&mut unparsed, "inReplyTo")?, + replies: UnparsedMutExt::remove(&mut unparsed, "replies")?, + to: UnparsedMutExt::remove(&mut unparsed, "to")?, + bto: UnparsedMutExt::remove(&mut unparsed, "bto")?, + cc: UnparsedMutExt::remove(&mut unparsed, "cc")?, + bcc: UnparsedMutExt::remove(&mut unparsed, "bcc")?, unparsed, }) } @@ -466,32 +2019,32 @@ impl Object { mut unparsed, } = self; - WithUnparsedExt::insert(&mut unparsed, "type", kind)?; - WithUnparsedExt::insert(&mut unparsed, "context", context)?; - WithUnparsedExt::insert(&mut unparsed, "id", id)?; - WithUnparsedExt::insert(&mut unparsed, "attachment", attachment)?; - WithUnparsedExt::insert(&mut unparsed, "attributedTo", attributed_to)?; - WithUnparsedExt::insert(&mut unparsed, "audience", audience)?; - WithUnparsedExt::insert(&mut unparsed, "content", content)?; - WithUnparsedExt::insert(&mut unparsed, "name", name)?; - WithUnparsedExt::insert(&mut unparsed, "summary", summary)?; - WithUnparsedExt::insert(&mut unparsed, "url", url)?; - WithUnparsedExt::insert(&mut unparsed, "mediaType", media_type)?; - WithUnparsedExt::insert(&mut unparsed, "generator", generator)?; - WithUnparsedExt::insert(&mut unparsed, "image", image)?; - WithUnparsedExt::insert(&mut unparsed, "location", location)?; - WithUnparsedExt::insert(&mut unparsed, "preview", preview)?; - WithUnparsedExt::insert(&mut unparsed, "startTime", start_time)?; - WithUnparsedExt::insert(&mut unparsed, "endTime", end_time)?; - WithUnparsedExt::insert(&mut unparsed, "duration", duration)?; - WithUnparsedExt::insert(&mut unparsed, "published", published)?; - WithUnparsedExt::insert(&mut unparsed, "updated", updated)?; - WithUnparsedExt::insert(&mut unparsed, "inReplyTo", in_reply_to)?; - WithUnparsedExt::insert(&mut unparsed, "replies", replies)?; - WithUnparsedExt::insert(&mut unparsed, "to", to)?; - WithUnparsedExt::insert(&mut unparsed, "bto", bto)?; - WithUnparsedExt::insert(&mut unparsed, "cc", cc)?; - WithUnparsedExt::insert(&mut unparsed, "bcc", bcc)?; + UnparsedMutExt::insert(&mut unparsed, "type", kind)?; + UnparsedMutExt::insert(&mut unparsed, "context", context)?; + UnparsedMutExt::insert(&mut unparsed, "id", id)?; + UnparsedMutExt::insert(&mut unparsed, "attachment", attachment)?; + UnparsedMutExt::insert(&mut unparsed, "attributedTo", attributed_to)?; + UnparsedMutExt::insert(&mut unparsed, "audience", audience)?; + UnparsedMutExt::insert(&mut unparsed, "content", content)?; + UnparsedMutExt::insert(&mut unparsed, "name", name)?; + UnparsedMutExt::insert(&mut unparsed, "summary", summary)?; + UnparsedMutExt::insert(&mut unparsed, "url", url)?; + UnparsedMutExt::insert(&mut unparsed, "mediaType", media_type)?; + UnparsedMutExt::insert(&mut unparsed, "generator", generator)?; + UnparsedMutExt::insert(&mut unparsed, "image", image)?; + UnparsedMutExt::insert(&mut unparsed, "location", location)?; + UnparsedMutExt::insert(&mut unparsed, "preview", preview)?; + UnparsedMutExt::insert(&mut unparsed, "startTime", start_time)?; + UnparsedMutExt::insert(&mut unparsed, "endTime", end_time)?; + UnparsedMutExt::insert(&mut unparsed, "duration", duration)?; + UnparsedMutExt::insert(&mut unparsed, "published", published)?; + UnparsedMutExt::insert(&mut unparsed, "updated", updated)?; + UnparsedMutExt::insert(&mut unparsed, "inReplyTo", in_reply_to)?; + UnparsedMutExt::insert(&mut unparsed, "replies", replies)?; + UnparsedMutExt::insert(&mut unparsed, "to", to)?; + UnparsedMutExt::insert(&mut unparsed, "bto", bto)?; + UnparsedMutExt::insert(&mut unparsed, "cc", cc)?; + UnparsedMutExt::insert(&mut unparsed, "bcc", bcc)?; Ok(unparsed) } @@ -585,7 +2138,7 @@ impl Object { impl ApObject { fn extending(mut inner: Inner) -> Result where - Inner: WithUnparsed + traits::Object, + Inner: UnparsedMut + traits::Object, { let shares = inner.remove("shares")?; let likes = inner.remove("likes")?; @@ -603,7 +2156,7 @@ impl ApObject { fn retracting(self) -> Result where - Inner: WithUnparsed + traits::Object, + Inner: UnparsedMut + traits::Object, { let ApObject { shares, @@ -742,24 +2295,6 @@ impl Tombstone { } } -impl traits::Base for Object where Kind: std::fmt::Debug {} -impl traits::Object for Object where Kind: std::fmt::Debug {} - -impl traits::Base for ApObject where Inner: traits::Base {} -impl traits::Object for ApObject where Inner: traits::Object {} - -impl traits::Base for Place {} -impl traits::Object for Place {} - -impl traits::Base for Profile {} -impl traits::Object for Profile {} - -impl traits::Base for Relationship {} -impl traits::Object for Relationship {} - -impl traits::Base for Tombstone {} -impl traits::Object for Tombstone {} - impl Extends for Object where Kind: serde::de::DeserializeOwned + serde::ser::Serialize, @@ -799,7 +2334,7 @@ where impl Extends for ApObject where - Inner: WithUnparsed + traits::Object, + Inner: UnparsedMut + traits::Object, { type Error = serde_json::Error; @@ -924,64 +2459,40 @@ impl TryFrom for Object { } } -impl WithUnparsed for Object { - fn unparsed(&self) -> &Unparsed { - &self.unparsed - } - +impl UnparsedMut for Object { fn unparsed_mut(&mut self) -> &mut Unparsed { &mut self.unparsed } } -impl WithUnparsed for ApObject +impl UnparsedMut for ApObject where - Inner: WithUnparsed, + Inner: UnparsedMut, { - fn unparsed(&self) -> &Unparsed { - self.inner.unparsed() - } - fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } -impl WithUnparsed for Place { - fn unparsed(&self) -> &Unparsed { - self.inner.unparsed() - } - +impl UnparsedMut for Place { fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } -impl WithUnparsed for Profile { - fn unparsed(&self) -> &Unparsed { - self.inner.unparsed() - } - +impl UnparsedMut for Profile { fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } -impl WithUnparsed for Relationship { - fn unparsed(&self) -> &Unparsed { - self.inner.unparsed() - } - +impl UnparsedMut for Relationship { fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } } -impl WithUnparsed for Tombstone { - fn unparsed(&self) -> &Unparsed { - self.inner.unparsed() - } - +impl UnparsedMut for Tombstone { fn unparsed_mut(&mut self) -> &mut Unparsed { self.inner.unparsed_mut() } @@ -1022,3 +2533,196 @@ impl From for OneOrMany { Self::from_xsd_string(xsd_string) } } + +impl traits::Base for Object {} +impl traits::Object for Object {} + +impl traits::Base for ApObject where Inner: traits::Base {} +impl traits::Object for ApObject where Inner: traits::Object {} + +impl traits::Base for Place {} +impl traits::Object for Place {} + +impl traits::Base for Profile {} +impl traits::Object for Profile {} + +impl traits::Base for Relationship {} +impl traits::Object for Relationship {} + +impl traits::Base for Tombstone {} +impl traits::Object for Tombstone {} + +impl ObjectRefExt for T where T: ObjectRef {} +impl ObjectMutExt for T where T: ObjectMut {} + +impl ApObjectRefExt for T where T: ApObjectRef {} +impl ApObjectMutExt for T where T: ApObjectMut {} + +impl PlaceRefExt for T where T: PlaceRef {} +impl PlaceMutExt for T where T: PlaceMut {} + +impl ProfileRefExt for T where T: ProfileRef {} +impl ProfileMutExt for T where T: ProfileMut {} + +impl RelationshipRefExt for T where T: RelationshipRef {} +impl RelationshipMutExt for T where T: RelationshipMut {} + +impl TombstoneRefExt for T where T: TombstoneRef {} +impl TombstoneMutExt for T where T: TombstoneMut {} + +impl ObjectRef for Object { + fn object_ref(&self) -> &Object { + self + } +} +impl ObjectMut for Object { + fn object_mut(&mut self) -> &mut Object { + self + } +} + +impl ObjectRef for ApObject +where + Inner: ObjectRef, +{ + fn object_ref(&self) -> &Object { + self.inner.object_ref() + } +} +impl ObjectMut for ApObject +where + Inner: ObjectMut, +{ + fn object_mut(&mut self) -> &mut Object { + self.inner.object_mut() + } +} + +impl ApObjectRef for ApObject +where + Inner: traits::Object, +{ + fn ap_object_ref(&self) -> &ApObject { + self + } +} +impl ApObjectMut for ApObject +where + Inner: traits::Object, +{ + fn ap_object_mut(&mut self) -> &mut ApObject { + self + } +} + +impl PlaceRef for ApObject +where + Inner: PlaceRef, +{ + fn place_ref(&self) -> &Place { + self.inner.place_ref() + } +} +impl PlaceMut for ApObject +where + Inner: PlaceMut, +{ + fn place_mut(&mut self) -> &mut Place { + self.inner.place_mut() + } +} + +impl ProfileRef for ApObject +where + Inner: ProfileRef, +{ + fn profile_ref(&self) -> &Profile { + self.inner.profile_ref() + } +} +impl ProfileMut for ApObject +where + Inner: ProfileMut, +{ + fn profile_mut(&mut self) -> &mut Profile { + self.inner.profile_mut() + } +} + +impl RelationshipRef for ApObject +where + Inner: RelationshipRef, +{ + fn relationship_ref(&self) -> &Relationship { + self.inner.relationship_ref() + } +} +impl RelationshipMut for ApObject +where + Inner: RelationshipMut, +{ + fn relationship_mut(&mut self) -> &mut Relationship { + self.inner.relationship_mut() + } +} + +impl TombstoneRef for ApObject +where + Inner: TombstoneRef, +{ + fn tombstone_ref(&self) -> &Tombstone { + self.inner.tombstone_ref() + } +} +impl TombstoneMut for ApObject +where + Inner: TombstoneMut, +{ + fn tombstone_mut(&mut self) -> &mut Tombstone { + self.inner.tombstone_mut() + } +} + +impl PlaceRef for Place { + fn place_ref(&self) -> &Place { + self + } +} +impl PlaceMut for Place { + fn place_mut(&mut self) -> &mut Place { + self + } +} + +impl ProfileRef for Profile { + fn profile_ref(&self) -> &Profile { + self + } +} +impl ProfileMut for Profile { + fn profile_mut(&mut self) -> &mut Profile { + self + } +} + +impl RelationshipRef for Relationship { + fn relationship_ref(&self) -> &Relationship { + self + } +} +impl RelationshipMut for Relationship { + fn relationship_mut(&mut self) -> &mut Relationship { + self + } +} + +impl TombstoneRef for Tombstone { + fn tombstone_ref(&self) -> &Tombstone { + self + } +} +impl TombstoneMut for Tombstone { + fn tombstone_mut(&mut self) -> &mut Tombstone { + self + } +} diff --git a/src/primitives.rs b/src/primitives.rs index 316cb1d..735e8fa 100644 --- a/src/primitives.rs +++ b/src/primitives.rs @@ -187,16 +187,24 @@ impl OneOrMany { impl crate::traits::Base for Unparsed {} -impl crate::traits::WithUnparsed for Unparsed { - fn unparsed(&self) -> &Unparsed { - self - } - +impl crate::traits::UnparsedMut for Unparsed { fn unparsed_mut(&mut self) -> &mut Unparsed { self } } +impl From for OneOrMany { + fn from(t: T) -> Self { + OneOrMany::from_one(t) + } +} + +impl From> for OneOrMany { + fn from(t: Vec) -> Self { + OneOrMany::from_many(t) + } +} + impl From for AnyString { fn from(s: XsdString) -> Self { AnyString::from_xsd_string(s) diff --git a/src/traits.rs b/src/traits.rs index 161a513..090c939 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -3,13 +3,11 @@ pub use activitystreams::{ Activity, Actor, Base, Collection, CollectionPage, IntransitiveActivity, Link, Object, }; -pub trait WithUnparsed { - fn unparsed(&self) -> &Unparsed; - +pub trait UnparsedMut { fn unparsed_mut(&mut self) -> &mut Unparsed; } -pub trait WithUnparsedExt: WithUnparsed { +pub trait UnparsedMutExt: UnparsedMut { fn remove(&mut self, key: &str) -> Result where T: serde::de::DeserializeOwned, @@ -35,4 +33,4 @@ pub trait Extends: Sized { fn retracts(self) -> Result; } -impl WithUnparsedExt for T where T: WithUnparsed {} +impl UnparsedMutExt for T where T: UnparsedMut {}