activitystreams-new/src/actor.rs

410 lines
11 KiB
Rust
Raw Normal View History

2020-05-14 03:54:50 +00:00
use crate::{
2020-05-15 22:01:29 +00:00
base::{AsBase, Base, Extends},
markers,
object::{ApObject, AsApObject, AsObject, Object},
primitives::{OneOrMany, XsdAnyUri, XsdString},
unparsed::{Unparsed, UnparsedMut, UnparsedMutExt},
2020-05-14 03:54:50 +00:00
};
2020-05-14 16:23:38 +00:00
use typed_builder::TypedBuilder;
2020-05-14 03:54:50 +00:00
pub mod kind {
pub use activitystreams::actor::kind::*;
}
use self::kind::*;
2020-05-15 22:01:29 +00:00
pub trait AsApActor<Inner>: markers::Actor {
2020-05-15 03:18:34 +00:00
fn ap_actor_ref(&self) -> &ApActor<Inner>;
fn ap_actor_mut(&mut self) -> &mut ApActor<Inner>;
}
2020-05-15 22:01:29 +00:00
pub trait ApActorExt<Inner>: AsApActor<Inner> {
2020-05-15 03:18:34 +00:00
fn inbox<'a>(&'a self) -> &'a XsdAnyUri
where
Inner: 'a,
{
&self.ap_actor_ref().inbox
}
2020-05-15 22:01:29 +00:00
fn set_inbox(&mut self, inbox: XsdAnyUri) -> &mut Self {
self.ap_actor_mut().inbox = inbox;
self
2020-05-15 03:18:34 +00:00
}
2020-05-15 22:01:29 +00:00
fn outbox<'a>(&'a self) -> &'a XsdAnyUri
2020-05-15 03:18:34 +00:00
where
Inner: 'a,
{
2020-05-15 22:01:29 +00:00
&self.ap_actor_ref().outbox
2020-05-15 03:18:34 +00:00
}
2020-05-15 22:01:29 +00:00
fn set_outbox(&mut self, outbox: XsdAnyUri) -> &mut Self {
self.ap_actor_mut().outbox = outbox;
self
2020-05-15 03:18:34 +00:00
}
2020-05-15 22:01:29 +00:00
fn following<'a>(&'a self) -> Option<&'a XsdAnyUri>
2020-05-15 03:18:34 +00:00
where
Inner: 'a,
{
2020-05-15 22:01:29 +00:00
self.ap_actor_ref().following.as_ref()
2020-05-15 03:18:34 +00:00
}
fn set_following(&mut self, following: XsdAnyUri) -> &mut Self {
self.ap_actor_mut().following = Some(following);
self
}
fn take_following(&mut self) -> Option<XsdAnyUri> {
self.ap_actor_mut().following.take()
}
fn delete_following(&mut self) -> &mut Self {
self.ap_actor_mut().following = None;
self
}
2020-05-15 22:01:29 +00:00
fn followers<'a>(&'a self) -> Option<&'a XsdAnyUri>
where
Inner: 'a,
{
self.ap_actor_ref().followers.as_ref()
}
2020-05-15 03:18:34 +00:00
fn set_followers(&mut self, followers: XsdAnyUri) -> &mut Self {
self.ap_actor_mut().followers = Some(followers);
self
}
fn take_followers(&mut self) -> Option<XsdAnyUri> {
self.ap_actor_mut().followers.take()
}
fn delete_followers(&mut self) -> &mut Self {
self.ap_actor_mut().followers = None;
self
}
2020-05-15 22:01:29 +00:00
fn liked<'a>(&'a self) -> Option<&'a XsdAnyUri>
where
Inner: 'a,
{
self.ap_actor_ref().liked.as_ref()
}
2020-05-15 03:18:34 +00:00
fn set_liked(&mut self, liked: XsdAnyUri) -> &mut Self {
self.ap_actor_mut().liked = Some(liked);
self
}
fn take_liked(&mut self) -> Option<XsdAnyUri> {
self.ap_actor_mut().liked.take()
}
2020-05-15 22:01:29 +00:00
fn delete_liked(&mut self) -> &mut Self {
2020-05-15 03:18:34 +00:00
self.ap_actor_mut().liked = None;
self
}
2020-05-15 22:01:29 +00:00
fn streams<'a>(&'a self) -> Option<&'a OneOrMany<XsdAnyUri>>
where
Inner: 'a,
{
self.ap_actor_ref().streams.as_ref()
}
2020-05-15 03:18:34 +00:00
fn set_streams(&mut self, streams: XsdAnyUri) -> &mut Self {
self.ap_actor_mut().streams = Some(streams.into());
self
}
fn set_many_streams<I>(&mut self, items: I) -> &mut Self
where
I: IntoIterator<Item = XsdAnyUri>,
{
let v: Vec<_> = items.into_iter().collect();
self.ap_actor_mut().streams = Some(v.into());
self
}
fn add_stream(&mut self, stream: XsdAnyUri) -> &mut Self {
let v = match self.ap_actor_mut().streams.take() {
Some(mut v) => {
v.add(stream);
v
}
None => vec![stream].into(),
};
self.ap_actor_mut().streams = Some(v);
self
}
fn take_streams(&mut self) -> Option<OneOrMany<XsdAnyUri>> {
self.ap_actor_mut().streams.take()
}
fn delete_streams(&mut self) -> &mut Self {
self.ap_actor_mut().streams = None;
self
}
2020-05-15 22:01:29 +00:00
fn preferred_username<'a>(&'a self) -> Option<&'a XsdString>
where
Inner: 'a,
{
self.ap_actor_ref().preferred_username.as_ref()
}
2020-05-15 03:18:34 +00:00
fn set_preferred_username(&mut self, string: XsdString) -> &mut Self {
self.ap_actor_mut().preferred_username = Some(string);
self
}
fn take_preferred_username(&mut self) -> Option<XsdString> {
self.ap_actor_mut().preferred_username.take()
}
fn delete_preferred_username(&mut self) -> &mut Self {
self.ap_actor_mut().preferred_username = None;
self
}
2020-05-15 22:01:29 +00:00
fn endpoints<'a>(&'a self) -> Option<&'a Endpoints>
where
Inner: 'a,
{
self.ap_actor_ref().endpoints.as_ref()
}
2020-05-15 03:18:34 +00:00
fn set_endpoints(&mut self, endpoints: Endpoints) -> &mut Self {
self.ap_actor_mut().endpoints = Some(endpoints);
self
}
fn take_endpoints(&mut self) -> Option<Endpoints> {
self.ap_actor_mut().endpoints.take()
}
fn delete_endpoints(&mut self) -> &mut Self {
self.ap_actor_mut().endpoints = None;
self
}
}
2020-05-14 03:54:50 +00:00
pub type Application = Object<ApplicationType>;
pub type Group = Object<GroupType>;
pub type Organization = Object<OrganizationType>;
pub type Person = Object<PersonType>;
pub type Service = Object<ServiceType>;
2020-05-14 16:23:38 +00:00
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize, TypedBuilder)]
2020-05-14 03:54:50 +00:00
#[serde(rename_all = "camelCase")]
2020-05-14 16:23:38 +00:00
#[builder(doc)]
2020-05-14 03:54:50 +00:00
pub struct ApActor<Inner> {
pub inbox: XsdAnyUri,
pub outbox: XsdAnyUri,
#[serde(skip_serializing_if = "Option::is_none")]
2020-05-14 16:23:38 +00:00
#[builder(default, setter(strip_option))]
2020-05-14 03:54:50 +00:00
pub following: Option<XsdAnyUri>,
#[serde(skip_serializing_if = "Option::is_none")]
2020-05-14 16:23:38 +00:00
#[builder(default, setter(strip_option))]
2020-05-14 03:54:50 +00:00
pub followers: Option<XsdAnyUri>,
#[serde(skip_serializing_if = "Option::is_none")]
2020-05-14 16:23:38 +00:00
#[builder(default, setter(strip_option))]
2020-05-14 03:54:50 +00:00
pub liked: Option<XsdAnyUri>,
#[serde(skip_serializing_if = "Option::is_none")]
2020-05-14 16:23:38 +00:00
#[builder(default, setter(strip_option, into))]
2020-05-14 03:54:50 +00:00
pub streams: Option<OneOrMany<XsdAnyUri>>,
#[serde(skip_serializing_if = "Option::is_none")]
2020-05-14 16:23:38 +00:00
#[builder(default, setter(strip_option))]
2020-05-14 03:54:50 +00:00
pub preferred_username: Option<XsdString>,
#[serde(skip_serializing_if = "Option::is_none")]
2020-05-14 16:23:38 +00:00
#[builder(default, setter(strip_option))]
2020-05-14 03:54:50 +00:00
pub endpoints: Option<Endpoints>,
#[serde(flatten)]
pub inner: Inner,
}
2020-05-14 16:23:38 +00:00
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize, TypedBuilder)]
2020-05-14 03:54:50 +00:00
#[serde(rename_all = "camelCase")]
pub struct Endpoints {
#[serde(skip_serializing_if = "Option::is_none")]
2020-05-14 16:23:38 +00:00
#[builder(default, setter(strip_option))]
2020-05-14 03:54:50 +00:00
pub proxy_url: Option<XsdAnyUri>,
#[serde(skip_serializing_if = "Option::is_none")]
2020-05-14 16:23:38 +00:00
#[builder(default, setter(strip_option))]
2020-05-14 03:54:50 +00:00
pub oauth_authorization_endpoint: Option<XsdAnyUri>,
#[serde(skip_serializing_if = "Option::is_none")]
2020-05-14 16:23:38 +00:00
#[builder(default, setter(strip_option))]
2020-05-14 03:54:50 +00:00
pub oauth_token_endpoint: Option<XsdAnyUri>,
#[serde(skip_serializing_if = "Option::is_none")]
2020-05-14 16:23:38 +00:00
#[builder(default, setter(strip_option))]
2020-05-14 03:54:50 +00:00
pub provide_client_key: Option<XsdAnyUri>,
#[serde(skip_serializing_if = "Option::is_none")]
2020-05-14 16:23:38 +00:00
#[builder(default, setter(strip_option))]
2020-05-14 03:54:50 +00:00
pub sign_client_key: Option<XsdAnyUri>,
#[serde(skip_serializing_if = "Option::is_none")]
2020-05-14 16:23:38 +00:00
#[builder(default, setter(strip_option))]
2020-05-14 03:54:50 +00:00
pub shared_inbox: Option<XsdAnyUri>,
}
impl<Inner> ApActor<Inner> {
fn extending(mut inner: Inner) -> Result<Self, serde_json::Error>
where
2020-05-15 22:01:29 +00:00
Inner: UnparsedMut + markers::Actor,
2020-05-14 03:54:50 +00:00
{
let inbox = inner.remove("inbox")?;
let outbox = inner.remove("outbox")?;
let following = inner.remove("following")?;
let followers = inner.remove("followers")?;
let liked = inner.remove("liked")?;
let streams = inner.remove("streams")?;
let preferred_username = inner.remove("preferredUsername")?;
let endpoints = inner.remove("endpoints")?;
Ok(ApActor {
inbox,
outbox,
following,
followers,
liked,
streams,
preferred_username,
endpoints,
inner,
})
}
fn retracting(self) -> Result<Inner, serde_json::Error>
where
2020-05-15 22:01:29 +00:00
Inner: UnparsedMut + markers::Actor,
2020-05-14 03:54:50 +00:00
{
let ApActor {
inbox,
outbox,
following,
followers,
liked,
streams,
preferred_username,
endpoints,
mut inner,
} = self;
inner
.insert("endpoints", endpoints)?
.insert("preferredUsername", preferred_username)?
.insert("streams", streams)?
.insert("liked", liked)?
.insert("followers", followers)?
.insert("following", following)?
.insert("outbox", outbox)?
.insert("inbox", inbox)?;
Ok(inner)
}
}
2020-05-15 22:01:29 +00:00
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 {}
2020-05-14 03:54:50 +00:00
2020-05-15 22:01:29 +00:00
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::Actor for ApActor<Inner> where Inner: markers::Actor {}
2020-05-14 03:54:50 +00:00
2020-05-15 22:01:29 +00:00
impl<Inner, Kind> Extends<Kind> for ApActor<Inner>
2020-05-14 03:54:50 +00:00
where
2020-05-15 22:01:29 +00:00
Inner: Extends<Kind, Error = serde_json::Error> + UnparsedMut + markers::Actor,
Kind: serde::de::DeserializeOwned + serde::ser::Serialize,
2020-05-14 03:54:50 +00:00
{
type Error = serde_json::Error;
2020-05-15 22:01:29 +00:00
fn extends(base: Base<Kind>) -> Result<Self, Self::Error> {
let inner = Inner::extends(base)?;
2020-05-14 03:54:50 +00:00
Self::extending(inner)
}
2020-05-15 22:01:29 +00:00
fn retracts(self) -> Result<Base<Kind>, Self::Error> {
let inner = self.retracting()?;
inner.retracts()
2020-05-14 03:54:50 +00:00
}
}
2020-05-15 03:18:34 +00:00
impl<Inner> UnparsedMut for ApActor<Inner>
2020-05-14 03:54:50 +00:00
where
2020-05-15 03:18:34 +00:00
Inner: UnparsedMut,
2020-05-14 03:54:50 +00:00
{
fn unparsed_mut(&mut self) -> &mut Unparsed {
self.inner.unparsed_mut()
}
}
2020-05-15 03:18:34 +00:00
2020-05-15 22:01:29 +00:00
impl<Inner, Kind> AsBase<Kind> for ApActor<Inner>
2020-05-15 03:18:34 +00:00
where
2020-05-15 22:01:29 +00:00
Inner: AsBase<Kind>,
2020-05-15 03:18:34 +00:00
{
2020-05-15 22:01:29 +00:00
fn base_ref(&self) -> &Base<Kind> {
self.inner.base_ref()
}
fn base_mut(&mut self) -> &mut Base<Kind> {
self.inner.base_mut()
2020-05-15 03:18:34 +00:00
}
}
2020-05-15 22:01:29 +00:00
impl<Inner, Kind> AsObject<Kind> for ApActor<Inner>
2020-05-15 03:18:34 +00:00
where
2020-05-15 22:01:29 +00:00
Inner: AsObject<Kind>,
2020-05-15 03:18:34 +00:00
{
2020-05-15 22:01:29 +00:00
fn object_ref(&self) -> &Object<Kind> {
self.inner.object_ref()
}
2020-05-15 03:18:34 +00:00
fn object_mut(&mut self) -> &mut Object<Kind> {
self.inner.object_mut()
}
}
2020-05-15 22:01:29 +00:00
impl<Inner> AsApActor<Inner> for ApActor<Inner>
2020-05-15 03:18:34 +00:00
where
2020-05-15 22:01:29 +00:00
Inner: markers::Actor,
2020-05-15 03:18:34 +00:00
{
fn ap_actor_ref(&self) -> &ApActor<Inner> {
self
}
2020-05-15 22:01:29 +00:00
fn ap_actor_mut(&mut self) -> &mut ApActor<Inner> {
self
}
2020-05-15 03:18:34 +00:00
}
2020-05-15 22:01:29 +00:00
impl<Inner1, Inner2> AsApObject<Inner2> for ApActor<Inner1>
2020-05-15 03:18:34 +00:00
where
2020-05-15 22:01:29 +00:00
Inner1: AsApObject<Inner2>,
2020-05-15 03:18:34 +00:00
{
2020-05-15 22:01:29 +00:00
fn ap_object_ref(&self) -> &ApObject<Inner2> {
self.inner.ap_object_ref()
}
fn ap_object_mut(&mut self) -> &mut ApObject<Inner2> {
self.inner.ap_object_mut()
2020-05-15 03:18:34 +00:00
}
}
2020-05-15 22:01:29 +00:00
impl<T, Inner> ApActorExt<Inner> for T where T: AsApActor<Inner> {}