Add Actor wrapper, new method

This commit is contained in:
asonix 2020-05-21 14:07:35 -05:00
parent ed2bf31517
commit f62902a7aa
5 changed files with 100 additions and 74 deletions

View file

@ -2456,10 +2456,7 @@ impl<Inner> markers::IntransitiveActivity for ApObject<Inner> where
{ {
} }
impl<Kind> Extends<Kind> for Activity<Kind> impl<Kind> Extends<Kind> for Activity<Kind> {
where
Kind: serde::de::DeserializeOwned + serde::ser::Serialize,
{
type Error = serde_json::Error; type Error = serde_json::Error;
fn extends(base: Base<Kind>) -> Result<Self, Self::Error> { fn extends(base: Base<Kind>) -> Result<Self, Self::Error> {
@ -2489,10 +2486,7 @@ impl<Kind> TryFrom<Activity<Kind>> for Object<Kind> {
} }
} }
impl<Kind> Extends<Kind> for ActorAndObject<Kind> impl<Kind> Extends<Kind> for ActorAndObject<Kind> {
where
Kind: serde::de::DeserializeOwned + serde::ser::Serialize,
{
type Error = serde_json::Error; type Error = serde_json::Error;
fn extends(base: Base<Kind>) -> Result<Self, Self::Error> { fn extends(base: Base<Kind>) -> Result<Self, Self::Error> {
@ -2612,10 +2606,7 @@ impl TryFrom<Delete> for Object<DeleteType> {
} }
} }
impl<Kind> Extends<Kind> for ActorAndObjectOptOriginAndTarget<Kind> impl<Kind> Extends<Kind> for ActorAndObjectOptOriginAndTarget<Kind> {
where
Kind: serde::de::DeserializeOwned + serde::ser::Serialize,
{
type Error = serde_json::Error; type Error = serde_json::Error;
fn extends(base: Base<Kind>) -> Result<Self, Self::Error> { fn extends(base: Base<Kind>) -> Result<Self, Self::Error> {
@ -2645,10 +2636,7 @@ impl<Kind> TryFrom<ActorAndObjectOptOriginAndTarget<Kind>> for Object<Kind> {
} }
} }
impl<Kind> Extends<Kind> for ActorAndObjectOptTarget<Kind> impl<Kind> Extends<Kind> for ActorAndObjectOptTarget<Kind> {
where
Kind: serde::de::DeserializeOwned + serde::ser::Serialize,
{
type Error = serde_json::Error; type Error = serde_json::Error;
fn extends(base: Base<Kind>) -> Result<Self, Self::Error> { fn extends(base: Base<Kind>) -> Result<Self, Self::Error> {

View file

@ -594,33 +594,33 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// Describes a software application. /// Describes a software application.
/// ///
/// This is just an alias for `Object<ApplicationType>` because there's no fields inherent to /// This is just an alias for `Actor<ApplicationType>` because there's no fields inherent to
/// Application that aren't already present on an Object. /// Application that aren't already present on an Actor.
pub type Application = Object<ApplicationType>; pub type Application = Actor<ApplicationType>;
/// Represents a formal or informal collective of Actors. /// Represents a formal or informal collective of Actors.
/// ///
/// This is just an alias for `Object<GroupType>` because there's no fields inherent to /// This is just an alias for `Actor<GroupType>` because there's no fields inherent to
/// Group that aren't already present on an Object. /// Group that aren't already present on an Actor.
pub type Group = Object<GroupType>; pub type Group = Actor<GroupType>;
/// Represents an organization. /// Represents an organization.
/// ///
/// This is just an alias for `Object<OrganizationType>` because there's no fields inherent to /// This is just an alias for `Actor<OrganizationType>` because there's no fields inherent to
/// Organization that aren't already present on an Object. /// Organization that aren't already present on an Actor.
pub type Organization = Object<OrganizationType>; pub type Organization = Actor<OrganizationType>;
/// Represents an individual person. /// Represents an individual person.
/// ///
/// This is just an alias for `Object<PersonType>` because there's no fields inherent to /// This is just an alias for `Actor<PersonType>` because there's no fields inherent to
/// Person that aren't already present on an Object. /// Person that aren't already present on an Actor.
pub type Person = Object<PersonType>; pub type Person = Actor<PersonType>;
/// Represents a service of any kind. /// Represents a service of any kind.
/// ///
/// This is just an alias for `Object<ServiceType>` because there's no fields inherent to /// This is just an alias for `Actor<ServiceType>` because there's no fields inherent to
/// Service that aren't already present on an Object. /// Service that aren't already present on an Actor.
pub type Service = Object<ServiceType>; pub type Service = Actor<ServiceType>;
/// Actor types are Object types that are capable of performing activities. /// Actor types are Object types that are capable of performing activities.
/// ///
@ -790,6 +790,33 @@ pub struct Endpoints {
pub shared_inbox: Option<XsdAnyUri>, pub shared_inbox: Option<XsdAnyUri>,
} }
/// A simple type to create an Actor out of any Object
///
/// ```rust
/// # use activitystreams_new::{object::Object, actor::Actor, prelude::*};
/// let object = Object::<String>::new();
/// let mut actor = Actor(object);
/// actor.set_kind("MyCustomActor".into());
/// ```
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(transparent)]
pub struct Actor<Kind>(pub Object<Kind>);
impl<Kind> Actor<Kind> {
/// Create a new Actor
///
/// ```rust
/// # use activitystreams_new::actor::Actor;
/// let actor = Actor::<String>::new();
/// ```
pub fn new() -> Self
where
Kind: Default,
{
Actor(Object::new())
}
}
impl<Inner> ApActor<Inner> { impl<Inner> ApActor<Inner> {
/// Create a new ActivityPub Actor /// Create a new ActivityPub Actor
/// ///
@ -872,26 +899,24 @@ impl<Inner> ApActor<Inner> {
} }
} }
impl markers::Actor for Application {} impl<Kind> markers::Base for Actor<Kind> {}
impl markers::Actor for Group {} impl<Kind> markers::Object for Actor<Kind> {}
impl markers::Actor for Organization {} impl<Kind> markers::Actor for Actor<Kind> {}
impl markers::Actor for Person {}
impl markers::Actor for Service {}
impl<Inner> markers::Base for ApActor<Inner> where Inner: markers::Base {} impl<Inner> markers::Base for ApActor<Inner> where Inner: markers::Base {}
impl<Inner> markers::Object for ApActor<Inner> where Inner: markers::Object {} impl<Inner> markers::Object for ApActor<Inner> where Inner: markers::Object {}
impl<Inner> markers::Actor for ApActor<Inner> where Inner: markers::Actor {} impl<Inner> markers::Actor for ApActor<Inner> where Inner: markers::Actor {}
impl<Inner, Kind> Extends<Kind> for ApActor<Inner> impl<Inner, Kind, Error> Extends<Kind> for ApActor<Inner>
where where
Inner: Extends<Kind, Error = serde_json::Error> + UnparsedMut + markers::Actor, Inner: Extends<Kind, Error = Error> + UnparsedMut + markers::Actor,
Kind: serde::de::DeserializeOwned + serde::ser::Serialize, Error: From<serde_json::Error> + std::error::Error,
{ {
type Error = serde_json::Error; type Error = Error;
fn extends(base: Base<Kind>) -> Result<Self, Self::Error> { fn extends(base: Base<Kind>) -> Result<Self, Self::Error> {
let inner = Inner::extends(base)?; let inner = Inner::extends(base)?;
Self::extending(inner) Ok(Self::extending(inner)?)
} }
fn retracts(self) -> Result<Base<Kind>, Self::Error> { fn retracts(self) -> Result<Base<Kind>, Self::Error> {
@ -961,4 +986,42 @@ where
} }
} }
impl<Kind> Extends<Kind> for Actor<Kind> {
type Error = serde_json::Error;
fn extends(base: Base<Kind>) -> Result<Self, Self::Error> {
Object::extends(base).map(Actor)
}
fn retracts(self) -> Result<Base<Kind>, Self::Error> {
self.0.retracts()
}
}
impl<Kind> UnparsedMut for Actor<Kind> {
fn unparsed_mut(&mut self) -> &mut Unparsed {
self.0.unparsed_mut()
}
}
impl<Kind> AsBase<Kind> for Actor<Kind> {
fn base_ref(&self) -> &Base<Kind> {
self.0.base_ref()
}
fn base_mut(&mut self) -> &mut Base<Kind> {
self.0.base_mut()
}
}
impl<Kind> AsObject<Kind> for Actor<Kind> {
fn object_ref(&self) -> &Object<Kind> {
self.0.object_ref()
}
fn object_mut(&mut self) -> &mut Object<Kind> {
self.0.object_mut()
}
}
impl<T, Inner> ApActorExt<Inner> for T where T: AsApActor<Inner> {} impl<T, Inner> ApActorExt<Inner> for T where T: AsApActor<Inner> {}

View file

@ -1202,10 +1202,7 @@ impl markers::CollectionPage for OrderedCollectionPage {}
impl<Inner> markers::Collection for ApObject<Inner> where Inner: markers::Collection {} impl<Inner> markers::Collection for ApObject<Inner> where Inner: markers::Collection {}
impl<Inner> markers::CollectionPage for ApObject<Inner> where Inner: markers::CollectionPage {} impl<Inner> markers::CollectionPage for ApObject<Inner> where Inner: markers::CollectionPage {}
impl<Kind> Extends<Kind> for Collection<Kind> impl<Kind> Extends<Kind> for Collection<Kind> {
where
Kind: serde::de::DeserializeOwned + serde::ser::Serialize,
{
type Error = serde_json::Error; type Error = serde_json::Error;
fn extends(base: Base<Kind>) -> Result<Self, Self::Error> { fn extends(base: Base<Kind>) -> Result<Self, Self::Error> {
@ -1235,10 +1232,7 @@ impl<Kind> TryFrom<Object<Kind>> for Collection<Kind> {
} }
} }
impl<Kind> Extends<Kind> for CollectionPage<Kind> impl<Kind> Extends<Kind> for CollectionPage<Kind> {
where
Kind: serde::de::DeserializeOwned + serde::ser::Serialize,
{
type Error = serde_json::Error; type Error = serde_json::Error;
fn extends(base: Base<Kind>) -> Result<Self, Self::Error> { fn extends(base: Base<Kind>) -> Result<Self, Self::Error> {

View file

@ -590,10 +590,7 @@ impl<Kind> Link<Kind> {
impl<Kind> markers::Base for Link<Kind> {} impl<Kind> markers::Base for Link<Kind> {}
impl<Kind> markers::Link for Link<Kind> {} impl<Kind> markers::Link for Link<Kind> {}
impl<Kind> Extends<Kind> for Link<Kind> impl<Kind> Extends<Kind> for Link<Kind> {
where
Kind: serde::de::DeserializeOwned + serde::ser::Serialize,
{
type Error = serde_json::Error; type Error = serde_json::Error;
fn extends(base: Base<Kind>) -> Result<Self, Self::Error> { fn extends(base: Base<Kind>) -> Result<Self, Self::Error> {

View file

@ -4724,10 +4724,7 @@ impl<Kind> Object<Kind> {
Object::builder().inner(Base::new()).build() Object::builder().inner(Base::new()).build()
} }
fn extending(mut base: Base<Kind>) -> Result<Self, serde_json::Error> fn extending(mut base: Base<Kind>) -> Result<Self, serde_json::Error> {
where
Kind: serde::de::DeserializeOwned,
{
Ok(Object { Ok(Object {
attachment: base.remove("attachment")?, attachment: base.remove("attachment")?,
attributed_to: base.remove("attributedTo")?, attributed_to: base.remove("attributedTo")?,
@ -4755,10 +4752,7 @@ impl<Kind> Object<Kind> {
}) })
} }
fn retracting(self) -> Result<Base<Kind>, serde_json::Error> fn retracting(self) -> Result<Base<Kind>, serde_json::Error> {
where
Kind: serde::ser::Serialize,
{
let Object { let Object {
attachment, attachment,
attributed_to, attributed_to,
@ -5031,10 +5025,7 @@ impl Tombstone {
} }
} }
impl<Kind> Extends<Kind> for Object<Kind> impl<Kind> Extends<Kind> for Object<Kind> {
where
Kind: serde::de::DeserializeOwned + serde::ser::Serialize,
{
type Error = serde_json::Error; type Error = serde_json::Error;
fn extends(base: Base<Kind>) -> Result<Self, Self::Error> { fn extends(base: Base<Kind>) -> Result<Self, Self::Error> {
@ -5046,10 +5037,7 @@ where
} }
} }
impl<Kind> TryFrom<Base<Kind>> for Object<Kind> impl<Kind> TryFrom<Base<Kind>> for Object<Kind> {
where
Kind: serde::de::DeserializeOwned,
{
type Error = serde_json::Error; type Error = serde_json::Error;
fn try_from(base: Base<Kind>) -> Result<Self, Self::Error> { fn try_from(base: Base<Kind>) -> Result<Self, Self::Error> {
@ -5057,10 +5045,7 @@ where
} }
} }
impl<Kind> TryFrom<Object<Kind>> for Base<Kind> impl<Kind> TryFrom<Object<Kind>> for Base<Kind> {
where
Kind: serde::ser::Serialize,
{
type Error = serde_json::Error; type Error = serde_json::Error;
fn try_from(object: Object<Kind>) -> Result<Self, Self::Error> { fn try_from(object: Object<Kind>) -> Result<Self, Self::Error> {
@ -5071,7 +5056,6 @@ where
impl<Inner, Kind> Extends<Kind> for ApObject<Inner> impl<Inner, Kind> Extends<Kind> for ApObject<Inner>
where where
Inner: Extends<Kind, Error = serde_json::Error> + UnparsedMut + markers::Object, Inner: Extends<Kind, Error = serde_json::Error> + UnparsedMut + markers::Object,
Kind: serde::de::DeserializeOwned + serde::ser::Serialize,
{ {
type Error = serde_json::Error; type Error = serde_json::Error;