diff --git a/src/activity.rs b/src/activity.rs index 3ced894..ce46518 100644 --- a/src/activity.rs +++ b/src/activity.rs @@ -1,7 +1,9 @@ use crate::{ - object::{AnyObject, ApObject, Object, ObjectMut, ObjectRef}, - primitives::{OneOrMany, Unparsed}, - traits::{self, Extends, UnparsedMut, UnparsedMutExt}, + base::{AnyBase, AsBase, Base, Extends}, + markers, + object::{ApObject, AsObject, Object}, + primitives::OneOrMany, + unparsed::{Unparsed, UnparsedMut, UnparsedMutExt}, }; use std::convert::TryFrom; use typed_builder::TypedBuilder; @@ -12,84 +14,54 @@ pub mod kind { use self::kind::*; -pub trait ActivityRef: traits::Activity { +pub trait AsActivity: markers::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 ActorAndObjectRef: markers::Activity { + fn actor_field_ref(&self) -> &OneOrMany; + fn object_field_ref(&self) -> &OneOrMany; + fn actor_field_mut(&mut self) -> &mut OneOrMany; + fn object_field_mut(&mut self) -> &mut 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: markers::Activity { + fn target_field_ref(&self) -> &OneOrMany; + fn target_field_mut(&mut self) -> &mut OneOrMany; } -pub trait TargetRef: traits::Activity { - fn target_field_ref(&self) -> &OneOrMany; +pub trait OriginRef: markers::Activity { + fn origin_field_ref(&self) -> &OneOrMany; + fn origin_field_mut(&mut self) -> &mut OneOrMany; } -pub trait TargetMut: traits::Activity { - fn target_field_mut(&mut self) -> &mut OneOrMany; +pub trait OptTargetRef: markers::Activity { + fn target_field_ref(&self) -> &Option>; + fn target_field_mut(&mut self) -> &mut Option>; } -pub trait OriginRef: traits::Activity { - fn origin_field_ref(&self) -> &OneOrMany; +pub trait OptOriginRef: markers::Activity { + fn origin_field_ref(&self) -> &Option>; + fn origin_field_mut(&mut self) -> &mut Option>; } -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 { +pub trait AsQuestion: markers::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> +pub trait ActivityExt: AsActivity { + 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, + T: Into, { self.activity_mut().result = Some(result.into().into()); self @@ -98,7 +70,7 @@ pub trait ActivityMutExt: ActivityMut { fn set_many_results(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.activity_mut().result = Some(v.into()); @@ -107,7 +79,7 @@ pub trait ActivityMutExt: ActivityMut { fn add_result(&mut self, result: T) -> &mut Self where - T: Into, + T: Into, { let c = match self.activity_mut().result.take() { Some(mut c) => { @@ -120,7 +92,7 @@ pub trait ActivityMutExt: ActivityMut { self } - fn take_result(&mut self) -> Option> { + fn take_result(&mut self) -> Option> { self.activity_mut().result.take() } @@ -129,9 +101,16 @@ pub trait ActivityMutExt: ActivityMut { self } + fn instrument<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.activity_ref().instrument.as_ref() + } + fn set_instrument(&mut self, instrument: T) -> &mut Self where - T: Into, + T: Into, { self.activity_mut().instrument = Some(instrument.into().into()); self @@ -140,7 +119,7 @@ pub trait ActivityMutExt: ActivityMut { fn set_many_instruments(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.activity_mut().instrument = Some(v.into()); @@ -149,7 +128,7 @@ pub trait ActivityMutExt: ActivityMut { fn add_instrument(&mut self, instrument: T) -> &mut Self where - T: Into, + T: Into, { let c = match self.activity_mut().instrument.take() { Some(mut c) => { @@ -162,7 +141,7 @@ pub trait ActivityMutExt: ActivityMut { self } - fn take_instrument(&mut self) -> Option> { + fn take_instrument(&mut self) -> Option> { self.activity_mut().instrument.take() } @@ -173,19 +152,13 @@ pub trait ActivityMutExt: ActivityMut { } pub trait ActorAndObjectRefExt: ActorAndObjectRef { - fn actor(&self) -> &OneOrMany { + 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, + T: Into, { *self.actor_field_mut() = actor.into().into(); self @@ -194,7 +167,7 @@ pub trait ActorAndObjectMutExt: ActorAndObjectMut { fn set_many_actors(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); *self.actor_field_mut() = v.into(); @@ -203,15 +176,19 @@ pub trait ActorAndObjectMutExt: ActorAndObjectMut { fn add_actor(&mut self, actor: T) -> &mut Self where - T: Into, + T: Into, { self.actor_field_mut().add(actor.into()); self } + fn object(&self) -> &OneOrMany { + self.object_field_ref() + } + fn set_object(&mut self, object: T) -> &mut Self where - T: Into, + T: Into, { *self.object_field_mut() = object.into().into(); self @@ -220,7 +197,7 @@ pub trait ActorAndObjectMutExt: ActorAndObjectMut { fn set_many_objects(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); *self.object_field_mut() = v.into(); @@ -229,7 +206,7 @@ pub trait ActorAndObjectMutExt: ActorAndObjectMut { fn add_object(&mut self, object: T) -> &mut Self where - T: Into, + T: Into, { self.object_field_mut().add(object.into()); self @@ -237,15 +214,13 @@ pub trait ActorAndObjectMutExt: ActorAndObjectMut { } pub trait TargetRefExt: TargetRef { - fn target(&self) -> &OneOrMany { + fn target(&self) -> &OneOrMany { self.target_field_ref() } -} -pub trait TargetMutExt: TargetMut { fn set_target(&mut self, target: T) -> &mut Self where - T: Into, + T: Into, { *self.target_field_mut() = target.into().into(); self @@ -254,7 +229,7 @@ pub trait TargetMutExt: TargetMut { fn set_many_targets(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); *self.target_field_mut() = v.into(); @@ -263,7 +238,7 @@ pub trait TargetMutExt: TargetMut { fn add_target(&mut self, target: T) -> &mut Self where - T: Into, + T: Into, { self.target_field_mut().add(target.into()); self @@ -271,15 +246,13 @@ pub trait TargetMutExt: TargetMut { } pub trait OriginRefExt: OriginRef { - fn origin(&self) -> &OneOrMany { + fn origin(&self) -> &OneOrMany { self.origin_field_ref() } -} -pub trait OriginMutExt: OriginMut { fn set_origin(&mut self, origin: T) -> &mut Self where - T: Into, + T: Into, { *self.origin_field_mut() = origin.into().into(); self @@ -288,7 +261,7 @@ pub trait OriginMutExt: OriginMut { fn set_many_origins(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); *self.origin_field_mut() = v.into(); @@ -297,7 +270,7 @@ pub trait OriginMutExt: OriginMut { fn add_origin(&mut self, origin: T) -> &mut Self where - T: Into, + T: Into, { self.origin_field_mut().add(origin.into()); self @@ -305,15 +278,13 @@ pub trait OriginMutExt: OriginMut { } pub trait OptTargetRefExt: OptTargetRef { - fn target(&self) -> Option<&OneOrMany> { + 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, + T: Into, { *self.target_field_mut() = Some(target.into().into()); self @@ -322,7 +293,7 @@ pub trait OptTargetMutExt: OptTargetMut { fn set_many_targets(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); *self.target_field_mut() = Some(v.into()); @@ -331,7 +302,7 @@ pub trait OptTargetMutExt: OptTargetMut { fn add_target(&mut self, target: T) -> &mut Self where - T: Into, + T: Into, { let c = match self.target_field_mut().take() { Some(mut c) => { @@ -344,7 +315,7 @@ pub trait OptTargetMutExt: OptTargetMut { self } - fn take_target(&mut self) -> Option> { + fn take_target(&mut self) -> Option> { self.target_field_mut().take() } @@ -355,15 +326,13 @@ pub trait OptTargetMutExt: OptTargetMut { } pub trait OptOriginRefExt: OptOriginRef { - fn origin(&self) -> Option<&OneOrMany> { + 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, + T: Into, { *self.origin_field_mut() = Some(origin.into().into()); self @@ -372,7 +341,7 @@ pub trait OptOriginMutExt: OptOriginMut { fn set_many_origins(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); *self.origin_field_mut() = Some(v.into()); @@ -381,7 +350,7 @@ pub trait OptOriginMutExt: OptOriginMut { fn add_origin(&mut self, origin: T) -> &mut Self where - T: Into, + T: Into, { let c = match self.origin_field_mut().take() { Some(mut c) => { @@ -394,7 +363,7 @@ pub trait OptOriginMutExt: OptOriginMut { self } - fn take_origin(&mut self) -> Option> { + fn take_origin(&mut self) -> Option> { self.origin_field_mut().take() } @@ -404,20 +373,18 @@ pub trait OptOriginMutExt: OptOriginMut { } } -pub trait QuestionRefExt: QuestionRef { - fn one_of(&self) -> Option<&OneOrMany> { +pub trait QuestionExt: AsQuestion { + fn one_of(&self) -> Option<&OneOrMany> { self.question_ref().one_of.as_ref() } - fn any_of(&self) -> Option<&OneOrMany> { + 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, + T: Into, { self.question_mut().one_of = Some(one_of.into().into()); self @@ -426,7 +393,7 @@ pub trait QuestionMutExt: QuestionMut { fn set_many_one_ofs(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.question_mut().one_of = Some(v.into()); @@ -435,7 +402,7 @@ pub trait QuestionMutExt: QuestionMut { fn add_one_of(&mut self, one_of: T) -> &mut Self where - T: Into, + T: Into, { let v = match self.question_mut().one_of.take() { Some(mut v) => { @@ -448,7 +415,7 @@ pub trait QuestionMutExt: QuestionMut { self } - fn take_one_of(&mut self) -> Option> { + fn take_one_of(&mut self) -> Option> { self.question_mut().one_of.take() } @@ -459,7 +426,7 @@ pub trait QuestionMutExt: QuestionMut { fn set_any_of(&mut self, any_of: T) -> &mut Self where - T: Into, + T: Into, { self.question_mut().any_of = Some(any_of.into().into()); self @@ -468,7 +435,7 @@ pub trait QuestionMutExt: QuestionMut { fn set_many_any_ofs(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.question_mut().any_of = Some(v.into()); @@ -477,7 +444,7 @@ pub trait QuestionMutExt: QuestionMut { fn add_any_of(&mut self, any_of: T) -> &mut Self where - T: Into, + T: Into, { let v = match self.question_mut().any_of.take() { Some(mut v) => { @@ -490,7 +457,7 @@ pub trait QuestionMutExt: QuestionMut { self } - fn take_any_of(&mut self) -> Option> { + fn take_any_of(&mut self) -> Option> { self.question_mut().any_of.take() } @@ -532,11 +499,11 @@ pub type Remove = ActorAndObjectOptOriginAndTarget; pub struct Activity { #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub result: Option>, + pub result: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub instrument: Option>, + pub instrument: Option>, #[serde(flatten)] pub inner: Object, @@ -547,10 +514,10 @@ pub struct Activity { #[builder(doc)] pub struct ActorAndObject { #[builder(setter(into))] - pub actor: OneOrMany, + pub actor: OneOrMany, #[builder(setter(into))] - pub object: OneOrMany, + pub object: OneOrMany, #[serde(flatten)] pub inner: Activity, @@ -561,10 +528,10 @@ pub struct ActorAndObject { #[builder(doc)] pub struct Arrive { #[builder(setter(into))] - pub actor: OneOrMany, + pub actor: OneOrMany, #[builder(setter(into))] - pub origin: OneOrMany, + pub origin: OneOrMany, #[serde(flatten)] pub inner: Activity, @@ -575,13 +542,13 @@ pub struct Arrive { #[builder(doc)] pub struct Invite { #[builder(setter(into))] - pub actor: OneOrMany, + pub actor: OneOrMany, #[builder(setter(into))] - pub object: OneOrMany, + pub object: OneOrMany, #[builder(setter(into))] - pub target: OneOrMany, + pub target: OneOrMany, #[serde(flatten)] pub inner: Activity, @@ -592,14 +559,14 @@ pub struct Invite { #[builder(doc)] pub struct Delete { #[builder(setter(into))] - pub actor: OneOrMany, + pub actor: OneOrMany, #[builder(setter(into))] - pub object: OneOrMany, + pub object: OneOrMany, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub origin: Option>, + pub origin: Option>, #[serde(flatten)] pub inner: Activity, @@ -610,18 +577,18 @@ pub struct Delete { #[builder(doc)] pub struct ActorAndObjectOptOriginAndTarget { #[builder(setter(into))] - pub actor: OneOrMany, + pub actor: OneOrMany, #[builder(setter(into))] - pub object: OneOrMany, + pub object: OneOrMany, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub origin: Option>, + pub origin: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub target: Option>, + pub target: Option>, #[serde(flatten)] pub inner: Activity, @@ -632,14 +599,14 @@ pub struct ActorAndObjectOptOriginAndTarget { #[builder(doc)] pub struct ActorAndObjectOptTarget { #[builder(setter(into))] - pub actor: OneOrMany, + pub actor: OneOrMany, #[builder(setter(into))] - pub object: OneOrMany, + pub object: OneOrMany, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub target: Option>, + pub target: Option>, #[serde(flatten)] pub inner: Activity, @@ -650,15 +617,15 @@ pub struct ActorAndObjectOptTarget { #[builder(doc)] pub struct Travel { #[builder(setter(into))] - pub actor: OneOrMany, + pub actor: OneOrMany, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub origin: Option>, + pub origin: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub target: Option>, + pub target: Option>, #[serde(flatten)] pub inner: Activity, @@ -670,11 +637,11 @@ pub struct Travel { pub struct Question { #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub one_of: Option>, + pub one_of: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub any_of: Option>, + pub any_of: Option>, #[serde(flatten)] pub inner: Activity, @@ -957,54 +924,65 @@ impl Question { } } -impl traits::Base for Activity {} -impl traits::Object for Activity {} -impl traits::Activity for Activity {} +impl markers::Base for Activity {} +impl markers::Object for Activity {} +impl markers::Activity for Activity {} -impl traits::Base for ActorAndObject {} -impl traits::Object for ActorAndObject {} -impl traits::Activity for ActorAndObject {} +impl markers::Base for ActorAndObject {} +impl markers::Object for ActorAndObject {} +impl markers::Activity for ActorAndObject {} -impl traits::Base for ActorAndObjectOptTarget {} -impl traits::Object for ActorAndObjectOptTarget {} -impl traits::Activity for ActorAndObjectOptTarget {} +impl markers::Base for ActorAndObjectOptTarget {} +impl markers::Object for ActorAndObjectOptTarget {} +impl markers::Activity for ActorAndObjectOptTarget {} -impl traits::Base for ActorAndObjectOptOriginAndTarget {} -impl traits::Object for ActorAndObjectOptOriginAndTarget {} -impl traits::Activity for ActorAndObjectOptOriginAndTarget {} +impl markers::Base for ActorAndObjectOptOriginAndTarget {} +impl markers::Object for ActorAndObjectOptOriginAndTarget {} +impl markers::Activity for ActorAndObjectOptOriginAndTarget {} -impl traits::Base for Arrive {} -impl traits::Object for Arrive {} -impl traits::Activity for Arrive {} -impl traits::IntransitiveActivity for Arrive {} +impl markers::Base for Arrive {} +impl markers::Object for Arrive {} +impl markers::Activity for Arrive {} +impl markers::IntransitiveActivity for Arrive {} -impl traits::Base for Invite {} -impl traits::Object for Invite {} -impl traits::Activity for Invite {} +impl markers::Base for Invite {} +impl markers::Object for Invite {} +impl markers::Activity for Invite {} -impl traits::Base for Delete {} -impl traits::Object for Delete {} -impl traits::Activity for Delete {} +impl markers::Base for Delete {} +impl markers::Object for Delete {} +impl markers::Activity for Delete {} -impl traits::Base for Travel {} -impl traits::Object for Travel {} -impl traits::Activity for Travel {} -impl traits::IntransitiveActivity for Travel {} +impl markers::Base for Travel {} +impl markers::Object for Travel {} +impl markers::Activity for Travel {} +impl markers::IntransitiveActivity for Travel {} -impl traits::Base for Question {} -impl traits::Object for Question {} -impl traits::Activity for Question {} -impl traits::IntransitiveActivity for Question {} +impl markers::Base for Question {} +impl markers::Object for Question {} +impl markers::Activity for Question {} +impl markers::IntransitiveActivity for Question {} -impl Extends> for Activity { +impl markers::Activity for ApObject where Inner: markers::Activity {} +impl markers::IntransitiveActivity for ApObject where + Inner: markers::IntransitiveActivity +{ +} + +impl Extends for Activity +where + Kind: serde::de::DeserializeOwned + serde::ser::Serialize, +{ type Error = serde_json::Error; - fn extends(object: Object) -> Result { - Self::extending(object) + fn extends(base: Base) -> Result { + let inner = Object::extends(base)?; + Self::extending(inner) } - fn retracts(self) -> Result, Self::Error> { - self.retracting() + fn retracts(self) -> Result, Self::Error> { + let inner = self.retracting()?; + inner.retracts() } } @@ -1024,15 +1002,20 @@ impl TryFrom> for Object { } } -impl Extends> for ActorAndObject { +impl Extends for ActorAndObject +where + Kind: serde::de::DeserializeOwned + serde::ser::Serialize, +{ type Error = serde_json::Error; - fn extends(object: Object) -> Result { - Self::extending(object) + fn extends(base: Base) -> Result { + let inner = Object::extends(base)?; + Self::extending(inner) } - fn retracts(self) -> Result, Self::Error> { - self.retracting() + fn retracts(self) -> Result, Self::Error> { + let inner = self.retracting()?; + inner.retracts() } } @@ -1052,15 +1035,17 @@ impl TryFrom> for Object { } } -impl Extends> for Arrive { +impl Extends for Arrive { type Error = serde_json::Error; - fn extends(object: Object) -> Result { - Self::extending(object) + fn extends(base: Base) -> Result { + let inner = Object::extends(base)?; + Self::extending(inner) } - fn retracts(self) -> Result, Self::Error> { - self.retracting() + fn retracts(self) -> Result, Self::Error> { + let inner = self.retracting()?; + inner.retracts() } } @@ -1080,15 +1065,17 @@ impl TryFrom for Object { } } -impl Extends> for Invite { +impl Extends for Invite { type Error = serde_json::Error; - fn extends(object: Object) -> Result { - Self::extending(object) + fn extends(base: Base) -> Result { + let inner = Object::extends(base)?; + Self::extending(inner) } - fn retracts(self) -> Result, Self::Error> { - self.retracting() + fn retracts(self) -> Result, Self::Error> { + let inner = self.retracting()?; + inner.retracts() } } @@ -1108,15 +1095,17 @@ impl TryFrom for Object { } } -impl Extends> for Delete { +impl Extends for Delete { type Error = serde_json::Error; - fn extends(object: Object) -> Result { - Self::extending(object) + fn extends(base: Base) -> Result { + let inner = Object::extends(base)?; + Self::extending(inner) } - fn retracts(self) -> Result, Self::Error> { - self.retracting() + fn retracts(self) -> Result, Self::Error> { + let inner = self.retracting()?; + inner.retracts() } } @@ -1136,15 +1125,20 @@ impl TryFrom for Object { } } -impl Extends> for ActorAndObjectOptOriginAndTarget { +impl Extends for ActorAndObjectOptOriginAndTarget +where + Kind: serde::de::DeserializeOwned + serde::ser::Serialize, +{ type Error = serde_json::Error; - fn extends(object: Object) -> Result { - Self::extending(object) + fn extends(base: Base) -> Result { + let inner = Object::extends(base)?; + Self::extending(inner) } - fn retracts(self) -> Result, Self::Error> { - self.retracting() + fn retracts(self) -> Result, Self::Error> { + let inner = self.retracting()?; + inner.retracts() } } @@ -1164,15 +1158,20 @@ impl TryFrom> for Object { } } -impl Extends> for ActorAndObjectOptTarget { +impl Extends for ActorAndObjectOptTarget +where + Kind: serde::de::DeserializeOwned + serde::ser::Serialize, +{ type Error = serde_json::Error; - fn extends(object: Object) -> Result { - Self::extending(object) + fn extends(base: Base) -> Result { + let inner = Object::extends(base)?; + Self::extending(inner) } - fn retracts(self) -> Result, Self::Error> { - self.retracting() + fn retracts(self) -> Result, Self::Error> { + let inner = self.retracting()?; + inner.retracts() } } @@ -1192,15 +1191,17 @@ impl TryFrom> for Object { } } -impl Extends> for Travel { +impl Extends for Travel { type Error = serde_json::Error; - fn extends(object: Object) -> Result { - Self::extending(object) + fn extends(base: Base) -> Result { + let inner = Object::extends(base)?; + Self::extending(inner) } - fn retracts(self) -> Result, Self::Error> { - self.retracting() + fn retracts(self) -> Result, Self::Error> { + let inner = self.retracting()?; + inner.retracts() } } @@ -1220,15 +1221,17 @@ impl TryFrom for Object { } } -impl Extends> for Question { +impl Extends for Question { type Error = serde_json::Error; - fn extends(object: Object) -> Result { - Self::extending(object) + fn extends(base: Base) -> Result { + let inner = Object::extends(base)?; + Self::extending(inner) } - fn retracts(self) -> Result, Self::Error> { - self.retracting() + fn retracts(self) -> Result, Self::Error> { + let inner = self.retracting()?; + inner.retracts() } } @@ -1302,465 +1305,464 @@ impl UnparsedMut for Question { } } -impl ObjectRef for Activity { - fn object_ref(&self) -> &Object { - &self.inner +impl AsBase for Activity { + fn base_ref(&self) -> &Base { + self.inner.base_ref() + } + + fn base_mut(&mut self) -> &mut Base { + self.inner.base_mut() } } -impl ObjectMut for Activity { +impl AsObject for Activity { + fn object_ref(&self) -> &Object { + &self.inner + } + fn object_mut(&mut self) -> &mut Object { &mut self.inner } } -impl ActivityRef for Activity { +impl AsActivity 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 AsBase for ActorAndObject { + fn base_ref(&self) -> &Base { + self.inner.base_ref() + } + + fn base_mut(&mut self) -> &mut Base { + self.inner.base_mut() } } -impl ObjectMut for ActorAndObject { +impl AsObject for ActorAndObject { + fn object_ref(&self) -> &Object { + self.inner.object_ref() + } + fn object_mut(&mut self) -> &mut Object { self.inner.object_mut() } } -impl ActivityRef for ActorAndObject { +impl AsActivity 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 { + fn actor_field_ref(&self) -> &OneOrMany { &self.actor } - fn object_field_ref(&self) -> &OneOrMany { + fn object_field_ref(&self) -> &OneOrMany { &self.object } -} -impl ActorAndObjectMut for ActorAndObject { - fn actor_field_mut(&mut self) -> &mut OneOrMany { + fn actor_field_mut(&mut self) -> &mut OneOrMany { &mut self.actor } - fn object_field_mut(&mut self) -> &mut OneOrMany { + 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 AsBase for ActorAndObjectOptTarget { + fn base_ref(&self) -> &Base { + self.inner.base_ref() + } + + fn base_mut(&mut self) -> &mut Base { + self.inner.base_mut() } } -impl ObjectMut for ActorAndObjectOptTarget { +impl AsObject for ActorAndObjectOptTarget { + fn object_ref(&self) -> &Object { + self.inner.object_ref() + } + fn object_mut(&mut self) -> &mut Object { self.inner.object_mut() } } -impl ActivityRef for ActorAndObjectOptTarget { +impl AsActivity 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 { + fn actor_field_ref(&self) -> &OneOrMany { &self.actor } - fn object_field_ref(&self) -> &OneOrMany { + fn object_field_ref(&self) -> &OneOrMany { &self.object } -} -impl ActorAndObjectMut for ActorAndObjectOptTarget { - fn actor_field_mut(&mut self) -> &mut OneOrMany { + fn actor_field_mut(&mut self) -> &mut OneOrMany { &mut self.actor } - fn object_field_mut(&mut self) -> &mut OneOrMany { + fn object_field_mut(&mut self) -> &mut OneOrMany { &mut self.object } } impl OptTargetRef for ActorAndObjectOptTarget { - fn target_field_ref(&self) -> &Option> { + fn target_field_ref(&self) -> &Option> { &self.target } -} -impl OptTargetMut for ActorAndObjectOptTarget { - fn target_field_mut(&mut self) -> &mut Option> { + 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 AsBase for ActorAndObjectOptOriginAndTarget { + fn base_ref(&self) -> &Base { + self.inner.base_ref() + } + + fn base_mut(&mut self) -> &mut Base { + self.inner.base_mut() } } -impl ObjectMut for ActorAndObjectOptOriginAndTarget { +impl AsObject for ActorAndObjectOptOriginAndTarget { + fn object_ref(&self) -> &Object { + self.inner.object_ref() + } + fn object_mut(&mut self) -> &mut Object { self.inner.object_mut() } } -impl ActivityRef for ActorAndObjectOptOriginAndTarget { +impl AsActivity 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 { + fn actor_field_ref(&self) -> &OneOrMany { &self.actor } - fn object_field_ref(&self) -> &OneOrMany { + fn object_field_ref(&self) -> &OneOrMany { &self.object } -} -impl ActorAndObjectMut for ActorAndObjectOptOriginAndTarget { - fn actor_field_mut(&mut self) -> &mut OneOrMany { + fn actor_field_mut(&mut self) -> &mut OneOrMany { &mut self.actor } - fn object_field_mut(&mut self) -> &mut OneOrMany { + fn object_field_mut(&mut self) -> &mut OneOrMany { &mut self.object } } impl OptTargetRef for ActorAndObjectOptOriginAndTarget { - fn target_field_ref(&self) -> &Option> { + fn target_field_ref(&self) -> &Option> { &self.target } -} -impl OptTargetMut for ActorAndObjectOptOriginAndTarget { - fn target_field_mut(&mut self) -> &mut Option> { + fn target_field_mut(&mut self) -> &mut Option> { &mut self.target } } impl OptOriginRef for ActorAndObjectOptOriginAndTarget { - fn origin_field_ref(&self) -> &Option> { + fn origin_field_ref(&self) -> &Option> { &self.origin } -} -impl OptOriginMut for ActorAndObjectOptOriginAndTarget { - fn origin_field_mut(&mut self) -> &mut Option> { + 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 AsBase for Arrive { + fn base_ref(&self) -> &Base { + self.inner.base_ref() + } + + fn base_mut(&mut self) -> &mut Base { + self.inner.base_mut() } } -impl ObjectMut for Arrive { +impl AsObject for Arrive { + fn object_ref(&self) -> &Object { + self.inner.object_ref() + } + fn object_mut(&mut self) -> &mut Object { self.inner.object_mut() } } -impl ActivityRef for Arrive { +impl AsActivity 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 { + fn origin_field_ref(&self) -> &OneOrMany { &self.origin } -} -impl OriginMut for Arrive { - fn origin_field_mut(&mut self) -> &mut OneOrMany { + 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 AsBase for Invite { + fn base_ref(&self) -> &Base { + self.inner.base_ref() + } + + fn base_mut(&mut self) -> &mut Base { + self.inner.base_mut() } } -impl ObjectMut for Invite { +impl AsObject for Invite { + fn object_ref(&self) -> &Object { + self.inner.object_ref() + } + fn object_mut(&mut self) -> &mut Object { self.inner.object_mut() } } -impl ActivityRef for Invite { +impl AsActivity 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 { + fn actor_field_ref(&self) -> &OneOrMany { &self.actor } - fn object_field_ref(&self) -> &OneOrMany { + fn object_field_ref(&self) -> &OneOrMany { &self.object } -} -impl ActorAndObjectMut for Invite { - fn actor_field_mut(&mut self) -> &mut OneOrMany { + fn actor_field_mut(&mut self) -> &mut OneOrMany { &mut self.actor } - fn object_field_mut(&mut self) -> &mut OneOrMany { + fn object_field_mut(&mut self) -> &mut OneOrMany { &mut self.object } } impl TargetRef for Invite { - fn target_field_ref(&self) -> &OneOrMany { + fn target_field_ref(&self) -> &OneOrMany { &self.target } -} -impl TargetMut for Invite { - fn target_field_mut(&mut self) -> &mut OneOrMany { + 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 AsBase for Delete { + fn base_ref(&self) -> &Base { + self.inner.base_ref() + } + + fn base_mut(&mut self) -> &mut Base { + self.inner.base_mut() } } -impl ObjectMut for Delete { +impl AsObject for Delete { + fn object_ref(&self) -> &Object { + self.inner.object_ref() + } + fn object_mut(&mut self) -> &mut Object { self.inner.object_mut() } } -impl ActivityRef for Delete { +impl AsActivity 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 { + fn actor_field_ref(&self) -> &OneOrMany { &self.actor } - fn object_field_ref(&self) -> &OneOrMany { + fn object_field_ref(&self) -> &OneOrMany { &self.object } -} -impl ActorAndObjectMut for Delete { - fn actor_field_mut(&mut self) -> &mut OneOrMany { + fn actor_field_mut(&mut self) -> &mut OneOrMany { &mut self.actor } - fn object_field_mut(&mut self) -> &mut OneOrMany { + fn object_field_mut(&mut self) -> &mut OneOrMany { &mut self.object } } impl OptOriginRef for Delete { - fn origin_field_ref(&self) -> &Option> { + fn origin_field_ref(&self) -> &Option> { &self.origin } -} -impl OptOriginMut for Delete { - fn origin_field_mut(&mut self) -> &mut Option> { + 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 AsBase for Travel { + fn base_ref(&self) -> &Base { + self.inner.base_ref() + } + + fn base_mut(&mut self) -> &mut Base { + self.inner.base_mut() } } -impl ObjectMut for Travel { +impl AsObject for Travel { + fn object_ref(&self) -> &Object { + self.inner.object_ref() + } + fn object_mut(&mut self) -> &mut Object { self.inner.object_mut() } } -impl ActivityRef for Travel { +impl AsActivity 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> { + fn target_field_ref(&self) -> &Option> { &self.target } -} -impl OptTargetMut for Travel { - fn target_field_mut(&mut self) -> &mut Option> { + fn target_field_mut(&mut self) -> &mut Option> { &mut self.target } } impl OptOriginRef for Travel { - fn origin_field_ref(&self) -> &Option> { + fn origin_field_ref(&self) -> &Option> { &self.origin } -} -impl OptOriginMut for Travel { - fn origin_field_mut(&mut self) -> &mut Option> { + 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 AsBase for Question { + fn base_ref(&self) -> &Base { + self.inner.base_ref() + } + + fn base_mut(&mut self) -> &mut Base { + self.inner.base_mut() } } -impl ObjectMut for Question { +impl AsObject for Question { + fn object_ref(&self) -> &Object { + self.inner.object_ref() + } + fn object_mut(&mut self) -> &mut Object { self.inner.object_mut() } } -impl ActivityRef for Question { +impl AsActivity 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 { +impl AsQuestion 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 +impl AsActivity for ApObject where - Inner: ActivityRef, + Inner: AsActivity, { fn activity_ref(&self) -> &Activity { self.inner.activity_ref() } -} -impl ActivityMut for ApObject -where - Inner: ActivityMut, -{ + fn activity_mut(&mut self) -> &mut Activity { self.inner.activity_mut() } @@ -1770,23 +1772,19 @@ impl ActorAndObjectRef for ApObject where Inner: ActorAndObjectRef, { - fn actor_field_ref(&self) -> &OneOrMany { + fn actor_field_ref(&self) -> &OneOrMany { self.inner.actor_field_ref() } - fn object_field_ref(&self) -> &OneOrMany { + 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 { + + fn actor_field_mut(&mut self) -> &mut OneOrMany { self.inner.actor_field_mut() } - fn object_field_mut(&mut self) -> &mut OneOrMany { + fn object_field_mut(&mut self) -> &mut OneOrMany { self.inner.object_field_mut() } } @@ -1795,15 +1793,11 @@ impl TargetRef for ApObject where Inner: TargetRef, { - fn target_field_ref(&self) -> &OneOrMany { + fn target_field_ref(&self) -> &OneOrMany { self.inner.target_field_ref() } -} -impl TargetMut for ApObject -where - Inner: TargetMut, -{ - fn target_field_mut(&mut self) -> &mut OneOrMany { + + fn target_field_mut(&mut self) -> &mut OneOrMany { self.inner.target_field_mut() } } @@ -1812,15 +1806,11 @@ impl OriginRef for ApObject where Inner: OriginRef, { - fn origin_field_ref(&self) -> &OneOrMany { + 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 { + + fn origin_field_mut(&mut self) -> &mut OneOrMany { self.inner.origin_field_mut() } } @@ -1829,15 +1819,11 @@ impl OptTargetRef for ApObject where Inner: OptTargetRef, { - fn target_field_ref(&self) -> &Option> { + 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> { + + fn target_field_mut(&mut self) -> &mut Option> { self.inner.target_field_mut() } } @@ -1846,32 +1832,32 @@ impl OptOriginRef for ApObject where Inner: OptOriginRef, { - fn origin_field_ref(&self) -> &Option> { + 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> { + + fn origin_field_mut(&mut self) -> &mut Option> { self.inner.origin_field_mut() } } -impl QuestionRef for ApObject +impl AsQuestion for ApObject where - Inner: QuestionRef, + Inner: AsQuestion, { 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() } } + +impl ActivityExt for T where T: AsActivity {} +impl ActorAndObjectRefExt for T where T: ActorAndObjectRef {} +impl TargetRefExt for T where T: TargetRef {} +impl OriginRefExt for T where T: OriginRef {} +impl OptTargetRefExt for T where T: OptTargetRef {} +impl OptOriginRefExt for T where T: OptOriginRef {} +impl QuestionExt for T where T: AsQuestion {} diff --git a/src/actor.rs b/src/actor.rs index aac0949..45860ea 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -1,7 +1,9 @@ use crate::{ - object::{Object, ObjectMut, ObjectRef}, - primitives::{OneOrMany, Unparsed, XsdAnyUri, XsdString}, - traits::{self, Extends, UnparsedMut, UnparsedMutExt}, + base::{AsBase, Base, Extends}, + markers, + object::{ApObject, AsApObject, AsObject, Object}, + primitives::{OneOrMany, XsdAnyUri, XsdString}, + unparsed::{Unparsed, UnparsedMut, UnparsedMutExt}, }; use typed_builder::TypedBuilder; @@ -11,15 +13,12 @@ pub mod kind { use self::kind::*; -pub trait ApActorRef: traits::Actor { +pub trait AsApActor: markers::Actor { fn ap_actor_ref(&self) -> &ApActor; -} - -pub trait ApActorMut: traits::Actor { fn ap_actor_mut(&mut self) -> &mut ApActor; } -pub trait ApActorRefExt: ApActorRef { +pub trait ApActorExt: AsApActor { fn inbox<'a>(&'a self) -> &'a XsdAnyUri where Inner: 'a, @@ -27,6 +26,11 @@ pub trait ApActorRefExt: ApActorRef { &self.ap_actor_ref().inbox } + fn set_inbox(&mut self, inbox: XsdAnyUri) -> &mut Self { + self.ap_actor_mut().inbox = inbox; + self + } + fn outbox<'a>(&'a self) -> &'a XsdAnyUri where Inner: 'a, @@ -34,6 +38,11 @@ pub trait ApActorRefExt: ApActorRef { &self.ap_actor_ref().outbox } + fn set_outbox(&mut self, outbox: XsdAnyUri) -> &mut Self { + self.ap_actor_mut().outbox = outbox; + self + } + fn following<'a>(&'a self) -> Option<&'a XsdAnyUri> where Inner: 'a, @@ -41,53 +50,6 @@ pub trait ApActorRefExt: ApActorRef { 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 @@ -102,6 +64,13 @@ pub trait ApActorMutExt: ApActorMut { self } + fn followers<'a>(&'a self) -> Option<&'a XsdAnyUri> + where + Inner: 'a, + { + self.ap_actor_ref().followers.as_ref() + } + fn set_followers(&mut self, followers: XsdAnyUri) -> &mut Self { self.ap_actor_mut().followers = Some(followers); self @@ -116,6 +85,13 @@ pub trait ApActorMutExt: ApActorMut { self } + fn liked<'a>(&'a self) -> Option<&'a XsdAnyUri> + where + Inner: 'a, + { + self.ap_actor_ref().liked.as_ref() + } + fn set_liked(&mut self, liked: XsdAnyUri) -> &mut Self { self.ap_actor_mut().liked = Some(liked); self @@ -125,11 +101,18 @@ pub trait ApActorMutExt: ApActorMut { self.ap_actor_mut().liked.take() } - fn delete_likes(&mut self) -> &mut Self { + fn delete_liked(&mut self) -> &mut Self { self.ap_actor_mut().liked = None; self } + fn streams<'a>(&'a self) -> Option<&'a OneOrMany> + where + Inner: 'a, + { + self.ap_actor_ref().streams.as_ref() + } + fn set_streams(&mut self, streams: XsdAnyUri) -> &mut Self { self.ap_actor_mut().streams = Some(streams.into()); self @@ -165,6 +148,13 @@ pub trait ApActorMutExt: ApActorMut { self } + fn preferred_username<'a>(&'a self) -> Option<&'a XsdString> + where + Inner: 'a, + { + self.ap_actor_ref().preferred_username.as_ref() + } + fn set_preferred_username(&mut self, string: XsdString) -> &mut Self { self.ap_actor_mut().preferred_username = Some(string); self @@ -179,6 +169,13 @@ pub trait ApActorMutExt: ApActorMut { self } + fn endpoints<'a>(&'a self) -> Option<&'a Endpoints> + where + Inner: 'a, + { + self.ap_actor_ref().endpoints.as_ref() + } + fn set_endpoints(&mut self, endpoints: Endpoints) -> &mut Self { self.ap_actor_mut().endpoints = Some(endpoints); self @@ -266,7 +263,7 @@ pub struct Endpoints { impl ApActor { fn extending(mut inner: Inner) -> Result where - Inner: UnparsedMut + traits::Actor, + Inner: UnparsedMut + markers::Actor, { let inbox = inner.remove("inbox")?; let outbox = inner.remove("outbox")?; @@ -292,7 +289,7 @@ impl ApActor { fn retracting(self) -> Result where - Inner: UnparsedMut + traits::Actor, + Inner: UnparsedMut + markers::Actor, { let ApActor { inbox, @@ -320,28 +317,31 @@ impl ApActor { } } -impl traits::Actor for Application {} -impl traits::Actor for Group {} -impl traits::Actor for Organization {} -impl traits::Actor for Person {} -impl traits::Actor for Service {} +impl markers::Actor for Application {} +impl markers::Actor for Group {} +impl markers::Actor for Organization {} +impl markers::Actor for Person {} +impl markers::Actor for Service {} -impl traits::Base for ApActor where Inner: traits::Base {} -impl traits::Object for ApActor where Inner: traits::Object {} -impl traits::Actor for ApActor where Inner: traits::Actor {} +impl markers::Base for ApActor where Inner: markers::Base {} +impl markers::Object for ApActor where Inner: markers::Object {} +impl markers::Actor for ApActor where Inner: markers::Actor {} -impl Extends for ApActor +impl Extends for ApActor where - Inner: UnparsedMut + traits::Actor, + Inner: Extends + UnparsedMut + markers::Actor, + Kind: serde::de::DeserializeOwned + serde::ser::Serialize, { type Error = serde_json::Error; - fn extends(inner: Inner) -> Result { + fn extends(base: Base) -> Result { + let inner = Inner::extends(base)?; Self::extending(inner) } - fn retracts(self) -> Result { - self.retracting() + fn retracts(self) -> Result, Self::Error> { + let inner = self.retracting()?; + inner.retracts() } } @@ -354,39 +354,56 @@ where } } -impl ObjectRef for ApActor +impl AsBase for ApActor where - Inner: ObjectRef, + Inner: AsBase, +{ + fn base_ref(&self) -> &Base { + self.inner.base_ref() + } + + fn base_mut(&mut self) -> &mut Base { + self.inner.base_mut() + } +} + +impl AsObject for ApActor +where + Inner: AsObject, { 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 +impl AsApActor for ApActor where - Inner: traits::Actor, + Inner: markers::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 {} +impl AsApObject for ApActor +where + Inner1: AsApObject, +{ + fn ap_object_ref(&self) -> &ApObject { + self.inner.ap_object_ref() + } + + fn ap_object_mut(&mut self) -> &mut ApObject { + self.inner.ap_object_mut() + } +} + +impl ApActorExt for T where T: AsApActor {} diff --git a/src/base.rs b/src/base.rs new file mode 100644 index 0000000..1e93bff --- /dev/null +++ b/src/base.rs @@ -0,0 +1,534 @@ +use crate::{ + either::Either, + markers, + primitives::{AnyString, MimeMediaType, OneOrMany, XsdAnyUri, XsdString}, + unparsed::{Unparsed, UnparsedMut}, +}; +use typed_builder::TypedBuilder; + +pub trait Extends: Sized { + type Error: std::error::Error; + + fn extends(base: Base) -> Result; + + fn retracts(self) -> Result, Self::Error>; +} + +pub trait AsBase: markers::Base { + fn base_ref(&self) -> &Base; + fn base_mut(&mut self) -> &mut Base; +} + +pub trait BaseExt: AsBase { + fn context<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.base_ref().context.as_ref() + } + + fn set_context(&mut self, context: T) -> &mut Self + where + T: Into, + { + self.base_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.base_mut().context = Some(v.into()); + self + } + + fn add_context(&mut self, context: T) -> &mut Self + where + T: Into, + { + let c = match self.base_mut().context.take() { + Some(mut c) => { + c.add(context.into()); + c + } + None => vec![context.into()].into(), + }; + self.base_mut().context = Some(c); + self + } + + fn take_context(&mut self) -> Option> { + self.base_mut().context.take() + } + + fn delete_context(&mut self) -> &mut Self { + self.base_mut().context = None; + self + } + + fn id<'a>(&'a self) -> Option<&'a XsdAnyUri> + where + Kind: 'a, + { + self.base_ref().id.as_ref() + } + + fn set_id(&mut self, id: XsdAnyUri) -> &mut Self { + self.base_mut().id = Some(id); + self + } + + fn take_id(&mut self) -> Option { + self.base_mut().id.take() + } + + fn delete_id(&mut self) -> &mut Self { + self.base_mut().id = None; + self + } + + fn kind<'a>(&'a self) -> Option<&'a Kind> + where + Kind: 'a, + { + self.base_ref().kind.as_ref() + } + + fn set_kind(&mut self, kind: Kind) -> &mut Self { + self.base_mut().kind = Some(kind); + self + } + + fn take_kind(&mut self) -> Option { + self.base_mut().kind.take() + } + + fn delete_kind(&mut self) -> &mut Self { + self.base_mut().kind = None; + self + } + + fn name<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.base_ref().name.as_ref() + } + + fn set_name(&mut self, name: T) -> &mut Self + where + T: Into, + { + self.base_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.base_mut().name = Some(v.into()); + self + } + + fn add_name(&mut self, name: T) -> &mut Self + where + T: Into, + { + let a = match self.base_mut().name.take() { + Some(mut a) => { + a.add(name.into()); + a + } + None => vec![name.into()].into(), + }; + self.base_mut().name = Some(a); + self + } + + fn take_name(&mut self) -> Option> { + self.base_mut().name.take() + } + + fn delete_name(&mut self) -> &mut Self { + self.base_mut().name = None; + self + } + + fn media_type<'a>(&'a self) -> Option<&'a MimeMediaType> + where + Kind: 'a, + { + self.base_ref().media_type.as_ref() + } + + fn set_media_type(&mut self, media_type: MimeMediaType) -> &mut Self { + self.base_mut().media_type = Some(media_type); + self + } + + fn take_media_type(&mut self) -> Option { + self.base_mut().media_type.take() + } + + fn delete_media_type(&mut self) -> &mut Self { + self.base_mut().media_type = None; + self + } + + fn preview<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.base_ref().preview.as_ref() + } + + fn set_preview(&mut self, preview: T) -> &mut Self + where + T: Into, + { + self.base_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.base_mut().preview = Some(v.into()); + self + } + + fn add_preview(&mut self, preview: T) -> &mut Self + where + T: Into, + { + let a = match self.base_mut().preview.take() { + Some(mut a) => { + a.add(preview.into()); + a + } + None => vec![preview.into()].into(), + }; + self.base_mut().preview = Some(a); + self + } + + fn take_preview(&mut self) -> Option> { + self.base_mut().preview.take() + } + + fn delete_preview(&mut self) -> &mut Self { + self.base_mut().preview = None; + self + } +} + +#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[serde(transparent)] +struct IdOrBase(Either>>); + +#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[serde(transparent)] +pub struct AnyBase(Either); + +#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize, TypedBuilder)] +#[serde(rename_all = "camelCase")] +#[builder(doc)] +pub struct Base { + #[serde(skip_serializing_if = "Option::is_none")] + #[builder(default, setter(strip_option, into))] + pub context: Option>, + + #[serde(skip_serializing_if = "Option::is_none")] + #[builder(default, setter(strip_option, into))] + pub id: Option, + + #[serde(rename = "type")] + #[serde(skip_serializing_if = "Option::is_none")] + #[builder(default, setter(strip_option))] + pub kind: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + #[builder(default, setter(strip_option, into))] + pub name: Option>, + + #[serde(skip_serializing_if = "Option::is_none")] + #[builder(default, setter(strip_option))] + pub media_type: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + #[builder(default, setter(strip_option, into))] + pub preview: Option>, + + #[serde(flatten)] + #[builder(default)] + pub unparsed: Unparsed, +} + +impl Base { + pub fn solidify(self) -> Result, serde_json::Error> + where + Kind: serde::de::DeserializeOwned, + { + self.try_map_kind(serde_json::from_value) + } +} + +impl Base { + pub fn extend(self) -> Result + where + T: Extends, + { + T::extends(self) + } + + pub fn retract(t: T) -> Result + where + T: Extends, + { + t.retracts() + } + + pub fn into_generic(self) -> Result, serde_json::Error> + where + Kind: serde::ser::Serialize, + { + self.try_map_kind(serde_json::to_value) + } + + pub fn map_kind(self, f: impl Fn(Kind) -> NewKind) -> Base { + Base { + kind: self.kind.map(f), + context: self.context, + id: self.id, + name: self.name, + media_type: self.media_type, + preview: self.preview, + unparsed: self.unparsed, + } + } + + pub fn try_map_kind( + self, + f: impl Fn(Kind) -> Result, + ) -> Result, E> { + Ok(Base { + kind: if let Some(kind) = self.kind { + Some((f)(kind)?) + } else { + None + }, + context: self.context, + id: self.id, + name: self.name, + media_type: self.media_type, + preview: self.preview, + unparsed: self.unparsed, + }) + } +} + +impl AnyBase { + pub fn as_xsd_any_uri(&self) -> Option<&XsdAnyUri> { + self.0.as_ref().left().and_then(|l| l.as_xsd_any_uri()) + } + + pub fn as_xsd_string(&self) -> Option<&XsdString> { + self.0.as_ref().right() + } + + pub fn as_base(&self) -> Option<&Base> { + self.0.as_ref().left().and_then(|l| l.as_base()) + } + + pub fn id(self) -> Option { + self.0.left().and_then(|l| l.id()) + } + + pub fn xsd_string(self) -> Option { + self.0.right() + } + + pub fn base(self) -> Option> { + self.0.left().and_then(|l| l.base()) + } + + pub fn set_xsd_any_uri(&mut self, id: XsdAnyUri) { + self.0 = Either::Left(IdOrBase::from_xsd_any_uri(id)); + } + + pub fn set_base(&mut self, base: Base) { + self.0 = Either::Left(IdOrBase::from_base(base)); + } + + pub fn set_xsd_string(&mut self, xsd_string: XsdString) { + self.0 = Either::Right(xsd_string); + } + + pub fn from_xsd_any_uri(id: XsdAnyUri) -> Self { + AnyBase(Either::Left(IdOrBase::from_xsd_any_uri(id))) + } + + pub fn from_base(base: Base) -> Self { + AnyBase(Either::Left(IdOrBase::from_base(base))) + } + + pub fn from_xsd_string(xsd_string: XsdString) -> Self { + AnyBase(Either::Right(xsd_string)) + } +} + +impl IdOrBase { + fn as_xsd_any_uri(&self) -> Option<&XsdAnyUri> { + self.0.as_ref().left() + } + + fn as_base(&self) -> Option<&Base> { + self.0.as_ref().right().map(|b| b.as_ref()) + } + + fn id(self) -> Option { + self.0.left() + } + + fn base(self) -> Option> { + self.0.right().map(|b| *b) + } + + fn from_xsd_any_uri(id: XsdAnyUri) -> Self { + IdOrBase(Either::Left(id)) + } + + fn from_base(base: Base) -> Self { + IdOrBase(Either::Right(Box::new(base))) + } +} + +impl OneOrMany { + pub fn as_single_xsd_any_uri(&self) -> Option<&XsdAnyUri> { + self.as_one().and_then(|inner| inner.as_xsd_any_uri()) + } + + pub fn as_single_xsd_string(&self) -> Option<&XsdString> { + self.as_one().and_then(|inner| inner.as_xsd_string()) + } + + pub fn as_single_base(&self) -> Option<&Base> { + self.as_one().and_then(|inner| inner.as_base()) + } + + pub fn single_xsd_any_uri(self) -> Option { + self.one().and_then(|inner| inner.id()) + } + + pub fn single_xsd_string(self) -> Option { + self.one().and_then(|inner| inner.xsd_string()) + } + + pub fn single_base(self) -> Option> { + self.one().and_then(|inner| inner.base()) + } + + pub fn from_xsd_any_uri(id: XsdAnyUri) -> Self { + OneOrMany(Either::Left(AnyBase::from_xsd_any_uri(id))) + } + + pub fn from_xsd_string(xsd_string: XsdString) -> Self { + OneOrMany(Either::Left(AnyBase::from_xsd_string(xsd_string))) + } + + pub fn from_base(base: Base) -> Self { + OneOrMany(Either::Left(AnyBase::from_base(base))) + } + + pub fn set_single_xsd_any_uri(&mut self, id: XsdAnyUri) -> &mut Self { + self.0 = Either::Left(AnyBase::from_xsd_any_uri(id)); + self + } + + pub fn set_single_xsd_string(&mut self, xsd_string: XsdString) -> &mut Self { + self.0 = Either::Left(AnyBase::from_xsd_string(xsd_string)); + self + } + + pub fn set_single_base(&mut self, base: Base) -> &mut Self { + self.0 = Either::Left(AnyBase::from_base(base)); + self + } + + pub fn add_xsd_any_uri(&mut self, id: XsdAnyUri) -> &mut Self { + self.add(AnyBase::from_xsd_any_uri(id)) + } + + pub fn add_xsd_string(&mut self, xsd_string: XsdString) -> &mut Self { + self.add(AnyBase::from_xsd_string(xsd_string)) + } + + pub fn add_base(&mut self, base: Base) -> &mut Self { + self.add(AnyBase::from_base(base)) + } +} + +impl markers::Base for Base {} + +impl UnparsedMut for Base { + fn unparsed_mut(&mut self) -> &mut Unparsed { + &mut self.unparsed + } +} + +impl AsBase for Base { + fn base_ref(&self) -> &Base { + self + } + + fn base_mut(&mut self) -> &mut Base { + self + } +} + +impl BaseExt for T where T: AsBase {} + +impl From> for AnyBase { + fn from(o: Base) -> Self { + Self::from_base(o) + } +} + +impl From for AnyBase { + fn from(id: XsdAnyUri) -> Self { + Self::from_xsd_any_uri(id) + } +} + +impl From for AnyBase { + fn from(xsd_string: XsdString) -> Self { + Self::from_xsd_string(xsd_string) + } +} + +impl From> for OneOrMany { + fn from(object: Base) -> Self { + Self::from_base(object) + } +} + +impl From for OneOrMany { + fn from(xsd_any_uri: XsdAnyUri) -> Self { + Self::from_xsd_any_uri(xsd_any_uri) + } +} + +impl From for OneOrMany { + fn from(xsd_string: XsdString) -> Self { + Self::from_xsd_string(xsd_string) + } +} diff --git a/src/collection.rs b/src/collection.rs index 8e009c8..8b04489 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -1,7 +1,9 @@ use crate::{ - object::{AnyObject, ApObject, Object, ObjectMut, ObjectRef}, - primitives::{OneOrMany, Unparsed, XsdNonNegativeInteger}, - traits::{self, Extends, UnparsedMut, UnparsedMutExt}, + base::{AnyBase, AsBase, Base, Extends}, + markers, + object::{ApObject, AsObject, Object}, + primitives::{OneOrMany, XsdNonNegativeInteger}, + unparsed::{Unparsed, UnparsedMut, UnparsedMutExt}, }; use std::convert::TryFrom; use typed_builder::TypedBuilder; @@ -12,71 +14,32 @@ pub mod kind { use self::kind::*; -pub trait CollectionRef: traits::Collection { +pub trait AsCollection: markers::Collection { fn collection_ref(&self) -> &Collection; -} - -pub trait CollectionMut: traits::Collection { fn collection_mut(&mut self) -> &mut Collection; } -pub trait CollectionPageRef: traits::CollectionPage { +pub trait AsCollectionPage: markers::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 { +pub trait AsOrderedCollectionPage: markers::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 +pub trait CollectionExt: AsCollection { + 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, + T: Into, { self.collection_mut().items = item.into().into(); self @@ -85,7 +48,7 @@ pub trait CollectionMutExt: CollectionMut { fn set_many_items(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.collection_mut().items = v.into(); @@ -94,12 +57,19 @@ pub trait CollectionMutExt: CollectionMut { fn add_item(&mut self, item: T) -> &mut Self where - T: Into, + T: Into, { self.collection_mut().items.add(item.into()); self } + fn total_items<'a>(&'a self) -> Option<&'a XsdNonNegativeInteger> + where + Kind: 'a, + { + self.collection_ref().total_items.as_ref() + } + fn set_total_items(&mut self, total_items: T) -> &mut Self where T: Into, @@ -117,15 +87,22 @@ pub trait CollectionMutExt: CollectionMut { self } + fn current<'a>(&'a self) -> Option<&'a AnyBase> + where + Kind: 'a, + { + self.collection_ref().current.as_ref() + } + fn set_current(&mut self, current: T) -> &mut Self where - T: Into, + T: Into, { self.collection_mut().current = Some(current.into()); self } - fn take_current(&mut self) -> Option { + fn take_current(&mut self) -> Option { self.collection_mut().current.take() } @@ -134,15 +111,22 @@ pub trait CollectionMutExt: CollectionMut { self } + fn first<'a>(&'a self) -> Option<&'a AnyBase> + where + Kind: 'a, + { + self.collection_ref().first.as_ref() + } + fn set_first(&mut self, first: T) -> &mut Self where - T: Into, + T: Into, { self.collection_mut().first = Some(first.into()); self } - fn take_first(&mut self) -> Option { + fn take_first(&mut self) -> Option { self.collection_mut().first.take() } @@ -151,15 +135,22 @@ pub trait CollectionMutExt: CollectionMut { self } + fn last<'a>(&'a self) -> Option<&'a AnyBase> + where + Kind: 'a, + { + self.collection_ref().last.as_ref() + } + fn set_last(&mut self, last: T) -> &mut Self where - T: Into, + T: Into, { self.collection_mut().last = Some(last.into()); self } - fn take_last(&mut self) -> Option { + fn take_last(&mut self) -> Option { self.collection_mut().last.take() } @@ -169,39 +160,23 @@ pub trait CollectionMutExt: CollectionMut { } } -pub trait CollectionPageRefExt: CollectionPageRef { - fn part_of<'a>(&'a self) -> Option<&'a AnyObject> +pub trait CollectionPageExt: AsCollectionPage { + fn part_of<'a>(&'a self) -> Option<&'a AnyBase> where Kind: 'a, { self.collection_page_ref().part_of.as_ref() } - fn 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, + T: Into, { self.collection_page_mut().part_of = Some(part_of.into()); self } - fn take_part_of(&mut self) -> Option { + fn take_part_of(&mut self) -> Option { self.collection_page_mut().part_of.take() } @@ -210,15 +185,22 @@ pub trait CollectionPageMutExt: CollectionPageMut { self } + fn next<'a>(&'a self) -> Option<&'a AnyBase> + where + Kind: 'a, + { + self.collection_page_ref().next.as_ref() + } + fn set_next(&mut self, next: T) -> &mut Self where - T: Into, + T: Into, { self.collection_page_mut().next = Some(next.into()); self } - fn take_next(&mut self) -> Option { + fn take_next(&mut self) -> Option { self.collection_page_mut().next.take() } @@ -227,15 +209,22 @@ pub trait CollectionPageMutExt: CollectionPageMut { self } + fn prev<'a>(&'a self) -> Option<&'a AnyBase> + where + Kind: 'a, + { + self.collection_page_ref().prev.as_ref() + } + fn set_prev(&mut self, prev: T) -> &mut Self where - T: Into, + T: Into, { self.collection_page_mut().prev = Some(prev.into()); self } - fn take_prev(&mut self) -> Option { + fn take_prev(&mut self) -> Option { self.collection_page_mut().prev.take() } @@ -245,13 +234,11 @@ pub trait CollectionPageMutExt: CollectionPageMut { } } -pub trait OrderedCollectionPageRefExt: OrderedCollectionPageRef { +pub trait OrderedCollectionPageExt: AsOrderedCollectionPage { 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, @@ -279,7 +266,7 @@ pub type UnorderedCollectionPage = CollectionPage; #[builder(doc)] pub struct Collection { #[builder(setter(into))] - pub items: OneOrMany, + pub items: OneOrMany, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] @@ -287,15 +274,15 @@ pub struct Collection { #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub current: Option, + pub current: Option, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub first: Option, + pub first: Option, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub last: Option, + pub last: Option, #[serde(flatten)] pub inner: Object, @@ -307,15 +294,15 @@ pub struct Collection { pub struct CollectionPage { #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub part_of: Option, + pub part_of: Option, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub next: Option, + pub next: Option, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub prev: Option, + pub prev: Option, #[serde(flatten)] pub inner: Collection, @@ -426,29 +413,34 @@ impl OrderedCollectionPage { } } -impl traits::Base for Collection {} -impl traits::Object for Collection {} -impl traits::Collection for Collection {} +impl markers::Base for Collection {} +impl markers::Object for Collection {} +impl markers::Collection for Collection {} -impl traits::Base for CollectionPage {} -impl traits::Object for CollectionPage {} -impl traits::Collection for CollectionPage {} -impl traits::CollectionPage for CollectionPage {} +impl markers::Base for CollectionPage {} +impl markers::Object for CollectionPage {} +impl markers::Collection for CollectionPage {} +impl markers::CollectionPage for CollectionPage {} -impl traits::Base for OrderedCollectionPage {} -impl traits::Object for OrderedCollectionPage {} -impl traits::Collection for OrderedCollectionPage {} -impl traits::CollectionPage for OrderedCollectionPage {} +impl markers::Base for OrderedCollectionPage {} +impl markers::Object for OrderedCollectionPage {} +impl markers::Collection for OrderedCollectionPage {} +impl markers::CollectionPage for OrderedCollectionPage {} -impl Extends> for Collection { +impl Extends for Collection +where + Kind: serde::de::DeserializeOwned + serde::ser::Serialize, +{ type Error = serde_json::Error; - fn extends(object: Object) -> Result { - Self::extending(object) + fn extends(base: Base) -> Result { + let inner = Object::extends(base)?; + Self::extending(inner) } - fn retracts(self) -> Result, Self::Error> { - self.retracting() + fn retracts(self) -> Result, Self::Error> { + let inner = self.retracting()?; + inner.retracts() } } @@ -468,15 +460,20 @@ impl TryFrom> for Collection { } } -impl Extends> for CollectionPage { +impl Extends for CollectionPage +where + Kind: serde::de::DeserializeOwned + serde::ser::Serialize, +{ type Error = serde_json::Error; - fn extends(object: Object) -> Result { - Self::extending(object) + fn extends(base: Base) -> Result { + let inner = Object::extends(base)?; + Self::extending(inner) } - fn retracts(self) -> Result, Self::Error> { - self.retracting() + fn retracts(self) -> Result, Self::Error> { + let inner = self.retracting()?; + inner.retracts() } } @@ -496,15 +493,17 @@ impl TryFrom> for Object { } } -impl Extends> for OrderedCollectionPage { +impl Extends for OrderedCollectionPage { type Error = serde_json::Error; - fn extends(object: Object) -> Result { - Self::extending(object) + fn extends(base: Base) -> Result { + let inner = Object::extends(base)?; + Self::extending(inner) } - fn retracts(self) -> Result, Self::Error> { - self.retracting() + fn retracts(self) -> Result, Self::Error> { + let inner = self.retracting()?; + inner.retracts() } } @@ -542,153 +541,158 @@ impl UnparsedMut for OrderedCollectionPage { } } -impl ObjectRef for Collection { +impl AsBase for Collection { + fn base_ref(&self) -> &Base { + self.inner.base_ref() + } + + fn base_mut(&mut self) -> &mut Base { + self.inner.base_mut() + } +} + +impl AsObject for Collection { fn object_ref(&self) -> &Object { &self.inner } -} -impl ObjectMut for Collection { + fn object_mut(&mut self) -> &mut Object { &mut self.inner } } -impl CollectionRef for Collection { +impl AsCollection for Collection { fn collection_ref(&self) -> &Collection { self } -} -impl CollectionMut for Collection { + fn collection_mut(&mut self) -> &mut Collection { self } } -impl ObjectRef for CollectionPage { +impl AsBase for CollectionPage { + fn base_ref(&self) -> &Base { + self.inner.base_ref() + } + + fn base_mut(&mut self) -> &mut Base { + self.inner.base_mut() + } +} + +impl AsObject for CollectionPage { fn object_ref(&self) -> &Object { self.inner.object_ref() } -} -impl ObjectMut for CollectionPage { + fn object_mut(&mut self) -> &mut Object { self.inner.object_mut() } } -impl CollectionRef for CollectionPage { +impl AsCollection 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 { +impl AsCollectionPage 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 { +impl AsBase for OrderedCollectionPage { + fn base_ref(&self) -> &Base { + self.inner.base_ref() + } + + fn base_mut(&mut self) -> &mut Base { + self.inner.base_mut() + } +} + +impl AsObject for OrderedCollectionPage { fn object_ref(&self) -> &Object { self.inner.object_ref() } -} -impl ObjectMut for OrderedCollectionPage { + fn object_mut(&mut self) -> &mut Object { self.inner.object_mut() } } -impl CollectionRef for OrderedCollectionPage { +impl AsCollection 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 { +impl AsCollectionPage 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 markers::Collection for ApObject where Inner: markers::Collection {} +impl markers::CollectionPage for ApObject where Inner: markers::CollectionPage {} -impl CollectionRef for ApObject +impl AsCollection for ApObject where - Inner: CollectionRef, + Inner: AsCollection, { 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 +impl AsCollectionPage for ApObject where - Inner: CollectionPageRef, + Inner: AsCollectionPage, { 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 +impl AsOrderedCollectionPage for ApObject where - Inner: OrderedCollectionPageRef, + Inner: AsOrderedCollectionPage, { 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 {} +impl CollectionExt for T where T: AsCollection {} +impl CollectionPageExt for T where T: AsCollectionPage {} +impl OrderedCollectionPageExt for T where T: AsOrderedCollectionPage {} diff --git a/src/lib.rs b/src/lib.rs index acf94b1..003886a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,29 +1,31 @@ pub mod activity; pub mod actor; +pub mod base; pub mod collection; pub mod either; +pub mod link; pub mod object; pub mod primitives; -pub mod traits; +pub mod unparsed; pub use activitystreams::{context, public, security}; +pub mod markers { + pub use activitystreams::{ + Activity, Actor, Base, Collection, CollectionPage, IntransitiveActivity, Link, Object, + }; +} + 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, + ActivityExt, ActorAndObjectRefExt, OptOriginRefExt, OptTargetRefExt, OriginRefExt, + QuestionExt, TargetRefExt, }, + actor::ApActorExt, + base::BaseExt, + collection::{CollectionExt, CollectionPageExt, OrderedCollectionPageExt}, + link::LinkExt, + object::{ApObjectExt, ObjectExt, PlaceExt, ProfileExt, RelationshipExt, TombstoneExt}, }; } diff --git a/src/link.rs b/src/link.rs new file mode 100644 index 0000000..4974a1f --- /dev/null +++ b/src/link.rs @@ -0,0 +1,284 @@ +use crate::{ + base::{AsBase, Base, Extends}, + markers, + primitives::{OneOrMany, XsdAnyUri, XsdNonNegativeInteger, XsdString}, + unparsed::{Unparsed, UnparsedMut, UnparsedMutExt}, +}; +use std::convert::TryFrom; +use typed_builder::TypedBuilder; + +pub mod kind { + pub use activitystreams::link::kind::MentionType; +} + +use self::kind::MentionType; + +pub trait AsLink: markers::Link { + fn link_ref(&self) -> &Link; + fn link_mut(&mut self) -> &mut Link; +} + +pub trait LinkExt: AsLink { + fn href<'a>(&'a self) -> Option<&'a XsdAnyUri> + where + Kind: 'a, + { + self.link_ref().href.as_ref() + } + + fn set_href(&mut self, href: XsdAnyUri) -> &mut Self { + self.link_mut().href = Some(href); + self + } + + fn take_href(&mut self) -> Option { + self.link_mut().href.take() + } + + fn delete_href(&mut self) -> &mut Self { + self.link_mut().href = None; + self + } + + fn hreflang<'a>(&'a self) -> Option<&'a XsdString> + where + Kind: 'a, + { + self.link_ref().hreflang.as_ref() + } + + fn set_hreflang(&mut self, hreflang: XsdString) -> &mut Self { + self.link_mut().hreflang = Some(hreflang); + self + } + + fn take_hreflang(&mut self) -> Option { + self.link_mut().hreflang.take() + } + + fn delete_hreflang(&mut self) -> &mut Self { + self.link_mut().hreflang = None; + self + } + + fn rel<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.link_ref().rel.as_ref() + } + + fn set_rel(&mut self, rel: XsdString) -> &mut Self { + self.link_mut().rel = Some(rel.into()); + self + } + + fn set_many_rels(&mut self, items: I) -> &mut Self + where + I: IntoIterator, + { + let v: Vec<_> = items.into_iter().collect(); + self.link_mut().rel = Some(v.into()); + self + } + + fn add_rel(&mut self, rel: XsdString) -> &mut Self { + let v = match self.link_mut().rel.take() { + Some(mut v) => { + v.add(rel); + v + } + None => vec![rel].into(), + }; + self.link_mut().rel = Some(v); + self + } + + fn take_rel(&mut self) -> Option> { + self.link_mut().rel.take() + } + + fn delete_rel(&mut self) -> &mut Self { + self.link_mut().rel = None; + self + } + + fn height<'a>(&'a self) -> Option<&'a XsdNonNegativeInteger> + where + Kind: 'a, + { + self.link_ref().height.as_ref() + } + + fn set_height(&mut self, height: T) -> &mut Self + where + T: Into, + { + self.link_mut().height = Some(height.into()); + self + } + + fn take_height(&mut self) -> Option { + self.link_mut().height.take() + } + + fn delete_height(&mut self) -> &mut Self { + self.link_mut().height = None; + self + } + + fn width<'a>(&'a self) -> Option<&'a XsdNonNegativeInteger> + where + Kind: 'a, + { + self.link_ref().width.as_ref() + } + + fn set_width(&mut self, width: T) -> &mut Self + where + T: Into, + { + self.link_mut().width = Some(width.into()); + self + } + + fn take_width(&mut self) -> Option { + self.link_mut().width.take() + } + + fn delete_width(&mut self) -> &mut Self { + self.link_mut().width = None; + self + } +} + +pub type Mention = Link; + +#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize, TypedBuilder)] +#[serde(rename_all = "camelCase")] +#[builder(doc)] +pub struct Link { + #[serde(skip_serializing_if = "Option::is_none")] + #[builder(default, setter(strip_option))] + href: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + #[builder(default, setter(strip_option))] + hreflang: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + #[builder(default, setter(strip_option, into))] + rel: Option>, + + #[serde(skip_serializing_if = "Option::is_none")] + #[builder(default, setter(strip_option, into))] + height: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + #[builder(default, setter(strip_option, into))] + width: Option, + + #[serde(flatten)] + inner: Base, +} + +impl Link { + fn extending(mut inner: Base) -> Result { + Ok(Link { + href: inner.remove("href")?, + hreflang: inner.remove("hreflang")?, + rel: inner.remove("rel")?, + height: inner.remove("height")?, + width: inner.remove("width")?, + inner, + }) + } + + fn retracting(self) -> Result, serde_json::Error> { + let Link { + href, + hreflang, + rel, + height, + width, + mut inner, + } = self; + + inner + .insert("href", href)? + .insert("hreflang", hreflang)? + .insert("rel", rel)? + .insert("height", height)? + .insert("width", width)?; + + Ok(inner) + } +} + +impl markers::Base for Link {} +impl markers::Link for Link {} + +impl Extends for Link +where + Kind: serde::de::DeserializeOwned + serde::ser::Serialize, +{ + type Error = serde_json::Error; + + fn extends(base: Base) -> Result { + Self::extending(base) + } + + fn retracts(self) -> Result, Self::Error> { + self.retracting() + } +} + +impl TryFrom> for Link +where + Kind: serde::de::DeserializeOwned, +{ + type Error = serde_json::Error; + + fn try_from(base: Base) -> Result { + Self::extending(base) + } +} + +impl TryFrom> for Base +where + Kind: serde::ser::Serialize, +{ + type Error = serde_json::Error; + + fn try_from(link: Link) -> Result { + link.retracting() + } +} + +impl UnparsedMut for Link { + fn unparsed_mut(&mut self) -> &mut Unparsed { + self.inner.unparsed_mut() + } +} + +impl AsBase for Link { + fn base_ref(&self) -> &Base { + &self.inner + } + + fn base_mut(&mut self) -> &mut Base { + &mut self.inner + } +} + +impl AsLink for Link { + fn link_ref(&self) -> &Link { + self + } + + fn link_mut(&mut self) -> &mut Link { + self + } +} + +impl LinkExt for T where T: AsLink {} diff --git a/src/object.rs b/src/object.rs index 467abb0..fd5ef14 100644 --- a/src/object.rs +++ b/src/object.rs @@ -1,10 +1,8 @@ use crate::{ - either::Either, - primitives::{ - AnyString, MimeMediaType, OneOrMany, Unit, Unparsed, XsdAnyUri, XsdDateTime, XsdDuration, - XsdFloat, XsdString, - }, - traits::{self, Extends, UnparsedMut, UnparsedMutExt}, + base::{AnyBase, AsBase, Base, Extends}, + markers, + primitives::{AnyString, OneOrMany, Unit, XsdAnyUri, XsdDateTime, XsdDuration, XsdFloat}, + unparsed::{Unparsed, UnparsedMut, UnparsedMutExt}, }; use std::convert::TryFrom; use typed_builder::TypedBuilder; @@ -15,312 +13,47 @@ pub mod kind { use self::kind::*; -pub trait ObjectRef: traits::Object { +pub trait AsObject: markers::Object { fn object_ref(&self) -> &Object; -} - -pub trait ObjectMut: traits::Object { fn object_mut(&mut self) -> &mut Object; } -pub trait ApObjectRef: traits::Object { +pub trait AsApObject: markers::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 { +pub trait AsPlace: markers::Object { fn place_ref(&self) -> &Place; -} - -pub trait PlaceMut: traits::Object { fn place_mut(&mut self) -> &mut Place; } -pub trait ProfileRef: traits::Object { +pub trait AsProfile: markers::Object { fn profile_ref(&self) -> &Profile; -} - -pub trait ProfileMut: traits::Object { fn profile_mut(&mut self) -> &mut Profile; } -pub trait RelationshipRef: traits::Object { +pub trait AsRelationship: markers::Object { fn relationship_ref(&self) -> &Relationship; -} - -pub trait RelationshipMut: traits::Object { fn relationship_mut(&mut self) -> &mut Relationship; } -pub trait TombstoneRef: traits::Object { +pub trait AsTombstone: markers::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> +pub trait ObjectExt: AsObject { + 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, + T: Into, { self.object_mut().attachment = Some(attachment.into().into()); self @@ -329,7 +62,7 @@ pub trait ObjectMutExt: ObjectMut { fn set_many_attachments(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.object_mut().attachment = Some(v.into()); @@ -338,7 +71,7 @@ pub trait ObjectMutExt: ObjectMut { fn add_attachment(&mut self, attachment: T) -> &mut Self where - T: Into, + T: Into, { let a = match self.object_mut().attachment.take() { Some(mut a) => { @@ -351,7 +84,7 @@ pub trait ObjectMutExt: ObjectMut { self } - fn take_attachment(&mut self) -> Option> { + fn take_attachment(&mut self) -> Option> { self.object_mut().attachment.take() } @@ -360,9 +93,16 @@ pub trait ObjectMutExt: ObjectMut { self } + fn attributed_to<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().attributed_to.as_ref() + } + fn set_attributed_to(&mut self, attributed_to: T) -> &mut Self where - T: Into, + T: Into, { self.object_mut().attributed_to = Some(attributed_to.into().into()); self @@ -371,7 +111,7 @@ pub trait ObjectMutExt: ObjectMut { fn set_many_attributed_tos(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.object_mut().attributed_to = Some(v.into()); @@ -380,7 +120,7 @@ pub trait ObjectMutExt: ObjectMut { fn add_attributed_to(&mut self, attributed_to: T) -> &mut Self where - T: Into, + T: Into, { let a = match self.object_mut().attributed_to.take() { Some(mut a) => { @@ -393,7 +133,7 @@ pub trait ObjectMutExt: ObjectMut { self } - fn take_attributed_to(&mut self) -> Option> { + fn take_attributed_to(&mut self) -> Option> { self.object_mut().attributed_to.take() } @@ -402,9 +142,16 @@ pub trait ObjectMutExt: ObjectMut { self } + fn audience<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().audience.as_ref() + } + fn set_audience(&mut self, audience: T) -> &mut Self where - T: Into, + T: Into, { self.object_mut().audience = Some(audience.into().into()); self @@ -413,7 +160,7 @@ pub trait ObjectMutExt: ObjectMut { fn set_many_audiences(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.object_mut().audience = Some(v.into()); @@ -422,7 +169,7 @@ pub trait ObjectMutExt: ObjectMut { fn add_audience(&mut self, audience: T) -> &mut Self where - T: Into, + T: Into, { let a = match self.object_mut().audience.take() { Some(mut a) => { @@ -435,7 +182,7 @@ pub trait ObjectMutExt: ObjectMut { self } - fn take_audience(&mut self) -> Option> { + fn take_audience(&mut self) -> Option> { self.object_mut().audience.take() } @@ -444,6 +191,13 @@ pub trait ObjectMutExt: ObjectMut { self } + fn content<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().content.as_ref() + } + fn set_content(&mut self, content: T) -> &mut Self where T: Into, @@ -486,46 +240,11 @@ pub trait ObjectMutExt: ObjectMut { self } - fn set_name(&mut self, name: T) -> &mut Self + fn summary<'a>(&'a self) -> Option<&'a OneOrMany> where - T: Into, + Kind: 'a, { - 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 + self.object_ref().summary.as_ref() } fn set_summary(&mut self, summary: T) -> &mut Self @@ -570,9 +289,16 @@ pub trait ObjectMutExt: ObjectMut { self } + fn url<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().url.as_ref() + } + fn set_url(&mut self, url: T) -> &mut Self where - T: Into, + T: Into, { self.object_mut().url = Some(url.into().into()); self @@ -581,7 +307,7 @@ pub trait ObjectMutExt: ObjectMut { fn set_many_urls(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.object_mut().url = Some(v.into()); @@ -590,7 +316,7 @@ pub trait ObjectMutExt: ObjectMut { fn add_url(&mut self, url: T) -> &mut Self where - T: Into, + T: Into, { let a = match self.object_mut().url.take() { Some(mut a) => { @@ -603,7 +329,7 @@ pub trait ObjectMutExt: ObjectMut { self } - fn take_url(&mut self) -> Option> { + fn take_url(&mut self) -> Option> { self.object_mut().url.take() } @@ -612,23 +338,16 @@ pub trait ObjectMutExt: ObjectMut { 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 generator<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().generator.as_ref() } fn set_generator(&mut self, generator: T) -> &mut Self where - T: Into, + T: Into, { self.object_mut().generator = Some(generator.into().into()); self @@ -637,7 +356,7 @@ pub trait ObjectMutExt: ObjectMut { fn set_many_generators(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.object_mut().generator = Some(v.into()); @@ -646,7 +365,7 @@ pub trait ObjectMutExt: ObjectMut { fn add_generator(&mut self, generator: T) -> &mut Self where - T: Into, + T: Into, { let a = match self.object_mut().generator.take() { Some(mut a) => { @@ -659,7 +378,7 @@ pub trait ObjectMutExt: ObjectMut { self } - fn take_generator(&mut self) -> Option> { + fn take_generator(&mut self) -> Option> { self.object_mut().generator.take() } @@ -668,9 +387,16 @@ pub trait ObjectMutExt: ObjectMut { self } + fn image<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().image.as_ref() + } + fn set_image(&mut self, image: T) -> &mut Self where - T: Into, + T: Into, { self.object_mut().image = Some(image.into().into()); self @@ -679,7 +405,7 @@ pub trait ObjectMutExt: ObjectMut { fn set_many_images(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.object_mut().image = Some(v.into()); @@ -688,7 +414,7 @@ pub trait ObjectMutExt: ObjectMut { fn add_image(&mut self, image: T) -> &mut Self where - T: Into, + T: Into, { let a = match self.object_mut().image.take() { Some(mut a) => { @@ -701,7 +427,7 @@ pub trait ObjectMutExt: ObjectMut { self } - fn take_image(&mut self) -> Option> { + fn take_image(&mut self) -> Option> { self.object_mut().image.take() } @@ -710,9 +436,16 @@ pub trait ObjectMutExt: ObjectMut { self } + fn location<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().location.as_ref() + } + fn set_location(&mut self, location: T) -> &mut Self where - T: Into, + T: Into, { self.object_mut().location = Some(location.into().into()); self @@ -721,7 +454,7 @@ pub trait ObjectMutExt: ObjectMut { fn set_many_locations(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.object_mut().location = Some(v.into()); @@ -730,7 +463,7 @@ pub trait ObjectMutExt: ObjectMut { fn add_location(&mut self, location: T) -> &mut Self where - T: Into, + T: Into, { let a = match self.object_mut().location.take() { Some(mut a) => { @@ -743,7 +476,7 @@ pub trait ObjectMutExt: ObjectMut { self } - fn take_location(&mut self) -> Option> { + fn take_location(&mut self) -> Option> { self.object_mut().location.take() } @@ -752,46 +485,11 @@ pub trait ObjectMutExt: ObjectMut { self } - fn set_preview(&mut self, preview: T) -> &mut Self + fn start_time<'a>(&'a self) -> Option<&'a XsdDateTime> where - T: Into, + Kind: 'a, { - 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 + self.object_ref().start_time.as_ref() } fn set_start_time(&mut self, start_time: XsdDateTime) -> &mut Self { @@ -808,6 +506,13 @@ pub trait ObjectMutExt: ObjectMut { self } + fn end_time<'a>(&'a self) -> Option<&'a XsdDateTime> + where + Kind: 'a, + { + self.object_ref().end_time.as_ref() + } + fn set_end_time(&mut self, end_time: XsdDateTime) -> &mut Self { self.object_mut().end_time = Some(end_time); self @@ -822,6 +527,13 @@ pub trait ObjectMutExt: ObjectMut { self } + fn duration<'a>(&'a self) -> Option<&'a XsdDuration> + where + Kind: 'a, + { + self.object_ref().duration.as_ref() + } + fn set_duration(&mut self, duration: XsdDuration) -> &mut Self { self.object_mut().duration = Some(duration); self @@ -836,6 +548,13 @@ pub trait ObjectMutExt: ObjectMut { self } + fn published<'a>(&'a self) -> Option<&'a XsdDateTime> + where + Kind: 'a, + { + self.object_ref().published.as_ref() + } + fn set_published(&mut self, published: XsdDateTime) -> &mut Self { self.object_mut().published = Some(published); self @@ -850,6 +569,13 @@ pub trait ObjectMutExt: ObjectMut { self } + fn updated<'a>(&'a self) -> Option<&'a XsdDateTime> + where + Kind: 'a, + { + self.object_ref().updated.as_ref() + } + fn set_updated(&mut self, updated: XsdDateTime) -> &mut Self { self.object_mut().updated = Some(updated); self @@ -864,9 +590,16 @@ pub trait ObjectMutExt: ObjectMut { self } + fn in_reply_to<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().in_reply_to.as_ref() + } + fn set_in_reply_to(&mut self, in_reply_to: T) -> &mut Self where - T: Into, + T: Into, { self.object_mut().in_reply_to = Some(in_reply_to.into().into()); self @@ -875,7 +608,7 @@ pub trait ObjectMutExt: ObjectMut { fn set_many_in_reply_tos(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.object_mut().in_reply_to = Some(v.into()); @@ -884,7 +617,7 @@ pub trait ObjectMutExt: ObjectMut { fn add_in_reply_to(&mut self, in_reply_to: T) -> &mut Self where - T: Into, + T: Into, { let a = match self.object_mut().in_reply_to.take() { Some(mut a) => { @@ -897,7 +630,7 @@ pub trait ObjectMutExt: ObjectMut { self } - fn take_in_reply_to(&mut self) -> Option> { + fn take_in_reply_to(&mut self) -> Option> { self.object_mut().in_reply_to.take() } @@ -906,9 +639,16 @@ pub trait ObjectMutExt: ObjectMut { self } + fn replies<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().replies.as_ref() + } + fn set_replies(&mut self, replies: T) -> &mut Self where - T: Into, + T: Into, { self.object_mut().replies = Some(replies.into().into()); self @@ -917,7 +657,7 @@ pub trait ObjectMutExt: ObjectMut { fn set_many_repliess(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.object_mut().replies = Some(v.into()); @@ -926,7 +666,7 @@ pub trait ObjectMutExt: ObjectMut { fn add_replies(&mut self, replies: T) -> &mut Self where - T: Into, + T: Into, { let a = match self.object_mut().replies.take() { Some(mut a) => { @@ -939,7 +679,7 @@ pub trait ObjectMutExt: ObjectMut { self } - fn take_replies(&mut self) -> Option> { + fn take_replies(&mut self) -> Option> { self.object_mut().replies.take() } @@ -948,9 +688,16 @@ pub trait ObjectMutExt: ObjectMut { self } + fn to<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().to.as_ref() + } + fn set_to(&mut self, to: T) -> &mut Self where - T: Into, + T: Into, { self.object_mut().to = Some(to.into().into()); self @@ -959,7 +706,7 @@ pub trait ObjectMutExt: ObjectMut { fn set_many_tos(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.object_mut().to = Some(v.into()); @@ -968,7 +715,7 @@ pub trait ObjectMutExt: ObjectMut { fn add_to(&mut self, to: T) -> &mut Self where - T: Into, + T: Into, { let a = match self.object_mut().to.take() { Some(mut a) => { @@ -981,7 +728,7 @@ pub trait ObjectMutExt: ObjectMut { self } - fn take_to(&mut self) -> Option> { + fn take_to(&mut self) -> Option> { self.object_mut().to.take() } @@ -990,9 +737,16 @@ pub trait ObjectMutExt: ObjectMut { self } + fn bto<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().bto.as_ref() + } + fn set_bto(&mut self, bto: T) -> &mut Self where - T: Into, + T: Into, { self.object_mut().bto = Some(bto.into().into()); self @@ -1001,7 +755,7 @@ pub trait ObjectMutExt: ObjectMut { fn set_many_btos(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.object_mut().bto = Some(v.into()); @@ -1010,7 +764,7 @@ pub trait ObjectMutExt: ObjectMut { fn add_bto(&mut self, bto: T) -> &mut Self where - T: Into, + T: Into, { let a = match self.object_mut().bto.take() { Some(mut a) => { @@ -1023,7 +777,7 @@ pub trait ObjectMutExt: ObjectMut { self } - fn take_bto(&mut self) -> Option> { + fn take_bto(&mut self) -> Option> { self.object_mut().bto.take() } @@ -1032,9 +786,16 @@ pub trait ObjectMutExt: ObjectMut { self } + fn cc<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().cc.as_ref() + } + fn set_cc(&mut self, cc: T) -> &mut Self where - T: Into, + T: Into, { self.object_mut().cc = Some(cc.into().into()); self @@ -1043,7 +804,7 @@ pub trait ObjectMutExt: ObjectMut { fn set_many_ccs(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.object_mut().cc = Some(v.into()); @@ -1052,7 +813,7 @@ pub trait ObjectMutExt: ObjectMut { fn add_cc(&mut self, cc: T) -> &mut Self where - T: Into, + T: Into, { let a = match self.object_mut().cc.take() { Some(mut a) => { @@ -1065,7 +826,7 @@ pub trait ObjectMutExt: ObjectMut { self } - fn take_cc(&mut self) -> Option> { + fn take_cc(&mut self) -> Option> { self.object_mut().cc.take() } @@ -1074,9 +835,16 @@ pub trait ObjectMutExt: ObjectMut { self } + fn bcc<'a>(&'a self) -> Option<&'a OneOrMany> + where + Kind: 'a, + { + self.object_ref().bcc.as_ref() + } + fn set_bcc(&mut self, bcc: T) -> &mut Self where - T: Into, + T: Into, { self.object_mut().bcc = Some(bcc.into().into()); self @@ -1085,7 +853,7 @@ pub trait ObjectMutExt: ObjectMut { fn set_many_bcc(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.object_mut().bcc = Some(v.into()); @@ -1094,7 +862,7 @@ pub trait ObjectMutExt: ObjectMut { fn add_bcc(&mut self, bcc: T) -> &mut Self where - T: Into, + T: Into, { let a = match self.object_mut().bcc.take() { Some(mut a) => { @@ -1107,7 +875,7 @@ pub trait ObjectMutExt: ObjectMut { self } - fn take_bcc(&mut self) -> Option> { + fn take_bcc(&mut self) -> Option> { self.object_mut().bcc.take() } @@ -1117,7 +885,7 @@ pub trait ObjectMutExt: ObjectMut { } } -pub trait ApObjectRefExt: ApObjectRef { +pub trait ApObjectExt: AsApObject { fn shares<'a>(&'a self) -> Option<&'a XsdAnyUri> where Inner: 'a, @@ -1125,29 +893,6 @@ pub trait ApObjectRefExt: ApObjectRef { 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 @@ -1162,6 +907,13 @@ pub trait ApObjectMutExt: ApObjectMut { self } + fn likes<'a>(&'a self) -> Option<&'a XsdAnyUri> + where + Inner: 'a, + { + self.ap_object_ref().likes.as_ref() + } + fn set_likes(&mut self, likes: XsdAnyUri) -> &mut Self { self.ap_object_mut().likes = Some(likes); self @@ -1176,15 +928,22 @@ pub trait ApObjectMutExt: ApObjectMut { self } + fn source<'a>(&'a self) -> Option<&'a AnyBase> + where + Inner: 'a, + { + self.ap_object_ref().source.as_ref() + } + fn set_source(&mut self, source: T) -> &mut Self where - T: Into, + T: Into, { self.ap_object_mut().source = Some(source.into()); self } - fn take_source(&mut self) -> Option { + fn take_source(&mut self) -> Option { self.ap_object_mut().source.take() } @@ -1193,6 +952,13 @@ pub trait ApObjectMutExt: ApObjectMut { self } + fn upload_media<'a>(&'a self) -> Option<&'a OneOrMany> + where + Inner: 'a, + { + self.ap_object_ref().upload_media.as_ref() + } + fn set_upload_media(&mut self, upload_media: XsdAnyUri) -> &mut Self { self.ap_object_mut().upload_media = Some(upload_media.into()); self @@ -1229,29 +995,11 @@ pub trait ApObjectMutExt: ApObjectMut { } } -pub trait PlaceRefExt: PlaceRef { +pub trait PlaceExt: AsPlace { 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, @@ -1269,6 +1017,10 @@ pub trait PlaceMutExt: PlaceMut { self } + fn altitude(&self) -> Option<&XsdFloat> { + self.place_ref().altitude.as_ref() + } + fn set_altitude(&mut self, float: T) -> &mut Self where T: Into, @@ -1286,6 +1038,10 @@ pub trait PlaceMutExt: PlaceMut { self } + fn latitude(&self) -> Option<&XsdFloat> { + self.place_ref().latitude.as_ref() + } + fn set_latitude(&mut self, float: T) -> &mut Self where T: Into, @@ -1303,6 +1059,10 @@ pub trait PlaceMutExt: PlaceMut { self } + fn longitude(&self) -> Option<&XsdFloat> { + self.place_ref().longitude.as_ref() + } + fn set_longitude(&mut self, float: T) -> &mut Self where T: Into, @@ -1320,6 +1080,10 @@ pub trait PlaceMutExt: PlaceMut { self } + fn radius(&self) -> Option<&XsdFloat> { + self.place_ref().radius.as_ref() + } + fn set_radius(&mut self, float: T) -> &mut Self where T: Into, @@ -1337,6 +1101,10 @@ pub trait PlaceMutExt: PlaceMut { self } + fn units(&self) -> Option<&Unit> { + self.place_ref().units.as_ref() + } + fn set_units(&mut self, units: T) -> &mut Self where T: Into, @@ -1355,22 +1123,20 @@ pub trait PlaceMutExt: PlaceMut { } } -pub trait ProfileRefExt: ProfileRef { - fn describes(&self) -> Option<&AnyObject> { +pub trait ProfileExt: AsProfile { + fn describes(&self) -> Option<&AnyBase> { self.profile_ref().describes.as_ref() } -} -pub trait ProfileMutExt: ProfileMut { fn set_describes(&mut self, describes: T) -> &mut Self where - T: Into, + T: Into, { self.profile_mut().describes = Some(describes.into()); self } - fn take_describes(&mut self) -> Option { + fn take_describes(&mut self) -> Option { self.profile_mut().describes.take() } @@ -1380,30 +1146,20 @@ pub trait ProfileMutExt: ProfileMut { } } -pub trait RelationshipRefExt: RelationshipRef { - fn subject(&self) -> Option<&AnyObject> { +pub trait RelationshipExt: AsRelationship { + fn subject(&self) -> Option<&AnyBase> { 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, + T: Into, { self.relationship_mut().subject = Some(subject.into()); self } - fn take_subject(&mut self) -> Option { + fn take_subject(&mut self) -> Option { self.relationship_mut().subject.take() } @@ -1412,9 +1168,13 @@ pub trait RelationshipMutExt: RelationshipMut { self } + fn object(&self) -> Option<&OneOrMany> { + self.relationship_ref().object.as_ref() + } + fn set_object(&mut self, object: T) -> &mut Self where - T: Into, + T: Into, { self.relationship_mut().object = Some(object.into().into()); self @@ -1423,7 +1183,7 @@ pub trait RelationshipMutExt: RelationshipMut { fn set_many_objects(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.relationship_mut().object = Some(v.into()); @@ -1432,7 +1192,7 @@ pub trait RelationshipMutExt: RelationshipMut { fn add_object(&mut self, object: T) -> &mut Self where - T: Into, + T: Into, { let v = match self.relationship_mut().object.take() { Some(mut v) => { @@ -1445,7 +1205,7 @@ pub trait RelationshipMutExt: RelationshipMut { self } - fn take_object(&mut self) -> Option> { + fn take_object(&mut self) -> Option> { self.relationship_mut().object.take() } @@ -1454,9 +1214,13 @@ pub trait RelationshipMutExt: RelationshipMut { self } + fn relationship(&self) -> Option<&OneOrMany> { + self.relationship_ref().relationship.as_ref() + } + fn set_relationship(&mut self, relationship: T) -> &mut Self where - T: Into, + T: Into, { self.relationship_mut().relationship = Some(relationship.into().into()); self @@ -1465,7 +1229,7 @@ pub trait RelationshipMutExt: RelationshipMut { fn set_many_relationships(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.relationship_mut().relationship = Some(v.into()); @@ -1474,7 +1238,7 @@ pub trait RelationshipMutExt: RelationshipMut { fn add_relationship(&mut self, relationship: T) -> &mut Self where - T: Into, + T: Into, { let v = match self.relationship_mut().relationship.take() { Some(mut v) => { @@ -1487,7 +1251,7 @@ pub trait RelationshipMutExt: RelationshipMut { self } - fn take_relationship(&mut self) -> Option> { + fn take_relationship(&mut self) -> Option> { self.relationship_mut().relationship.take() } @@ -1497,20 +1261,14 @@ pub trait RelationshipMutExt: RelationshipMut { } } -pub trait TombstoneRefExt: TombstoneRef { - fn former_type(&self) -> Option<&OneOrMany> { +pub trait TombstoneExt: AsTombstone { + 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, + T: Into, { self.tombstone_mut().former_type = Some(former_type.into().into()); self @@ -1519,7 +1277,7 @@ pub trait TombstoneMutExt: TombstoneMut { fn set_many_former_types(&mut self, items: I) -> &mut Self where I: IntoIterator, - T: Into, + T: Into, { let v: Vec<_> = items.into_iter().map(Into::into).collect(); self.tombstone_mut().former_type = Some(v.into()); @@ -1528,7 +1286,7 @@ pub trait TombstoneMutExt: TombstoneMut { fn add_former_type(&mut self, former_type: T) -> &mut Self where - T: Into, + T: Into, { let v = match self.tombstone_mut().former_type.take() { Some(mut v) => { @@ -1541,7 +1299,7 @@ pub trait TombstoneMutExt: TombstoneMut { self } - fn take_former_type(&mut self) -> Option> { + fn take_former_type(&mut self) -> Option> { self.tombstone_mut().former_type.take() } @@ -1550,6 +1308,10 @@ pub trait TombstoneMutExt: TombstoneMut { self } + fn deleted(&self) -> Option<&XsdDateTime> { + self.tombstone_ref().deleted.as_ref() + } + fn set_deleted(&mut self, deleted: T) -> &mut Self where T: Into, @@ -1577,78 +1339,45 @@ pub type Note = Object; pub type Page = Object; pub type Video = Object; -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] -#[serde(transparent)] -struct IdOrObject(Either>>); - -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] -#[serde(transparent)] -pub struct AnyObject(Either); - #[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize, TypedBuilder)] #[serde(rename_all = "camelCase")] #[builder(doc)] pub struct Object { #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub context: Option>, + pub attachment: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub id: Option, - - #[serde(rename = "type")] - #[serde(skip_serializing_if = "Option::is_none")] - #[builder(default, setter(strip_option))] - pub kind: Option, + pub attributed_to: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub attachment: Option>, - - #[serde(skip_serializing_if = "Option::is_none")] - #[builder(default, setter(strip_option, into))] - pub attributed_to: Option>, - - #[serde(skip_serializing_if = "Option::is_none")] - #[builder(default, setter(strip_option, into))] - pub audience: Option>, + pub audience: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] pub content: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - #[builder(default, setter(strip_option, into))] - pub name: Option>, - #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] pub summary: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub url: Option>, - - #[serde(skip_serializing_if = "Option::is_none")] - #[builder(default, setter(strip_option))] - pub media_type: Option, + pub url: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub generator: Option>, + pub generator: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub image: Option>, + pub image: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub location: Option>, - - #[serde(skip_serializing_if = "Option::is_none")] - #[builder(default, setter(strip_option, into))] - pub preview: Option>, + pub location: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] @@ -1672,31 +1401,30 @@ pub struct Object { #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub in_reply_to: Option>, + pub in_reply_to: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub replies: Option>, + pub replies: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub to: Option>, + pub to: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub bto: Option>, + pub bto: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub cc: Option>, + pub cc: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub bcc: Option>, + pub bcc: Option>, #[serde(flatten)] - #[builder(default)] - pub unparsed: Unparsed, + pub inner: Base, } #[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize, TypedBuilder)] @@ -1713,7 +1441,7 @@ pub struct ApObject { #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub source: Option, + pub source: Option, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] @@ -1759,7 +1487,7 @@ pub struct Place { pub struct Profile { #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub describes: Option, + pub describes: Option, #[serde(flatten)] pub inner: Object, @@ -1770,15 +1498,15 @@ pub struct Profile { pub struct Relationship { #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub subject: Option, + pub subject: Option, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub object: Option>, + pub object: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub relationship: Option>, + pub relationship: Option>, #[serde(flatten)] pub inner: Object, @@ -1789,7 +1517,7 @@ pub struct Relationship { pub struct Tombstone { #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] - pub former_type: Option>, + pub former_type: Option>, #[serde(skip_serializing_if = "Option::is_none")] #[builder(default, setter(strip_option, into))] @@ -1799,212 +1527,50 @@ pub struct Tombstone { pub inner: Object, } -impl AnyObject { - pub fn as_xsd_any_uri(&self) -> Option<&XsdAnyUri> { - self.0.as_ref().left().and_then(|l| l.as_xsd_any_uri()) - } - - pub fn as_xsd_string(&self) -> Option<&XsdString> { - self.0.as_ref().right() - } - - pub fn as_object(&self) -> Option<&Object> { - self.0.as_ref().left().and_then(|l| l.as_object()) - } - - pub fn id(self) -> Option { - self.0.left().and_then(|l| l.id()) - } - - pub fn xsd_string(self) -> Option { - self.0.right() - } - - pub fn object(self) -> Option> { - self.0.left().and_then(|l| l.object()) - } - - pub fn set_xsd_any_uri(&mut self, id: XsdAnyUri) { - self.0 = Either::Left(IdOrObject::from_xsd_any_uri(id)); - } - - pub fn set_object(&mut self, object: Object) { - self.0 = Either::Left(IdOrObject::from_object(object)); - } - - pub fn set_xsd_string(&mut self, xsd_string: XsdString) { - self.0 = Either::Right(xsd_string); - } - - pub fn from_xsd_any_uri(id: XsdAnyUri) -> Self { - AnyObject(Either::Left(IdOrObject::from_xsd_any_uri(id))) - } - - pub fn from_object(object: Object) -> Self { - AnyObject(Either::Left(IdOrObject::from_object(object))) - } - - pub fn from_xsd_string(xsd_string: XsdString) -> Self { - AnyObject(Either::Right(xsd_string)) - } -} - -impl IdOrObject { - fn as_xsd_any_uri(&self) -> Option<&XsdAnyUri> { - self.0.as_ref().left() - } - - fn as_object(&self) -> Option<&Object> { - self.0.as_ref().right().map(|b| b.as_ref()) - } - - fn id(self) -> Option { - self.0.left() - } - - fn object(self) -> Option> { - self.0.right().map(|b| *b) - } - - fn from_xsd_any_uri(id: XsdAnyUri) -> Self { - IdOrObject(Either::Left(id)) - } - - fn from_object(object: Object) -> Self { - IdOrObject(Either::Right(Box::new(object))) - } -} - -impl OneOrMany { - pub fn as_single_xsd_any_uri(&self) -> Option<&XsdAnyUri> { - self.as_one().and_then(|inner| inner.as_xsd_any_uri()) - } - - pub fn as_single_xsd_string(&self) -> Option<&XsdString> { - self.as_one().and_then(|inner| inner.as_xsd_string()) - } - - pub fn as_single_object(&self) -> Option<&Object> { - self.as_one().and_then(|inner| inner.as_object()) - } - - pub fn single_xsd_any_uri(self) -> Option { - self.one().and_then(|inner| inner.id()) - } - - pub fn single_xsd_string(self) -> Option { - self.one().and_then(|inner| inner.xsd_string()) - } - - pub fn single_object(self) -> Option> { - self.one().and_then(|inner| inner.object()) - } - - pub fn from_xsd_any_uri(id: XsdAnyUri) -> Self { - OneOrMany(Either::Left(AnyObject::from_xsd_any_uri(id))) - } - - pub fn from_xsd_string(xsd_string: XsdString) -> Self { - OneOrMany(Either::Left(AnyObject::from_xsd_string(xsd_string))) - } - - pub fn from_object(object: Object) -> Self { - OneOrMany(Either::Left(AnyObject::from_object(object))) - } - - pub fn set_single_xsd_any_uri(&mut self, id: XsdAnyUri) -> &mut Self { - self.0 = Either::Left(AnyObject::from_xsd_any_uri(id)); - self - } - - pub fn set_single_xsd_string(&mut self, xsd_string: XsdString) -> &mut Self { - self.0 = Either::Left(AnyObject::from_xsd_string(xsd_string)); - self - } - - pub fn set_single_object(&mut self, object: Object) -> &mut Self { - self.0 = Either::Left(AnyObject::from_object(object)); - self - } - - pub fn add_xsd_any_uri(&mut self, id: XsdAnyUri) -> &mut Self { - self.add(AnyObject::from_xsd_any_uri(id)) - } - - pub fn add_xsd_string(&mut self, xsd_string: XsdString) -> &mut Self { - self.add(AnyObject::from_xsd_string(xsd_string)) - } - - pub fn add_object(&mut self, object: Object) -> &mut Self { - self.add(AnyObject::from_object(object)) - } -} - -impl Object { - pub fn solidify(self) -> Result, serde_json::Error> - where - Kind: serde::de::DeserializeOwned, - { - self.try_map_kind(serde_json::from_value) - } -} - impl Object { - fn extending(mut unparsed: Unparsed) -> Result + fn extending(mut base: Base) -> Result where Kind: serde::de::DeserializeOwned, { Ok(Object { - 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, + attachment: base.remove("attachment")?, + attributed_to: base.remove("attributedTo")?, + audience: base.remove("audience")?, + content: base.remove("content")?, + summary: base.remove("summary")?, + url: base.remove("url")?, + generator: base.remove("generator")?, + image: base.remove("image")?, + location: base.remove("location")?, + start_time: base.remove("startTime")?, + end_time: base.remove("endTime")?, + duration: base.remove("duration")?, + published: base.remove("published")?, + updated: base.remove("updated")?, + in_reply_to: base.remove("inReplyTo")?, + replies: base.remove("replies")?, + to: base.remove("to")?, + bto: base.remove("bto")?, + cc: base.remove("cc")?, + bcc: base.remove("bcc")?, + inner: base, }) } - fn retracting(self) -> Result + fn retracting(self) -> Result, serde_json::Error> where Kind: serde::ser::Serialize, { let Object { - kind, - context, - id, attachment, attributed_to, audience, content, - name, summary, url, - media_type, generator, image, location, - preview, start_time, end_time, duration, @@ -2016,129 +1582,39 @@ impl Object { bto, cc, bcc, - mut unparsed, + mut inner, } = self; - 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)?; + inner + .insert("attachment", attachment)? + .insert("attributedTo", attributed_to)? + .insert("audience", audience)? + .insert("content", content)? + .insert("summary", summary)? + .insert("url", url)? + .insert("generator", generator)? + .insert("image", image)? + .insert("location", location)? + .insert("startTime", start_time)? + .insert("endTime", end_time)? + .insert("duration", duration)? + .insert("published", published)? + .insert("updated", updated)? + .insert("inReplyTo", in_reply_to)? + .insert("replies", replies)? + .insert("to", to)? + .insert("bto", bto)? + .insert("cc", cc)? + .insert("bcc", bcc)?; - Ok(unparsed) - } - - pub fn into_generic(self) -> Result, serde_json::Error> - where - Kind: serde::ser::Serialize, - { - self.try_map_kind(serde_json::to_value) - } - - pub fn map_kind(self, f: impl Fn(Kind) -> NewKind) -> Object { - Object { - kind: self.kind.map(f), - context: self.context, - id: self.id, - attachment: self.attachment, - attributed_to: self.attributed_to, - audience: self.audience, - content: self.content, - name: self.name, - summary: self.summary, - url: self.url, - media_type: self.media_type, - generator: self.generator, - image: self.image, - location: self.location, - preview: self.preview, - start_time: self.start_time, - end_time: self.end_time, - duration: self.duration, - published: self.published, - updated: self.updated, - in_reply_to: self.in_reply_to, - replies: self.replies, - to: self.to, - bto: self.bto, - cc: self.cc, - bcc: self.bcc, - unparsed: self.unparsed, - } - } - - pub fn try_map_kind( - self, - f: impl Fn(Kind) -> Result, - ) -> Result, E> { - Ok(Object { - kind: if let Some(kind) = self.kind { - Some((f)(kind)?) - } else { - None - }, - context: self.context, - id: self.id, - attachment: self.attachment, - attributed_to: self.attributed_to, - audience: self.audience, - content: self.content, - name: self.name, - summary: self.summary, - url: self.url, - media_type: self.media_type, - generator: self.generator, - image: self.image, - location: self.location, - preview: self.preview, - start_time: self.start_time, - end_time: self.end_time, - duration: self.duration, - published: self.published, - updated: self.updated, - in_reply_to: self.in_reply_to, - replies: self.replies, - to: self.to, - bto: self.bto, - cc: self.cc, - bcc: self.bcc, - unparsed: self.unparsed, - }) - } - - pub fn extend(self) -> Result - where - Extended: Extends, - { - Extended::extends(self) + Ok(inner) } } impl ApObject { fn extending(mut inner: Inner) -> Result where - Inner: UnparsedMut + traits::Object, + Inner: UnparsedMut + markers::Object, { let shares = inner.remove("shares")?; let likes = inner.remove("likes")?; @@ -2156,7 +1632,7 @@ impl ApObject { fn retracting(self) -> Result where - Inner: UnparsedMut + traits::Object, + Inner: UnparsedMut + markers::Object, { let ApObject { shares, @@ -2295,33 +1771,33 @@ impl Tombstone { } } -impl Extends for Object +impl Extends for Object where Kind: serde::de::DeserializeOwned + serde::ser::Serialize, { type Error = serde_json::Error; - fn extends(unparsed: Unparsed) -> Result { - Self::extending(unparsed) + fn extends(base: Base) -> Result { + Self::extending(base) } - fn retracts(self) -> Result { + fn retracts(self) -> Result, Self::Error> { self.retracting() } } -impl TryFrom for Object +impl TryFrom> for Object where Kind: serde::de::DeserializeOwned, { type Error = serde_json::Error; - fn try_from(unparsed: Unparsed) -> Result { - Self::extending(unparsed) + fn try_from(base: Base) -> Result { + Self::extending(base) } } -impl TryFrom> for Unparsed +impl TryFrom> for Base where Kind: serde::ser::Serialize, { @@ -2332,30 +1808,35 @@ where } } -impl Extends for ApObject +impl Extends for ApObject where - Inner: UnparsedMut + traits::Object, + Inner: Extends + UnparsedMut + markers::Object, + Kind: serde::de::DeserializeOwned + serde::ser::Serialize, { type Error = serde_json::Error; - fn extends(inner: Inner) -> Result { + fn extends(base: Base) -> Result { + let inner = Inner::extends(base)?; Self::extending(inner) } - fn retracts(self) -> Result { - self.retracting() + fn retracts(self) -> Result, Self::Error> { + let inner = self.retracting()?; + inner.retracts() } } -impl Extends> for Place { +impl Extends for Place { type Error = serde_json::Error; - fn extends(object: Object) -> Result { - Self::extending(object) + fn extends(base: Base) -> Result { + let inner = Object::extends(base)?; + Self::extending(inner) } - fn retracts(self) -> Result, Self::Error> { - self.retracting() + fn retracts(self) -> Result, Self::Error> { + let inner = self.retracting()?; + inner.retracts() } } @@ -2375,15 +1856,17 @@ impl TryFrom for Object { } } -impl Extends> for Profile { +impl Extends for Profile { type Error = serde_json::Error; - fn extends(object: Object) -> Result { - Self::extending(object) + fn extends(base: Base) -> Result { + let inner = Object::extends(base)?; + Self::extending(inner) } - fn retracts(self) -> Result, Self::Error> { - self.retracting() + fn retracts(self) -> Result, Self::Error> { + let inner = self.retracting()?; + inner.retracts() } } @@ -2403,15 +1886,17 @@ impl TryFrom for Object { } } -impl Extends> for Relationship { +impl Extends for Relationship { type Error = serde_json::Error; - fn extends(object: Object) -> Result { - Self::extending(object) + fn extends(base: Base) -> Result { + let inner = Object::extends(base)?; + Self::extending(inner) } - fn retracts(self) -> Result, Self::Error> { - self.retracting() + fn retracts(self) -> Result, Self::Error> { + let inner = self.retracting()?; + inner.retracts() } } @@ -2431,15 +1916,17 @@ impl TryFrom for Object { } } -impl Extends> for Tombstone { +impl Extends for Tombstone { type Error = serde_json::Error; - fn extends(object: Object) -> Result { - Self::extending(object) + fn extends(base: Base) -> Result { + let inner = Object::extends(base)?; + Self::extending(inner) } - fn retracts(self) -> Result, Self::Error> { - self.retracting() + fn retracts(self) -> Result, Self::Error> { + let inner = self.retracting()?; + inner.retracts() } } @@ -2461,7 +1948,7 @@ impl TryFrom for Object { impl UnparsedMut for Object { fn unparsed_mut(&mut self) -> &mut Unparsed { - &mut self.unparsed + self.inner.unparsed_mut() } } @@ -2498,230 +1985,257 @@ impl UnparsedMut for Tombstone { } } -impl From> for AnyObject { - fn from(o: Object) -> Self { - Self::from_object(o) +impl markers::Base for Object {} +impl markers::Object for Object {} + +impl markers::Base for ApObject where Inner: markers::Base {} +impl markers::Object for ApObject where Inner: markers::Object {} + +impl markers::Base for Place {} +impl markers::Object for Place {} + +impl markers::Base for Profile {} +impl markers::Object for Profile {} + +impl markers::Base for Relationship {} +impl markers::Object for Relationship {} + +impl markers::Base for Tombstone {} +impl markers::Object for Tombstone {} + +impl ObjectExt for T where T: AsObject {} +impl ApObjectExt for T where T: AsApObject {} +impl PlaceExt for T where T: AsPlace {} +impl ProfileExt for T where T: AsProfile {} +impl RelationshipExt for T where T: AsRelationship {} +impl TombstoneExt for T where T: AsTombstone {} + +impl AsBase for Object { + fn base_ref(&self) -> &Base { + &self.inner + } + + fn base_mut(&mut self) -> &mut Base { + &mut self.inner } } -impl From for AnyObject { - fn from(id: XsdAnyUri) -> Self { - Self::from_xsd_any_uri(id) - } -} - -impl From for AnyObject { - fn from(xsd_string: XsdString) -> Self { - Self::from_xsd_string(xsd_string) - } -} - -impl From> for OneOrMany { - fn from(object: Object) -> Self { - Self::from_object(object) - } -} - -impl From for OneOrMany { - fn from(xsd_any_uri: XsdAnyUri) -> Self { - Self::from_xsd_any_uri(xsd_any_uri) - } -} - -impl From for OneOrMany { - fn from(xsd_string: XsdString) -> Self { - 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 { +impl AsObject for Object { fn object_ref(&self) -> &Object { self } -} -impl ObjectMut for Object { + fn object_mut(&mut self) -> &mut Object { self } } -impl ObjectRef for ApObject +impl AsBase for ApObject where - Inner: ObjectRef, + Inner: AsBase, +{ + fn base_ref(&self) -> &Base { + self.inner.base_ref() + } + + fn base_mut(&mut self) -> &mut Base { + self.inner.base_mut() + } +} + +impl AsObject for ApObject +where + Inner: AsObject, { 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 +impl AsApObject for ApObject where - Inner: traits::Object, + Inner: markers::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 +impl AsPlace for ApObject where - Inner: PlaceRef, + Inner: AsPlace, { 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 +impl AsProfile for ApObject where - Inner: ProfileRef, + Inner: AsProfile, { 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 +impl AsRelationship for ApObject where - Inner: RelationshipRef, + Inner: AsRelationship, { 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 +impl AsTombstone for ApObject where - Inner: TombstoneRef, + Inner: AsTombstone, { 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 { +impl AsBase for Place { + fn base_ref(&self) -> &Base { + self.inner.base_ref() + } + + fn base_mut(&mut self) -> &mut Base { + self.inner.base_mut() + } +} + +impl AsObject for Place { + fn object_ref(&self) -> &Object { + &self.inner + } + + fn object_mut(&mut self) -> &mut Object { + &mut self.inner + } +} + +impl AsPlace for Place { fn place_ref(&self) -> &Place { self } -} -impl PlaceMut for Place { + fn place_mut(&mut self) -> &mut Place { self } } -impl ProfileRef for Profile { +impl AsBase for Profile { + fn base_ref(&self) -> &Base { + self.inner.base_ref() + } + + fn base_mut(&mut self) -> &mut Base { + self.inner.base_mut() + } +} + +impl AsObject for Profile { + fn object_ref(&self) -> &Object { + &self.inner + } + + fn object_mut(&mut self) -> &mut Object { + &mut self.inner + } +} + +impl AsProfile for Profile { fn profile_ref(&self) -> &Profile { self } -} -impl ProfileMut for Profile { + fn profile_mut(&mut self) -> &mut Profile { self } } -impl RelationshipRef for Relationship { +impl AsBase for Relationship { + fn base_ref(&self) -> &Base { + self.inner.base_ref() + } + + fn base_mut(&mut self) -> &mut Base { + self.inner.base_mut() + } +} + +impl AsObject for Relationship { + fn object_ref(&self) -> &Object { + &self.inner + } + + fn object_mut(&mut self) -> &mut Object { + &mut self.inner + } +} + +impl AsRelationship for Relationship { fn relationship_ref(&self) -> &Relationship { self } -} -impl RelationshipMut for Relationship { + fn relationship_mut(&mut self) -> &mut Relationship { self } } -impl TombstoneRef for Tombstone { +impl AsBase for Tombstone { + fn base_ref(&self) -> &Base { + self.inner.base_ref() + } + + fn base_mut(&mut self) -> &mut Base { + self.inner.base_mut() + } +} + +impl AsObject for Tombstone { + fn object_ref(&self) -> &Object { + &self.inner + } + + fn object_mut(&mut self) -> &mut Object { + &mut self.inner + } +} + +impl AsTombstone 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 735e8fa..657cdd7 100644 --- a/src/primitives.rs +++ b/src/primitives.rs @@ -5,10 +5,6 @@ pub use activitystreams::primitives::{ XsdNonNegativeInteger, XsdString, }; -#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)] -#[serde(transparent)] -pub struct Unparsed(std::collections::HashMap); - #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[serde(transparent)] pub struct AnyString(Either); @@ -19,16 +15,6 @@ pub type Unit = Either; #[serde(transparent)] pub struct OneOrMany(pub Either>); -impl Unparsed { - pub(crate) fn remove(&mut self, key: &str) -> serde_json::Value { - self.0.remove(key).unwrap_or(serde_json::Value::Null) - } - - pub(crate) fn insert(&mut self, key: String, value: serde_json::Value) { - self.0.insert(key, value); - } -} - impl AnyString { pub fn as_xsd_string(&self) -> Option<&XsdString> { self.0.as_ref().left() @@ -185,14 +171,6 @@ impl OneOrMany { } } -impl crate::traits::Base for Unparsed {} - -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) diff --git a/src/traits.rs b/src/unparsed.rs similarity index 53% rename from src/traits.rs rename to src/unparsed.rs index 090c939..7b5d388 100644 --- a/src/traits.rs +++ b/src/unparsed.rs @@ -1,8 +1,3 @@ -use crate::primitives::Unparsed; -pub use activitystreams::{ - Activity, Actor, Base, Collection, CollectionPage, IntransitiveActivity, Link, Object, -}; - pub trait UnparsedMut { fn unparsed_mut(&mut self) -> &mut Unparsed; } @@ -25,12 +20,24 @@ pub trait UnparsedMutExt: UnparsedMut { } } -pub trait Extends: Sized { - type Error: std::error::Error; +#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)] +#[serde(transparent)] +pub struct Unparsed(std::collections::HashMap); - fn extends(t: T) -> Result; +impl Unparsed { + pub(crate) fn remove(&mut self, key: &str) -> serde_json::Value { + self.0.remove(key).unwrap_or(serde_json::Value::Null) + } - fn retracts(self) -> Result; + pub(crate) fn insert(&mut self, key: String, value: serde_json::Value) { + self.0.insert(key, value); + } +} + +impl UnparsedMut for Unparsed { + fn unparsed_mut(&mut self) -> &mut Unparsed { + self + } } impl UnparsedMutExt for T where T: UnparsedMut {}