activitystreams-new/src/actor.rs

392 lines
10 KiB
Rust

use crate::{
object::{Object, ObjectMut, ObjectRef},
primitives::{OneOrMany, Unparsed, XsdAnyUri, XsdString},
traits::{self, Extends, UnparsedMut, UnparsedMutExt},
};
use typed_builder::TypedBuilder;
pub mod kind {
pub use activitystreams::actor::kind::*;
}
use self::kind::*;
pub trait ApActorRef<Inner>: traits::Actor {
fn ap_actor_ref(&self) -> &ApActor<Inner>;
}
pub trait ApActorMut<Inner>: traits::Actor {
fn ap_actor_mut(&mut self) -> &mut ApActor<Inner>;
}
pub trait ApActorRefExt<Inner>: ApActorRef<Inner> {
fn inbox<'a>(&'a self) -> &'a XsdAnyUri
where
Inner: 'a,
{
&self.ap_actor_ref().inbox
}
fn outbox<'a>(&'a self) -> &'a XsdAnyUri
where
Inner: 'a,
{
&self.ap_actor_ref().outbox
}
fn following<'a>(&'a self) -> Option<&'a XsdAnyUri>
where
Inner: 'a,
{
self.ap_actor_ref().following.as_ref()
}
fn followers<'a>(&'a self) -> Option<&'a XsdAnyUri>
where
Inner: 'a,
{
self.ap_actor_ref().followers.as_ref()
}
fn liked<'a>(&'a self) -> Option<&'a XsdAnyUri>
where
Inner: 'a,
{
self.ap_actor_ref().liked.as_ref()
}
fn streams<'a>(&'a self) -> Option<&'a OneOrMany<XsdAnyUri>>
where
Inner: 'a,
{
self.ap_actor_ref().streams.as_ref()
}
fn preferred_username<'a>(&'a self) -> Option<&'a XsdString>
where
Inner: 'a,
{
self.ap_actor_ref().preferred_username.as_ref()
}
fn endpoints<'a>(&'a self) -> Option<&'a Endpoints>
where
Inner: 'a,
{
self.ap_actor_ref().endpoints.as_ref()
}
}
pub trait ApActorMutExt<Inner>: ApActorMut<Inner> {
fn set_inbox(&mut self, inbox: XsdAnyUri) -> &mut Self {
self.ap_actor_mut().inbox = inbox;
self
}
fn set_outbox(&mut self, outbox: XsdAnyUri) -> &mut Self {
self.ap_actor_mut().outbox = outbox;
self
}
fn set_following(&mut self, following: XsdAnyUri) -> &mut Self {
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
}
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
}
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()
}
fn delete_likes(&mut self) -> &mut Self {
self.ap_actor_mut().liked = None;
self
}
fn set_streams(&mut self, streams: XsdAnyUri) -> &mut Self {
self.ap_actor_mut().streams = Some(streams.into());
self
}
fn set_many_streams<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
}
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
}
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
}
}
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>;
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize, TypedBuilder)]
#[serde(rename_all = "camelCase")]
#[builder(doc)]
pub struct ApActor<Inner> {
pub inbox: XsdAnyUri,
pub outbox: XsdAnyUri,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option))]
pub following: Option<XsdAnyUri>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option))]
pub followers: Option<XsdAnyUri>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option))]
pub liked: Option<XsdAnyUri>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option, into))]
pub streams: Option<OneOrMany<XsdAnyUri>>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option))]
pub preferred_username: Option<XsdString>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option))]
pub endpoints: Option<Endpoints>,
#[serde(flatten)]
pub inner: Inner,
}
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize, TypedBuilder)]
#[serde(rename_all = "camelCase")]
pub struct Endpoints {
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option))]
pub proxy_url: Option<XsdAnyUri>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option))]
pub oauth_authorization_endpoint: Option<XsdAnyUri>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option))]
pub oauth_token_endpoint: Option<XsdAnyUri>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option))]
pub provide_client_key: Option<XsdAnyUri>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option))]
pub sign_client_key: Option<XsdAnyUri>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option))]
pub shared_inbox: Option<XsdAnyUri>,
}
impl<Inner> ApActor<Inner> {
fn extending(mut inner: Inner) -> Result<Self, serde_json::Error>
where
Inner: UnparsedMut + traits::Actor,
{
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
Inner: UnparsedMut + traits::Actor,
{
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)
}
}
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<Inner> traits::Base for ApActor<Inner> where Inner: traits::Base {}
impl<Inner> traits::Object for ApActor<Inner> where Inner: traits::Object {}
impl<Inner> traits::Actor for ApActor<Inner> where Inner: traits::Actor {}
impl<Inner> Extends<Inner> for ApActor<Inner>
where
Inner: UnparsedMut + traits::Actor,
{
type Error = serde_json::Error;
fn extends(inner: Inner) -> Result<Self, Self::Error> {
Self::extending(inner)
}
fn retracts(self) -> Result<Inner, Self::Error> {
self.retracting()
}
}
impl<Inner> UnparsedMut for ApActor<Inner>
where
Inner: UnparsedMut,
{
fn unparsed_mut(&mut self) -> &mut Unparsed {
self.inner.unparsed_mut()
}
}
impl<Inner, Kind> ObjectRef<Kind> for ApActor<Inner>
where
Inner: ObjectRef<Kind>,
{
fn object_ref(&self) -> &Object<Kind> {
self.inner.object_ref()
}
}
impl<Inner, Kind> ObjectMut<Kind> for ApActor<Inner>
where
Inner: ObjectMut<Kind>,
{
fn object_mut(&mut self) -> &mut Object<Kind> {
self.inner.object_mut()
}
}
impl<Inner> ApActorRef<Inner> for ApActor<Inner>
where
Inner: traits::Actor,
{
fn ap_actor_ref(&self) -> &ApActor<Inner> {
self
}
}
impl<Inner> ApActorMut<Inner> for ApActor<Inner>
where
Inner: traits::Actor,
{
fn ap_actor_mut(&mut self) -> &mut ApActor<Inner> {
self
}
}
impl<T, Inner> ApActorRefExt<Inner> for T where T: ApActorRef<Inner> {}
impl<T, Inner> ApActorMutExt<Inner> for T where T: ApActorMut<Inner> {}