From 07f3d2add3a931cd98f57330d14c590f6088c32b Mon Sep 17 00:00:00 2001 From: asonix Date: Sat, 12 May 2018 14:35:10 -0500 Subject: [PATCH] Use codegen to reduce library size by a lot --- Cargo.toml | 3 +- activitystreams-derive/.gitignore | 3 + activitystreams-derive/Cargo.toml | 12 + activitystreams-derive/src/lib.rs | 351 ++++++++++++++ src/activity/accept.rs | 37 +- src/activity/add.rs | 37 +- src/activity/amove.rs | 74 +-- src/activity/announce.rs | 56 +-- src/activity/arrive.rs | 63 +-- src/activity/block.rs | 37 +- src/activity/create.rs | 37 +- src/activity/delete.rs | 56 +-- src/activity/dislike.rs | 37 +- src/activity/flag.rs | 37 +- src/activity/follow.rs | 37 +- src/activity/ignore.rs | 37 +- src/activity/invite.rs | 55 +-- src/activity/join.rs | 37 +- src/activity/kind.rs | 117 +++++ src/activity/kind/accept.rs | 47 -- src/activity/kind/add.rs | 47 -- src/activity/kind/amove.rs | 47 -- src/activity/kind/announce.rs | 47 -- src/activity/kind/arrive.rs | 47 -- src/activity/kind/block.rs | 47 -- src/activity/kind/create.rs | 47 -- src/activity/kind/delete.rs | 47 -- src/activity/kind/dislike.rs | 47 -- src/activity/kind/flag.rs | 47 -- src/activity/kind/follow.rs | 47 -- src/activity/kind/ignore.rs | 47 -- src/activity/kind/invite.rs | 47 -- src/activity/kind/join.rs | 47 -- src/activity/kind/leave.rs | 47 -- src/activity/kind/like.rs | 47 -- src/activity/kind/listen.rs | 47 -- src/activity/kind/mod.rs | 57 --- src/activity/kind/offer.rs | 47 -- src/activity/kind/question.rs | 47 -- src/activity/kind/read.rs | 47 -- src/activity/kind/reject.rs | 47 -- src/activity/kind/remove.rs | 47 -- src/activity/kind/tentative_accept.rs | 47 -- src/activity/kind/tentative_reject.rs | 47 -- src/activity/kind/travel.rs | 47 -- src/activity/kind/undo.rs | 47 -- src/activity/kind/update.rs | 47 -- src/activity/kind/view.rs | 47 -- src/activity/leave.rs | 37 +- src/activity/like.rs | 37 +- src/activity/listen.rs | 37 +- src/activity/offer.rs | 56 +-- src/activity/properties.rs | 41 +- src/activity/question.rs | 57 +-- src/activity/read.rs | 37 +- src/activity/reject.rs | 37 +- src/activity/remove.rs | 74 +-- src/activity/tentative_accept.rs | 37 +- src/activity/tentative_reject.rs | 37 +- src/activity/travel.rs | 64 +-- src/activity/undo.rs | 37 +- src/activity/update.rs | 37 +- src/activity/view.rs | 37 +- src/actor/kind.rs | 25 + src/actor/kind/application.rs | 47 -- src/actor/kind/group.rs | 47 -- src/actor/kind/mod.rs | 11 - src/actor/kind/organization.rs | 47 -- src/actor/kind/person.rs | 47 -- src/actor/kind/service.rs | 47 -- src/actor/mod.rs | 5 + src/base.rs | 3 - src/collection/kind.rs | 21 + src/collection/kind/collection.rs | 47 -- src/collection/kind/collection_page.rs | 47 -- src/collection/kind/mod.rs | 9 - src/collection/kind/ordered_collection.rs | 47 -- .../kind/ordered_collection_page.rs | 47 -- src/collection/mod.rs | 34 +- src/collection/properties.rs | 87 +--- src/lib.rs | 30 +- src/link/kind.rs | 42 +- src/link/mod.rs | 1 + src/link/properties.rs | 78 ++-- src/object/kind.rs | 53 +++ src/object/kind/article.rs | 47 -- src/object/kind/audio.rs | 47 -- src/object/kind/document.rs | 47 -- src/object/kind/event.rs | 47 -- src/object/kind/image.rs | 47 -- src/object/kind/mod.rs | 25 - src/object/kind/note.rs | 47 -- src/object/kind/page.rs | 47 -- src/object/kind/place.rs | 47 -- src/object/kind/profile.rs | 47 -- src/object/kind/relationship.rs | 47 -- src/object/kind/tombstone.rs | 47 -- src/object/kind/video.rs | 47 -- src/object/mod.rs | 61 ++- src/object/properties.rs | 438 ++++-------------- 100 files changed, 1070 insertions(+), 3998 deletions(-) create mode 100644 activitystreams-derive/.gitignore create mode 100644 activitystreams-derive/Cargo.toml create mode 100644 activitystreams-derive/src/lib.rs create mode 100644 src/activity/kind.rs delete mode 100644 src/activity/kind/accept.rs delete mode 100644 src/activity/kind/add.rs delete mode 100644 src/activity/kind/amove.rs delete mode 100644 src/activity/kind/announce.rs delete mode 100644 src/activity/kind/arrive.rs delete mode 100644 src/activity/kind/block.rs delete mode 100644 src/activity/kind/create.rs delete mode 100644 src/activity/kind/delete.rs delete mode 100644 src/activity/kind/dislike.rs delete mode 100644 src/activity/kind/flag.rs delete mode 100644 src/activity/kind/follow.rs delete mode 100644 src/activity/kind/ignore.rs delete mode 100644 src/activity/kind/invite.rs delete mode 100644 src/activity/kind/join.rs delete mode 100644 src/activity/kind/leave.rs delete mode 100644 src/activity/kind/like.rs delete mode 100644 src/activity/kind/listen.rs delete mode 100644 src/activity/kind/mod.rs delete mode 100644 src/activity/kind/offer.rs delete mode 100644 src/activity/kind/question.rs delete mode 100644 src/activity/kind/read.rs delete mode 100644 src/activity/kind/reject.rs delete mode 100644 src/activity/kind/remove.rs delete mode 100644 src/activity/kind/tentative_accept.rs delete mode 100644 src/activity/kind/tentative_reject.rs delete mode 100644 src/activity/kind/travel.rs delete mode 100644 src/activity/kind/undo.rs delete mode 100644 src/activity/kind/update.rs delete mode 100644 src/activity/kind/view.rs create mode 100644 src/actor/kind.rs delete mode 100644 src/actor/kind/application.rs delete mode 100644 src/actor/kind/group.rs delete mode 100644 src/actor/kind/mod.rs delete mode 100644 src/actor/kind/organization.rs delete mode 100644 src/actor/kind/person.rs delete mode 100644 src/actor/kind/service.rs delete mode 100644 src/base.rs create mode 100644 src/collection/kind.rs delete mode 100644 src/collection/kind/collection.rs delete mode 100644 src/collection/kind/collection_page.rs delete mode 100644 src/collection/kind/mod.rs delete mode 100644 src/collection/kind/ordered_collection.rs delete mode 100644 src/collection/kind/ordered_collection_page.rs create mode 100644 src/object/kind.rs delete mode 100644 src/object/kind/article.rs delete mode 100644 src/object/kind/audio.rs delete mode 100644 src/object/kind/document.rs delete mode 100644 src/object/kind/event.rs delete mode 100644 src/object/kind/image.rs delete mode 100644 src/object/kind/mod.rs delete mode 100644 src/object/kind/note.rs delete mode 100644 src/object/kind/page.rs delete mode 100644 src/object/kind/place.rs delete mode 100644 src/object/kind/profile.rs delete mode 100644 src/object/kind/relationship.rs delete mode 100644 src/object/kind/tombstone.rs delete mode 100644 src/object/kind/video.rs diff --git a/Cargo.toml b/Cargo.toml index aeda562..16e6540 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,10 @@ [package] -name = "activitypub" +name = "activitystreams" version = "0.1.0" authors = ["asonix "] [dependencies] +activitystreams-derive = { version = "0.1", path = "activitystreams-derive" } failure = "0.1" mime = "0.3" serde = "1.0" diff --git a/activitystreams-derive/.gitignore b/activitystreams-derive/.gitignore new file mode 100644 index 0000000..6936990 --- /dev/null +++ b/activitystreams-derive/.gitignore @@ -0,0 +1,3 @@ +/target +**/*.rs.bk +Cargo.lock diff --git a/activitystreams-derive/Cargo.toml b/activitystreams-derive/Cargo.toml new file mode 100644 index 0000000..60e4ca3 --- /dev/null +++ b/activitystreams-derive/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "activitystreams-derive" +version = "0.1.0" +authors = ["asonix "] + +[dependencies] +quote = "0.5" +syn = "0.13" +proc-macro2 = "0.3" + +[lib] +proc-macro = true diff --git a/activitystreams-derive/src/lib.rs b/activitystreams-derive/src/lib.rs new file mode 100644 index 0000000..85f79fe --- /dev/null +++ b/activitystreams-derive/src/lib.rs @@ -0,0 +1,351 @@ +extern crate proc_macro; +extern crate proc_macro2; +extern crate syn; + +#[macro_use] +extern crate quote; + +use proc_macro::TokenStream; +use proc_macro2::TokenTree; +use quote::Tokens; +use syn::{Attribute, Data, DeriveInput, Fields, Ident, Type}; + +#[proc_macro_derive(UnitString, attributes(activitystreams))] +pub fn unit_string(input: TokenStream) -> TokenStream { + let input: DeriveInput = syn::parse(input).unwrap(); + + let name = input.ident; + + let attr = input + .attrs + .iter() + .find(|attribute| { + attribute + .path + .segments + .last() + .map(|segment| { + let segment = segment.into_value(); + segment.ident == Ident::new("activitystreams", segment.ident.span()) + }) + .unwrap_or(false) + }) + .unwrap() + .clone(); + + let value = get_value(attr); + + let visitor_name = Ident::from(format!("{}Visitor", value)); + + let serialize = quote! { + impl Serialize for #name { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(#value) + } + } + }; + + let expecting = quote! { + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "The string '{}'", #value) + } + }; + + let visit = quote! { + fn visit_str(self, v: &str) -> Result + where + E: de::Error, + { + if v == #value { + Ok(#name) + } else { + Err(de::Error::custom("Invalid type")) + } + } + }; + + let visitor = quote! { + pub struct #visitor_name; + + impl<'de> Visitor<'de> for #visitor_name { + type Value = #name; + + #expecting + + #visit + } + }; + + let deserialize = quote! { + impl<'de> Deserialize<'de> for #name { + fn deserialize(deserializer: D) -> Result<#name, D::Error> + where + D: Deserializer<'de>, + { + deserializer.deserialize_str(#visitor_name) + } + } + }; + + let c = quote! { + #serialize + #visitor + #deserialize + }; + + c.into() +} + +fn get_value(attr: Attribute) -> String { + let group = attr.tts + .clone() + .into_iter() + .filter_map(|token_tree| match token_tree { + TokenTree::Group(group) => Some(group), + _ => None, + }) + .next() + .unwrap(); + + group + .stream() + .clone() + .into_iter() + .filter_map(|token_tree| match token_tree { + TokenTree::Term(term) => Some(term.as_str().to_owned()), + _ => None, + }) + .next() + .unwrap() +} + +#[proc_macro_derive(Properties, attributes(activitystreams))] +pub fn properties_derive(input: TokenStream) -> TokenStream { + let input: DeriveInput = syn::parse(input).unwrap(); + + let name = input.ident; + + let data = match input.data { + Data::Struct(s) => s, + _ => panic!("Can only derive for structs"), + }; + + let fields = match data.fields { + Fields::Named(fields) => fields, + _ => panic!("Can only derive for named fields"), + }; + + let getters = fields + .named + .iter() + .filter_map(|field| { + let our_attr = field.attrs.iter().find(|attribute| { + attribute + .path + .segments + .last() + .map(|segment| { + let segment = segment.into_value(); + segment.ident == Ident::new("activitystreams", segment.ident.span()) + }) + .unwrap_or(false) + }); + + if our_attr.is_some() { + let our_attr = our_attr.unwrap(); + + let is_option = match field.ty { + Type::Path(ref path) => path.path + .segments + .last() + .map(|seg| { + let seg = seg.into_value(); + seg.ident == Ident::new("Option", seg.ident.span()) + }) + .unwrap_or(false), + _ => false, + }; + + let is_vec = match field.ty { + Type::Path(ref path) => path.path + .segments + .last() + .map(|seg| { + let seg = seg.into_value(); + seg.ident == Ident::new("Vec", seg.ident.span()) + }) + .unwrap_or(false), + _ => false, + }; + + Some(( + field.ident.clone().unwrap(), + is_option, + is_vec, + is_functional(our_attr.clone()), + our_attr.clone(), + )) + } else { + None + } + }) + .flat_map(|(ident, is_option, is_vec, is_functional, attr)| { + variants(attr) + .into_iter() + .map(move |(variant, is_concrete)| { + let lower_variant = variant.to_lowercase(); + let fn_name = Ident::from(format!("{}_{}", ident, lower_variant)); + let fn_plural = Ident::from(format!("{}_{}_vec", ident, lower_variant)); + let variant = Ident::from(variant); + + if is_concrete && is_option && is_functional { + quote! { + pub fn #fn_name(&self) -> Result<#variant> { + self.get_item(|t| &t.#ident) + } + } + } else if is_concrete && is_option { + quote! { + pub fn #fn_name(&self) -> Result<#variant> { + self.get_item(|t| &t.#ident) + } + + pub fn #fn_plural(&self) -> Result> { + self.get_item(|t| &t.#ident) + } + } + } else if is_concrete && is_vec { + quote! { + pub fn #fn_name(&self) -> Result> { + self.get_vec(|t| &t.#ident) + } + } + } else if is_concrete && is_functional { + quote! { + pub fn #fn_name(&self) -> Result<#variant> { + self.get_value(|t| &t.#ident) + } + } + } else if is_concrete { + quote! { + pub fn #fn_name(&self) -> Result<#variant> { + self.get_value(|t| &t.#ident) + } + + pub fn #fn_plural(&self) -> Result> { + self.get_value(|t| &t.#ident) + } + } + } else if is_option && is_functional { + quote! { + pub fn #fn_name(&self) -> Result { + self.get_item(|t| &t.#ident) + } + } + } else if is_option { + quote! { + pub fn #fn_name(&self) -> Result { + self.get_item(|t| &t.#ident) + } + + pub fn #fn_plural(&self) -> Result> { + self.get_item(|t| &t.#ident) + } + } + } else if is_vec { + quote! { + pub fn #fn_name(&self) -> Result> { + self.get_vec(|t| &t.#ident) + } + } + } else if is_functional { + quote! { + pub fn #fn_name(&self) -> Result { + self.get_value(|t| &t.#ident) + } + } + } else { + quote! { + pub fn #fn_name(&self) -> Result { + self.get_value(|t| &t.#ident) + } + + pub fn #fn_plural(&self) -> Result> { + self.get_value(|t| &t.#ident) + } + } + } + }) + }); + + let mut tokens = Tokens::new(); + tokens.append_all(getters); + + let full = quote!{ + impl Properties for #name {} + + impl #name { + #tokens + } + }; + + full.into() +} + +fn variants(attr: Attribute) -> Vec<(String, bool)> { + let group = attr.tts + .clone() + .into_iter() + .filter_map(|token_tree| match token_tree { + TokenTree::Group(group) => Some(group), + _ => None, + }) + .next() + .unwrap(); + + let mut is_concrete = false; + + group + .stream() + .clone() + .into_iter() + .filter_map(|token_tree| match token_tree { + TokenTree::Term(term) => { + is_concrete = term.as_str() == "concrete"; + None + } + TokenTree::Group(group) => Some(group.stream().into_iter().filter_map( + move |token_tree| match token_tree { + TokenTree::Term(term) => Some((term.as_str().to_owned(), is_concrete)), + _ => None, + }, + )), + _ => None, + }) + .flat_map(|i| i) + .collect() +} + +fn is_functional(attr: Attribute) -> bool { + let group = attr.tts + .clone() + .into_iter() + .filter_map(|token_tree| match token_tree { + TokenTree::Group(group) => Some(group), + _ => None, + }) + .next() + .unwrap(); + + group + .stream() + .clone() + .into_iter() + .any(|token_tree| match token_tree { + TokenTree::Term(term) => term.as_str() == "functional", + _ => false, + }) +} diff --git a/src/activity/accept.rs b/src/activity/accept.rs index 9fa750e..14574e7 100644 --- a/src/activity/accept.rs +++ b/src/activity/accept.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::AcceptType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Accept { #[serde(rename = "type")] kind: AcceptType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Accept { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Accept {} impl Activity for Accept {} diff --git a/src/activity/add.rs b/src/activity/add.rs index 1ee73ca..cb13e29 100644 --- a/src/activity/add.rs +++ b/src/activity/add.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::AddType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Add { #[serde(rename = "type")] kind: AddType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Add { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Add {} impl Activity for Add {} diff --git a/src/activity/amove.rs b/src/activity/amove.rs index f0f77ed..24953f1 100644 --- a/src/activity/amove.rs +++ b/src/activity/amove.rs @@ -2,87 +2,37 @@ use serde_json; use super::{kind::MoveType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct AMove { #[serde(rename = "type")] kind: MoveType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] origin: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] target: Option, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Properties for AMove {} - -impl AMove { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn origin(&self) -> Result { - self.get_item(|m| &m.origin) - } - - pub fn origins(&self) -> Result> { - self.get_item(|m| &m.origin) - } - - pub fn origin_link(&self) -> Result { - self.get_item(|m| &m.origin) - } - - pub fn origin_links(&self) -> Result> { - self.get_item(|m| &m.origin) - } - - pub fn target(&self) -> Result { - self.get_item(|m| &m.target) - } - - pub fn targets(&self) -> Result> { - self.get_item(|m| &m.target) - } - - pub fn target_link(&self) -> Result { - self.get_item(|m| &m.target) - } - - pub fn target_links(&self) -> Result> { - self.get_item(|m| &m.target) - } -} - impl Object for AMove {} impl Activity for AMove {} diff --git a/src/activity/announce.rs b/src/activity/announce.rs index 7052f34..d076cd7 100644 --- a/src/activity/announce.rs +++ b/src/activity/announce.rs @@ -2,69 +2,33 @@ use serde_json; use super::{kind::AnnounceType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Announce { #[serde(rename = "type")] kind: AnnounceType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] target: Option, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Properties for Announce {} - -impl Announce { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn target(&self) -> Result { - self.get_item(|m| &m.target) - } - - pub fn targets(&self) -> Result> { - self.get_item(|m| &m.target) - } - - pub fn target_link(&self) -> Result { - self.get_item(|m| &m.target) - } - - pub fn target_links(&self) -> Result> { - self.get_item(|m| &m.target) - } -} - impl Object for Announce {} impl Activity for Announce {} diff --git a/src/activity/arrive.rs b/src/activity/arrive.rs index 761623f..349c70f 100644 --- a/src/activity/arrive.rs +++ b/src/activity/arrive.rs @@ -2,74 +2,33 @@ use serde_json; use super::{kind::ArriveType, properties::ActivityProperties, Activity, IntransitiveActivity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Arrive { #[serde(rename = "type")] kind: ArriveType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] location: serde_json::Value, + + #[activitystreams(ab(Object, Link))] origin: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Arrive { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn location(&self) -> Result { - serde_json::from_value(self.location.clone()).map_err(|_| Error::Deserialize) - } - - pub fn locations(&self) -> Result> { - serde_json::from_value(self.location.clone()).map_err(|_| Error::Deserialize) - } - - pub fn location_link(&self) -> Result { - serde_json::from_value(self.location.clone()).map_err(|_| Error::Deserialize) - } - - pub fn location_links(&self) -> Result> { - serde_json::from_value(self.location.clone()).map_err(|_| Error::Deserialize) - } - - pub fn origin(&self) -> Result { - serde_json::from_value(self.origin.clone()).map_err(|_| Error::Deserialize) - } - - pub fn origins(&self) -> Result> { - serde_json::from_value(self.origin.clone()).map_err(|_| Error::Deserialize) - } - - pub fn origin_link(&self) -> Result { - serde_json::from_value(self.origin.clone()).map_err(|_| Error::Deserialize) - } - - pub fn origin_links(&self) -> Result> { - serde_json::from_value(self.origin.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Arrive {} impl Activity for Arrive {} impl IntransitiveActivity for Arrive {} diff --git a/src/activity/block.rs b/src/activity/block.rs index 5c2bdbf..2bad4fe 100644 --- a/src/activity/block.rs +++ b/src/activity/block.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::BlockType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Block { #[serde(rename = "type")] kind: BlockType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Block { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Block {} impl Activity for Block {} diff --git a/src/activity/create.rs b/src/activity/create.rs index b01d40a..be943b0 100644 --- a/src/activity/create.rs +++ b/src/activity/create.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::CreateType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Create { #[serde(rename = "type")] kind: CreateType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Create { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Create {} impl Activity for Create {} diff --git a/src/activity/delete.rs b/src/activity/delete.rs index 49d1b7f..493a7a7 100644 --- a/src/activity/delete.rs +++ b/src/activity/delete.rs @@ -2,69 +2,33 @@ use serde_json; use super::{kind::DeleteType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Delete { #[serde(rename = "type")] kind: DeleteType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] origin: Option, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Properties for Delete {} - -impl Delete { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn origin(&self) -> Result { - self.get_item(|d| &d.origin) - } - - pub fn origins(&self) -> Result { - self.get_item(|d| &d.origin) - } - - pub fn origin_link(&self) -> Result { - self.get_item(|d| &d.origin) - } - - pub fn origin_links(&self) -> Result> { - self.get_item(|d| &d.origin) - } -} - impl Object for Delete {} impl Activity for Delete {} diff --git a/src/activity/dislike.rs b/src/activity/dislike.rs index eb797fa..bf5b047 100644 --- a/src/activity/dislike.rs +++ b/src/activity/dislike.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::DislikeType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Dislike { #[serde(rename = "type")] kind: DislikeType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Dislike { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Dislike {} impl Activity for Dislike {} diff --git a/src/activity/flag.rs b/src/activity/flag.rs index 5f58ab6..6b5af81 100644 --- a/src/activity/flag.rs +++ b/src/activity/flag.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::FlagType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Flag { #[serde(rename = "type")] kind: FlagType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Flag { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Flag {} impl Activity for Flag {} diff --git a/src/activity/follow.rs b/src/activity/follow.rs index 844801a..462f9bb 100644 --- a/src/activity/follow.rs +++ b/src/activity/follow.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::FollowType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Follow { #[serde(rename = "type")] kind: FollowType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Follow { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Follow {} impl Activity for Follow {} diff --git a/src/activity/ignore.rs b/src/activity/ignore.rs index 314bdd3..4df88e4 100644 --- a/src/activity/ignore.rs +++ b/src/activity/ignore.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::IgnoreType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Ignore { #[serde(rename = "type")] kind: IgnoreType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Ignore { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Ignore {} impl Activity for Ignore {} diff --git a/src/activity/invite.rs b/src/activity/invite.rs index 268ee2e..1a4a855 100644 --- a/src/activity/invite.rs +++ b/src/activity/invite.rs @@ -2,65 +2,32 @@ use serde_json; use super::{kind::InviteType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Invite { #[serde(rename = "type")] kind: InviteType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + + #[activitystreams(ab(Object, Link))] target: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Invite { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn target(&self) -> Result { - serde_json::from_value(self.target.clone()).map_err(|_| Error::Deserialize) - } - - pub fn targets(&self) -> Result> { - serde_json::from_value(self.target.clone()).map_err(|_| Error::Deserialize) - } - - pub fn target_link(&self) -> Result { - serde_json::from_value(self.target.clone()).map_err(|_| Error::Deserialize) - } - - pub fn target_links(&self) -> Result> { - serde_json::from_value(self.target.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Invite {} impl Activity for Invite {} diff --git a/src/activity/join.rs b/src/activity/join.rs index f6c4a64..e4644b9 100644 --- a/src/activity/join.rs +++ b/src/activity/join.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::JoinType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Join { #[serde(rename = "type")] kind: JoinType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Join { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Join {} impl Activity for Join {} diff --git a/src/activity/kind.rs b/src/activity/kind.rs new file mode 100644 index 0000000..53ecb8d --- /dev/null +++ b/src/activity/kind.rs @@ -0,0 +1,117 @@ +use std::fmt; + +use serde::{ + de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, +}; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Accept)] +pub struct AcceptType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Add)] +pub struct AddType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Move)] +pub struct MoveType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Announce)] +pub struct AnnounceType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Arrive)] +pub struct ArriveType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Block)] +pub struct BlockType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Create)] +pub struct CreateType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Delete)] +pub struct DeleteType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Dislike)] +pub struct DislikeType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Flag)] +pub struct FlagType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Follow)] +pub struct FollowType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Ignore)] +pub struct IgnoreType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Invite)] +pub struct InviteType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Join)] +pub struct JoinType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Leave)] +pub struct LeaveType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Like)] +pub struct LikeType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Listen)] +pub struct ListenType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Offer)] +pub struct OfferType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Question)] +pub struct QuestionType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Real)] +pub struct ReadType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Reject)] +pub struct RejectType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Remove)] +pub struct RemoveType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(TentativeAccept)] +pub struct TentativeAcceptType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(TentativeReject)] +pub struct TentativeRejectType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Travel)] +pub struct TravelType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Undo)] +pub struct UndoType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Update)] +pub struct UpdateType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(View)] +pub struct ViewType; diff --git a/src/activity/kind/accept.rs b/src/activity/kind/accept.rs deleted file mode 100644 index f12f8a5..0000000 --- a/src/activity/kind/accept.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct AcceptType; - -impl Serialize for AcceptType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Accept") - } -} - -pub struct AcceptTypeVisitor; - -impl<'de> Visitor<'de> for AcceptTypeVisitor { - type Value = AcceptType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Accept'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Accept" { - Ok(AcceptType) - } else { - Err(de::Error::custom("Type not Accept")) - } - } -} - -impl<'de> Deserialize<'de> for AcceptType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(AcceptTypeVisitor) - } -} diff --git a/src/activity/kind/add.rs b/src/activity/kind/add.rs deleted file mode 100644 index 1327b3f..0000000 --- a/src/activity/kind/add.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct AddType; - -impl Serialize for AddType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Add") - } -} - -pub struct AddTypeVisitor; - -impl<'de> Visitor<'de> for AddTypeVisitor { - type Value = AddType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Add'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Add" { - Ok(AddType) - } else { - Err(de::Error::custom("Type not create")) - } - } -} - -impl<'de> Deserialize<'de> for AddType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(AddTypeVisitor) - } -} diff --git a/src/activity/kind/amove.rs b/src/activity/kind/amove.rs deleted file mode 100644 index 7fca201..0000000 --- a/src/activity/kind/amove.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct MoveType; - -impl Serialize for MoveType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Move") - } -} - -pub struct MoveTypeVisitor; - -impl<'de> Visitor<'de> for MoveTypeVisitor { - type Value = MoveType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Move'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Move" { - Ok(MoveType) - } else { - Err(de::Error::custom("Type not Move")) - } - } -} - -impl<'de> Deserialize<'de> for MoveType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(MoveTypeVisitor) - } -} diff --git a/src/activity/kind/announce.rs b/src/activity/kind/announce.rs deleted file mode 100644 index 467637b..0000000 --- a/src/activity/kind/announce.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct AnnounceType; - -impl Serialize for AnnounceType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Announce") - } -} - -pub struct AnnounceTypeVisitor; - -impl<'de> Visitor<'de> for AnnounceTypeVisitor { - type Value = AnnounceType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Announce'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Announce" { - Ok(AnnounceType) - } else { - Err(de::Error::custom("Type not Announce")) - } - } -} - -impl<'de> Deserialize<'de> for AnnounceType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(AnnounceTypeVisitor) - } -} diff --git a/src/activity/kind/arrive.rs b/src/activity/kind/arrive.rs deleted file mode 100644 index a3f84df..0000000 --- a/src/activity/kind/arrive.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct ArriveType; - -impl Serialize for ArriveType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Arrive") - } -} - -pub struct ArriveTypeVisitor; - -impl<'de> Visitor<'de> for ArriveTypeVisitor { - type Value = ArriveType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Arrive'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Arrive" { - Ok(ArriveType) - } else { - Err(de::Error::custom("Type not Arrive")) - } - } -} - -impl<'de> Deserialize<'de> for ArriveType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(ArriveTypeVisitor) - } -} diff --git a/src/activity/kind/block.rs b/src/activity/kind/block.rs deleted file mode 100644 index 33c4f29..0000000 --- a/src/activity/kind/block.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct BlockType; - -impl Serialize for BlockType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Block") - } -} - -pub struct BlockTypeVisitor; - -impl<'de> Visitor<'de> for BlockTypeVisitor { - type Value = BlockType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Block'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Block" { - Ok(BlockType) - } else { - Err(de::Error::custom("Type not Block")) - } - } -} - -impl<'de> Deserialize<'de> for BlockType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(BlockTypeVisitor) - } -} diff --git a/src/activity/kind/create.rs b/src/activity/kind/create.rs deleted file mode 100644 index e575d73..0000000 --- a/src/activity/kind/create.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct CreateType; - -impl Serialize for CreateType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Create") - } -} - -pub struct CreateTypeVisitor; - -impl<'de> Visitor<'de> for CreateTypeVisitor { - type Value = CreateType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Create'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Create" { - Ok(CreateType) - } else { - Err(de::Error::custom("Type not create")) - } - } -} - -impl<'de> Deserialize<'de> for CreateType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(CreateTypeVisitor) - } -} diff --git a/src/activity/kind/delete.rs b/src/activity/kind/delete.rs deleted file mode 100644 index caec804..0000000 --- a/src/activity/kind/delete.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct DeleteType; - -impl Serialize for DeleteType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Delete") - } -} - -pub struct DeleteTypeVisitor; - -impl<'de> Visitor<'de> for DeleteTypeVisitor { - type Value = DeleteType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Delete'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Delete" { - Ok(DeleteType) - } else { - Err(de::Error::custom("Type not Delete")) - } - } -} - -impl<'de> Deserialize<'de> for DeleteType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(DeleteTypeVisitor) - } -} diff --git a/src/activity/kind/dislike.rs b/src/activity/kind/dislike.rs deleted file mode 100644 index 470f0ba..0000000 --- a/src/activity/kind/dislike.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct DislikeType; - -impl Serialize for DislikeType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Dislike") - } -} - -pub struct DislikeTypeVisitor; - -impl<'de> Visitor<'de> for DislikeTypeVisitor { - type Value = DislikeType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Dislike'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Dislike" { - Ok(DislikeType) - } else { - Err(de::Error::custom("Type not Dislike")) - } - } -} - -impl<'de> Deserialize<'de> for DislikeType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(DislikeTypeVisitor) - } -} diff --git a/src/activity/kind/flag.rs b/src/activity/kind/flag.rs deleted file mode 100644 index 2a05e98..0000000 --- a/src/activity/kind/flag.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct FlagType; - -impl Serialize for FlagType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Flag") - } -} - -pub struct FlagTypeVisitor; - -impl<'de> Visitor<'de> for FlagTypeVisitor { - type Value = FlagType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Flag'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Flag" { - Ok(FlagType) - } else { - Err(de::Error::custom("Type not Flag")) - } - } -} - -impl<'de> Deserialize<'de> for FlagType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(FlagTypeVisitor) - } -} diff --git a/src/activity/kind/follow.rs b/src/activity/kind/follow.rs deleted file mode 100644 index 34f9a48..0000000 --- a/src/activity/kind/follow.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct FollowType; - -impl Serialize for FollowType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Follow") - } -} - -pub struct FollowTypeVisitor; - -impl<'de> Visitor<'de> for FollowTypeVisitor { - type Value = FollowType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Follow'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Follow" { - Ok(FollowType) - } else { - Err(de::Error::custom("Type not Follow")) - } - } -} - -impl<'de> Deserialize<'de> for FollowType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(FollowTypeVisitor) - } -} diff --git a/src/activity/kind/ignore.rs b/src/activity/kind/ignore.rs deleted file mode 100644 index 64d1849..0000000 --- a/src/activity/kind/ignore.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct IgnoreType; - -impl Serialize for IgnoreType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Ignore") - } -} - -pub struct IgnoreTypeVisitor; - -impl<'de> Visitor<'de> for IgnoreTypeVisitor { - type Value = IgnoreType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Ignore'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Ignore" { - Ok(IgnoreType) - } else { - Err(de::Error::custom("Type not Ignore")) - } - } -} - -impl<'de> Deserialize<'de> for IgnoreType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(IgnoreTypeVisitor) - } -} diff --git a/src/activity/kind/invite.rs b/src/activity/kind/invite.rs deleted file mode 100644 index ff63e30..0000000 --- a/src/activity/kind/invite.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct InviteType; - -impl Serialize for InviteType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Invite") - } -} - -pub struct InviteTypeVisitor; - -impl<'de> Visitor<'de> for InviteTypeVisitor { - type Value = InviteType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Invite'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Invite" { - Ok(InviteType) - } else { - Err(de::Error::custom("Type not Invite")) - } - } -} - -impl<'de> Deserialize<'de> for InviteType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(InviteTypeVisitor) - } -} diff --git a/src/activity/kind/join.rs b/src/activity/kind/join.rs deleted file mode 100644 index dc75831..0000000 --- a/src/activity/kind/join.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct JoinType; - -impl Serialize for JoinType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Join") - } -} - -pub struct JoinTypeVisitor; - -impl<'de> Visitor<'de> for JoinTypeVisitor { - type Value = JoinType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Join'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Join" { - Ok(JoinType) - } else { - Err(de::Error::custom("Type not Join")) - } - } -} - -impl<'de> Deserialize<'de> for JoinType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(JoinTypeVisitor) - } -} diff --git a/src/activity/kind/leave.rs b/src/activity/kind/leave.rs deleted file mode 100644 index d263b94..0000000 --- a/src/activity/kind/leave.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct LeaveType; - -impl Serialize for LeaveType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Leave") - } -} - -pub struct LeaveTypeVisitor; - -impl<'de> Visitor<'de> for LeaveTypeVisitor { - type Value = LeaveType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Leave'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Leave" { - Ok(LeaveType) - } else { - Err(de::Error::custom("Type not Leave")) - } - } -} - -impl<'de> Deserialize<'de> for LeaveType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(LeaveTypeVisitor) - } -} diff --git a/src/activity/kind/like.rs b/src/activity/kind/like.rs deleted file mode 100644 index ceffc42..0000000 --- a/src/activity/kind/like.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct LikeType; - -impl Serialize for LikeType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Like") - } -} - -pub struct LikeTypeVisitor; - -impl<'de> Visitor<'de> for LikeTypeVisitor { - type Value = LikeType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Like'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Like" { - Ok(LikeType) - } else { - Err(de::Error::custom("Type not Like")) - } - } -} - -impl<'de> Deserialize<'de> for LikeType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(LikeTypeVisitor) - } -} diff --git a/src/activity/kind/listen.rs b/src/activity/kind/listen.rs deleted file mode 100644 index 34cff45..0000000 --- a/src/activity/kind/listen.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct ListenType; - -impl Serialize for ListenType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Listen") - } -} - -pub struct ListenTypeVisitor; - -impl<'de> Visitor<'de> for ListenTypeVisitor { - type Value = ListenType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Listen'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Listen" { - Ok(ListenType) - } else { - Err(de::Error::custom("Type not Listen")) - } - } -} - -impl<'de> Deserialize<'de> for ListenType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(ListenTypeVisitor) - } -} diff --git a/src/activity/kind/mod.rs b/src/activity/kind/mod.rs deleted file mode 100644 index fb73e64..0000000 --- a/src/activity/kind/mod.rs +++ /dev/null @@ -1,57 +0,0 @@ -mod accept; -mod add; -mod amove; -mod announce; -mod arrive; -mod block; -mod create; -mod delete; -mod dislike; -mod flag; -mod follow; -mod ignore; -mod invite; -mod join; -mod leave; -mod like; -mod listen; -mod offer; -mod question; -mod read; -mod reject; -mod remove; -mod tentative_accept; -mod tentative_reject; -mod travel; -mod undo; -mod update; -mod view; - -pub use self::accept::*; -pub use self::add::*; -pub use self::amove::*; -pub use self::announce::*; -pub use self::arrive::*; -pub use self::block::*; -pub use self::create::*; -pub use self::delete::*; -pub use self::dislike::*; -pub use self::flag::*; -pub use self::follow::*; -pub use self::ignore::*; -pub use self::invite::*; -pub use self::join::*; -pub use self::leave::*; -pub use self::like::*; -pub use self::listen::*; -pub use self::offer::*; -pub use self::question::*; -pub use self::read::*; -pub use self::reject::*; -pub use self::remove::*; -pub use self::tentative_accept::*; -pub use self::tentative_reject::*; -pub use self::travel::*; -pub use self::undo::*; -pub use self::update::*; -pub use self::view::*; diff --git a/src/activity/kind/offer.rs b/src/activity/kind/offer.rs deleted file mode 100644 index 4ab9380..0000000 --- a/src/activity/kind/offer.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct OfferType; - -impl Serialize for OfferType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Offer") - } -} - -pub struct OfferTypeVisitor; - -impl<'de> Visitor<'de> for OfferTypeVisitor { - type Value = OfferType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Offer'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Offer" { - Ok(OfferType) - } else { - Err(de::Error::custom("Type not Offer")) - } - } -} - -impl<'de> Deserialize<'de> for OfferType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(OfferTypeVisitor) - } -} diff --git a/src/activity/kind/question.rs b/src/activity/kind/question.rs deleted file mode 100644 index cd02265..0000000 --- a/src/activity/kind/question.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct QuestionType; - -impl Serialize for QuestionType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Question") - } -} - -pub struct QuestionTypeVisitor; - -impl<'de> Visitor<'de> for QuestionTypeVisitor { - type Value = QuestionType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Question'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Question" { - Ok(QuestionType) - } else { - Err(de::Error::custom("Type not Question")) - } - } -} - -impl<'de> Deserialize<'de> for QuestionType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(QuestionTypeVisitor) - } -} diff --git a/src/activity/kind/read.rs b/src/activity/kind/read.rs deleted file mode 100644 index 928339e..0000000 --- a/src/activity/kind/read.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct ReadType; - -impl Serialize for ReadType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Read") - } -} - -pub struct ReadTypeVisitor; - -impl<'de> Visitor<'de> for ReadTypeVisitor { - type Value = ReadType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Read'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Read" { - Ok(ReadType) - } else { - Err(de::Error::custom("Type not Read")) - } - } -} - -impl<'de> Deserialize<'de> for ReadType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(ReadTypeVisitor) - } -} diff --git a/src/activity/kind/reject.rs b/src/activity/kind/reject.rs deleted file mode 100644 index 3c8f130..0000000 --- a/src/activity/kind/reject.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct RejectType; - -impl Serialize for RejectType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Reject") - } -} - -pub struct RejectTypeVisitor; - -impl<'de> Visitor<'de> for RejectTypeVisitor { - type Value = RejectType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Reject'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Reject" { - Ok(RejectType) - } else { - Err(de::Error::custom("Type not Reject")) - } - } -} - -impl<'de> Deserialize<'de> for RejectType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(RejectTypeVisitor) - } -} diff --git a/src/activity/kind/remove.rs b/src/activity/kind/remove.rs deleted file mode 100644 index e54c751..0000000 --- a/src/activity/kind/remove.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct RemoveType; - -impl Serialize for RemoveType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Remove") - } -} - -pub struct RemoveTypeVisitor; - -impl<'de> Visitor<'de> for RemoveTypeVisitor { - type Value = RemoveType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Remove'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Remove" { - Ok(RemoveType) - } else { - Err(de::Error::custom("Type not Remove")) - } - } -} - -impl<'de> Deserialize<'de> for RemoveType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(RemoveTypeVisitor) - } -} diff --git a/src/activity/kind/tentative_accept.rs b/src/activity/kind/tentative_accept.rs deleted file mode 100644 index b52d196..0000000 --- a/src/activity/kind/tentative_accept.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct TentativeAcceptType; - -impl Serialize for TentativeAcceptType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("TentativeAccept") - } -} - -pub struct TentativeAcceptTypeVisitor; - -impl<'de> Visitor<'de> for TentativeAcceptTypeVisitor { - type Value = TentativeAcceptType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'TentativeAccept'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "TentativeAccept" { - Ok(TentativeAcceptType) - } else { - Err(de::Error::custom("Type not TentativeAccept")) - } - } -} - -impl<'de> Deserialize<'de> for TentativeAcceptType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(TentativeAcceptTypeVisitor) - } -} diff --git a/src/activity/kind/tentative_reject.rs b/src/activity/kind/tentative_reject.rs deleted file mode 100644 index 1991e66..0000000 --- a/src/activity/kind/tentative_reject.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct TentativeRejectType; - -impl Serialize for TentativeRejectType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("TentativeReject") - } -} - -pub struct TentativeRejectTypeVisitor; - -impl<'de> Visitor<'de> for TentativeRejectTypeVisitor { - type Value = TentativeRejectType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'TentativeReject'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "TentativeReject" { - Ok(TentativeRejectType) - } else { - Err(de::Error::custom("Type not TentativeReject")) - } - } -} - -impl<'de> Deserialize<'de> for TentativeRejectType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(TentativeRejectTypeVisitor) - } -} diff --git a/src/activity/kind/travel.rs b/src/activity/kind/travel.rs deleted file mode 100644 index 49dac3d..0000000 --- a/src/activity/kind/travel.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct TravelType; - -impl Serialize for TravelType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Travel") - } -} - -pub struct TravelTypeVisitor; - -impl<'de> Visitor<'de> for TravelTypeVisitor { - type Value = TravelType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Travel'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Travel" { - Ok(TravelType) - } else { - Err(de::Error::custom("Type not Travel")) - } - } -} - -impl<'de> Deserialize<'de> for TravelType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(TravelTypeVisitor) - } -} diff --git a/src/activity/kind/undo.rs b/src/activity/kind/undo.rs deleted file mode 100644 index 0cf8ac3..0000000 --- a/src/activity/kind/undo.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct UndoType; - -impl Serialize for UndoType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Undo") - } -} - -pub struct UndoTypeVisitor; - -impl<'de> Visitor<'de> for UndoTypeVisitor { - type Value = UndoType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Undo'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Undo" { - Ok(UndoType) - } else { - Err(de::Error::custom("Type not Undo")) - } - } -} - -impl<'de> Deserialize<'de> for UndoType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(UndoTypeVisitor) - } -} diff --git a/src/activity/kind/update.rs b/src/activity/kind/update.rs deleted file mode 100644 index 3bc0496..0000000 --- a/src/activity/kind/update.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct UpdateType; - -impl Serialize for UpdateType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Update") - } -} - -pub struct UpdateTypeVisitor; - -impl<'de> Visitor<'de> for UpdateTypeVisitor { - type Value = UpdateType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Update'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Update" { - Ok(UpdateType) - } else { - Err(de::Error::custom("Type not update")) - } - } -} - -impl<'de> Deserialize<'de> for UpdateType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(UpdateTypeVisitor) - } -} diff --git a/src/activity/kind/view.rs b/src/activity/kind/view.rs deleted file mode 100644 index ea303ab..0000000 --- a/src/activity/kind/view.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct ViewType; - -impl Serialize for ViewType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("View") - } -} - -pub struct ViewTypeVisitor; - -impl<'de> Visitor<'de> for ViewTypeVisitor { - type Value = ViewType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'View'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "View" { - Ok(ViewType) - } else { - Err(de::Error::custom("Type not View")) - } - } -} - -impl<'de> Deserialize<'de> for ViewType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(ViewTypeVisitor) - } -} diff --git a/src/activity/leave.rs b/src/activity/leave.rs index 86dd578..202fd6c 100644 --- a/src/activity/leave.rs +++ b/src/activity/leave.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::LeaveType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Leave { #[serde(rename = "type")] kind: LeaveType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Leave { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Leave {} impl Activity for Leave {} diff --git a/src/activity/like.rs b/src/activity/like.rs index 207d3fc..582cf6f 100644 --- a/src/activity/like.rs +++ b/src/activity/like.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::LikeType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Like { #[serde(rename = "type")] kind: LikeType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Like { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Like {} impl Activity for Like {} diff --git a/src/activity/listen.rs b/src/activity/listen.rs index 9aab7a7..36378aa 100644 --- a/src/activity/listen.rs +++ b/src/activity/listen.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::ListenType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Listen { #[serde(rename = "type")] kind: ListenType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Listen { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Listen {} impl Activity for Listen {} diff --git a/src/activity/offer.rs b/src/activity/offer.rs index 2ac106d..9c964d9 100644 --- a/src/activity/offer.rs +++ b/src/activity/offer.rs @@ -2,69 +2,33 @@ use serde_json; use super::{kind::OfferType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Offer { #[serde(rename = "type")] kind: OfferType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] target: Option, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Properties for Offer {} - -impl Offer { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn target(&self) -> Result { - self.get_item(|d| &d.target) - } - - pub fn targets(&self) -> Result { - self.get_item(|d| &d.target) - } - - pub fn target_link(&self) -> Result { - self.get_item(|d| &d.target) - } - - pub fn target_links(&self) -> Result> { - self.get_item(|d| &d.target) - } -} - impl Object for Offer {} impl Activity for Offer {} diff --git a/src/activity/properties.rs b/src/activity/properties.rs index de5a2ca..5ac4dfa 100644 --- a/src/activity/properties.rs +++ b/src/activity/properties.rs @@ -5,47 +5,14 @@ use link::Link; use object::Object; use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct ActivityProperties { #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] result: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] instrument: Option, } - -impl Properties for ActivityProperties {} - -impl ActivityProperties { - pub fn result(&self) -> Result { - self.get_item(|ap| &ap.result) - } - - pub fn results(&self) -> Result> { - self.get_item(|ap| &ap.result) - } - - pub fn result_link(&self) -> Result { - self.get_item(|ap| &ap.result) - } - - pub fn result_links(&self) -> Result> { - self.get_item(|ap| &ap.result) - } - - pub fn instrument(&self) -> Result { - self.get_item(|ap| &ap.instrument) - } - - pub fn instruments(&self) -> Result> { - self.get_item(|ap| &ap.instrument) - } - - pub fn instrument_link(&self) -> Result { - self.get_item(|ap| &ap.instrument) - } - - pub fn instrument_links(&self) -> Result> { - self.get_item(|ap| &ap.instrument) - } -} diff --git a/src/activity/question.rs b/src/activity/question.rs index 5bd9b5a..3ae07d9 100644 --- a/src/activity/question.rs +++ b/src/activity/question.rs @@ -1,66 +1,33 @@ -use serde::de::DeserializeOwned; use serde_json; use super::{kind::QuestionType, properties::ActivityProperties, Activity, IntransitiveActivity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Question { #[serde(rename = "type")] kind: QuestionType, - #[serde(skip_serializing_if = "Option::is_none")] - one_of: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - any_of: Option>, + + #[serde(skip_serializing_if = "Vec::is_empty", default = "Vec::new")] + #[activitystreams(ab(Object, Link))] + one_of: Vec, + + #[serde(skip_serializing_if = "Vec::is_empty", default = "Vec::new")] + #[activitystreams(ab(Object, Link))] + any_of: Vec, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Properties for Question {} - -impl Question { - pub fn one_of(&self) -> Result> { - vec_item(&self.one_of) - } - - pub fn one_of_link(&self) -> Result> { - vec_item(&self.one_of) - } - - pub fn any_of(&self) -> Result> { - vec_item(&self.any_of) - } - - pub fn any_of_link(&self) -> Result> { - vec_item(&self.any_of) - } -} -fn vec_item(v: &Option>) -> Result> { - if let Some(v) = v.clone() { - v.into_iter() - .map(|value| serde_json::from_value(value)) - .fold(Ok(Vec::new()), |acc, item| match acc { - Ok(mut v) => match item { - Ok(item) => { - v.push(item); - Ok(v) - } - Err(_) => Err(Error::Deserialize), - }, - Err(e) => Err(e), - }) - } else { - Err(Error::NotFound) - } -} - impl Object for Question {} impl Activity for Question {} impl IntransitiveActivity for Question {} diff --git a/src/activity/read.rs b/src/activity/read.rs index 4aaa9e0..987d62d 100644 --- a/src/activity/read.rs +++ b/src/activity/read.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::ReadType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Read { #[serde(rename = "type")] kind: ReadType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Read { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Read {} impl Activity for Read {} diff --git a/src/activity/reject.rs b/src/activity/reject.rs index 272541a..2c998fb 100644 --- a/src/activity/reject.rs +++ b/src/activity/reject.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::RejectType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Reject { #[serde(rename = "type")] kind: RejectType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Reject { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Reject {} impl Activity for Reject {} diff --git a/src/activity/remove.rs b/src/activity/remove.rs index 9982699..a38c45e 100644 --- a/src/activity/remove.rs +++ b/src/activity/remove.rs @@ -2,87 +2,37 @@ use serde_json; use super::{kind::RemoveType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Remove { #[serde(rename = "type")] kind: RemoveType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] origin: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] target: Option, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Properties for Remove {} - -impl Remove { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn origin(&self) -> Result { - self.get_item(|d| &d.origin) - } - - pub fn origins(&self) -> Result { - self.get_item(|d| &d.origin) - } - - pub fn origin_link(&self) -> Result { - self.get_item(|d| &d.origin) - } - - pub fn origin_links(&self) -> Result> { - self.get_item(|d| &d.origin) - } - - pub fn target(&self) -> Result { - self.get_item(|d| &d.target) - } - - pub fn targets(&self) -> Result { - self.get_item(|d| &d.target) - } - - pub fn target_link(&self) -> Result { - self.get_item(|d| &d.target) - } - - pub fn target_links(&self) -> Result> { - self.get_item(|d| &d.target) - } -} - impl Object for Remove {} impl Activity for Remove {} diff --git a/src/activity/tentative_accept.rs b/src/activity/tentative_accept.rs index 05e0a94..f7414a8 100644 --- a/src/activity/tentative_accept.rs +++ b/src/activity/tentative_accept.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::TentativeAcceptType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct TentativeAccept { #[serde(rename = "type")] kind: TentativeAcceptType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl TentativeAccept { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for TentativeAccept {} impl Activity for TentativeAccept {} diff --git a/src/activity/tentative_reject.rs b/src/activity/tentative_reject.rs index 29bfebd..8afe6f2 100644 --- a/src/activity/tentative_reject.rs +++ b/src/activity/tentative_reject.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::TentativeRejectType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct TentativeReject { #[serde(rename = "type")] kind: TentativeRejectType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl TentativeReject { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for TentativeReject {} impl Activity for TentativeReject {} diff --git a/src/activity/travel.rs b/src/activity/travel.rs index c00929f..a9e1244 100644 --- a/src/activity/travel.rs +++ b/src/activity/travel.rs @@ -2,79 +2,35 @@ use serde_json; use super::{kind::TravelType, properties::ActivityProperties, Activity, IntransitiveActivity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Travel { #[serde(rename = "type")] kind: TravelType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] origin: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] target: Option, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Properties for Travel {} - -impl Travel { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn origin(&self) -> Result { - self.get_item(|d| &d.origin) - } - - pub fn origins(&self) -> Result { - self.get_item(|d| &d.origin) - } - - pub fn origin_link(&self) -> Result { - self.get_item(|d| &d.origin) - } - - pub fn origin_links(&self) -> Result> { - self.get_item(|d| &d.origin) - } - - pub fn target(&self) -> Result { - self.get_item(|d| &d.target) - } - - pub fn targets(&self) -> Result { - self.get_item(|d| &d.target) - } - - pub fn target_link(&self) -> Result { - self.get_item(|d| &d.target) - } - - pub fn target_links(&self) -> Result> { - self.get_item(|d| &d.target) - } -} - impl Object for Travel {} impl Activity for Travel {} impl IntransitiveActivity for Travel {} diff --git a/src/activity/undo.rs b/src/activity/undo.rs index 7c4f969..6f55490 100644 --- a/src/activity/undo.rs +++ b/src/activity/undo.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::UndoType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Undo { #[serde(rename = "type")] kind: UndoType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Undo { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Undo {} impl Activity for Undo {} diff --git a/src/activity/update.rs b/src/activity/update.rs index 01317a6..9dcc8d1 100644 --- a/src/activity/update.rs +++ b/src/activity/update.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::UpdateType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Update { #[serde(rename = "type")] kind: UpdateType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl Update { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Update {} impl Activity for Update {} diff --git a/src/activity/view.rs b/src/activity/view.rs index 5d40b18..db25671 100644 --- a/src/activity/view.rs +++ b/src/activity/view.rs @@ -2,48 +2,29 @@ use serde_json; use super::{kind::ViewType, properties::ActivityProperties, Activity}; -use error::{Error, Result}; +use error::Result; use link::Link; use object::{Object, ObjectProperties}; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct View { #[serde(rename = "type")] kind: ViewType, + + #[activitystreams(ab(Object, Link))] actor: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub activity_props: ActivityProperties, } -impl View { - pub fn actor(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actors(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_link(&self) -> Result { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn actor_links(&self) -> Result> { - serde_json::from_value(self.actor.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn objects(&self) -> Result> { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for View {} impl Activity for View {} diff --git a/src/actor/kind.rs b/src/actor/kind.rs new file mode 100644 index 0000000..e6ae003 --- /dev/null +++ b/src/actor/kind.rs @@ -0,0 +1,25 @@ +use std::fmt; + +use serde::{ + de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, +}; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Application)] +pub struct ApplicationType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Group)] +pub struct GroupType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Organization)] +pub struct OrganizationType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Person)] +pub struct PersonType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Service)] +pub struct ServiceType; diff --git a/src/actor/kind/application.rs b/src/actor/kind/application.rs deleted file mode 100644 index 0a317ad..0000000 --- a/src/actor/kind/application.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct ApplicationType; - -impl Serialize for ApplicationType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Application") - } -} - -pub struct ApplicationTypeVisitor; - -impl<'de> Visitor<'de> for ApplicationTypeVisitor { - type Value = ApplicationType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Application'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Application" { - Ok(ApplicationType) - } else { - Err(de::Error::custom("Type not Application")) - } - } -} - -impl<'de> Deserialize<'de> for ApplicationType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(ApplicationTypeVisitor) - } -} diff --git a/src/actor/kind/group.rs b/src/actor/kind/group.rs deleted file mode 100644 index 7dc73a0..0000000 --- a/src/actor/kind/group.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct GroupType; - -impl Serialize for GroupType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Group") - } -} - -pub struct GroupTypeVisitor; - -impl<'de> Visitor<'de> for GroupTypeVisitor { - type Value = GroupType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Group'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Group" { - Ok(GroupType) - } else { - Err(de::Error::custom("Type not Group")) - } - } -} - -impl<'de> Deserialize<'de> for GroupType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(GroupTypeVisitor) - } -} diff --git a/src/actor/kind/mod.rs b/src/actor/kind/mod.rs deleted file mode 100644 index 0e8c2e4..0000000 --- a/src/actor/kind/mod.rs +++ /dev/null @@ -1,11 +0,0 @@ -mod application; -mod group; -mod organization; -mod person; -mod service; - -pub use self::application::*; -pub use self::group::*; -pub use self::organization::*; -pub use self::person::*; -pub use self::service::*; diff --git a/src/actor/kind/organization.rs b/src/actor/kind/organization.rs deleted file mode 100644 index dc04a61..0000000 --- a/src/actor/kind/organization.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct OrganizationType; - -impl Serialize for OrganizationType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Organization") - } -} - -pub struct OrganizationTypeVisitor; - -impl<'de> Visitor<'de> for OrganizationTypeVisitor { - type Value = OrganizationType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Organization'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Organization" { - Ok(OrganizationType) - } else { - Err(de::Error::custom("Type not Organization")) - } - } -} - -impl<'de> Deserialize<'de> for OrganizationType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(OrganizationTypeVisitor) - } -} diff --git a/src/actor/kind/person.rs b/src/actor/kind/person.rs deleted file mode 100644 index 2ffe77e..0000000 --- a/src/actor/kind/person.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct PersonType; - -impl Serialize for PersonType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Person") - } -} - -pub struct PersonTypeVisitor; - -impl<'de> Visitor<'de> for PersonTypeVisitor { - type Value = PersonType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Person'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Person" { - Ok(PersonType) - } else { - Err(de::Error::custom("Type not Person")) - } - } -} - -impl<'de> Deserialize<'de> for PersonType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(PersonTypeVisitor) - } -} diff --git a/src/actor/kind/service.rs b/src/actor/kind/service.rs deleted file mode 100644 index 152cfcd..0000000 --- a/src/actor/kind/service.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct ServiceType; - -impl Serialize for ServiceType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Service") - } -} - -pub struct ServiceTypeVisitor; - -impl<'de> Visitor<'de> for ServiceTypeVisitor { - type Value = ServiceType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Service'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Service" { - Ok(ServiceType) - } else { - Err(de::Error::custom("Type not Service")) - } - } -} - -impl<'de> Deserialize<'de> for ServiceType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(ServiceTypeVisitor) - } -} diff --git a/src/actor/mod.rs b/src/actor/mod.rs index a5e26a3..ac03733 100644 --- a/src/actor/mod.rs +++ b/src/actor/mod.rs @@ -10,6 +10,7 @@ pub trait Actor: Object {} pub struct Appliation { #[serde(rename = "type")] kind: ApplicationType, + #[serde(flatten)] pub object_props: ObjectProperties, } @@ -21,6 +22,7 @@ impl Object for Appliation {} pub struct Group { #[serde(rename = "type")] kind: GroupType, + #[serde(flatten)] pub object_props: ObjectProperties, } @@ -32,6 +34,7 @@ impl Object for Group {} pub struct Organization { #[serde(rename = "type")] kind: OrganizationType, + #[serde(flatten)] pub object_props: ObjectProperties, } @@ -43,6 +46,7 @@ impl Object for Organization {} pub struct Person { #[serde(rename = "type")] kind: PersonType, + #[serde(flatten)] pub object_props: ObjectProperties, } @@ -54,6 +58,7 @@ impl Object for Person {} pub struct Service { #[serde(rename = "type")] kind: ServiceType, + #[serde(flatten)] pub object_props: ObjectProperties, } diff --git a/src/base.rs b/src/base.rs deleted file mode 100644 index f19f5ae..0000000 --- a/src/base.rs +++ /dev/null @@ -1,3 +0,0 @@ -use serde::{de::DeserializeOwned, ser::Serialize}; - -pub trait Base: Serialize + DeserializeOwned {} diff --git a/src/collection/kind.rs b/src/collection/kind.rs new file mode 100644 index 0000000..d9c566d --- /dev/null +++ b/src/collection/kind.rs @@ -0,0 +1,21 @@ +use std::fmt; + +use serde::{ + de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, +}; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Collection)] +pub struct CollectionType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(CollectionPage)] +pub struct CollectionPageType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(OrderedCollection)] +pub struct OrderedCollectionType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(OrderedCollectionPage)] +pub struct OrderedCollectionPageType; diff --git a/src/collection/kind/collection.rs b/src/collection/kind/collection.rs deleted file mode 100644 index 62f3fef..0000000 --- a/src/collection/kind/collection.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct CollectionType; - -impl Serialize for CollectionType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Collection") - } -} - -pub struct CollectionTypeVisitor; - -impl<'de> Visitor<'de> for CollectionTypeVisitor { - type Value = CollectionType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Collection'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Collection" { - Ok(CollectionType) - } else { - Err(de::Error::custom("Type not Collection")) - } - } -} - -impl<'de> Deserialize<'de> for CollectionType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(CollectionTypeVisitor) - } -} diff --git a/src/collection/kind/collection_page.rs b/src/collection/kind/collection_page.rs deleted file mode 100644 index 11c243a..0000000 --- a/src/collection/kind/collection_page.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct CollectionPageType; - -impl Serialize for CollectionPageType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("CollectionPage") - } -} - -pub struct CollectionPageTypeVisitor; - -impl<'de> Visitor<'de> for CollectionPageTypeVisitor { - type Value = CollectionPageType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'CollectionPage'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "CollectionPage" { - Ok(CollectionPageType) - } else { - Err(de::Error::custom("Type not CollectionPage")) - } - } -} - -impl<'de> Deserialize<'de> for CollectionPageType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(CollectionPageTypeVisitor) - } -} diff --git a/src/collection/kind/mod.rs b/src/collection/kind/mod.rs deleted file mode 100644 index bfcc7c2..0000000 --- a/src/collection/kind/mod.rs +++ /dev/null @@ -1,9 +0,0 @@ -mod collection; -mod collection_page; -mod ordered_collection; -mod ordered_collection_page; - -pub use self::collection::*; -pub use self::collection_page::*; -pub use self::ordered_collection::*; -pub use self::ordered_collection_page::*; diff --git a/src/collection/kind/ordered_collection.rs b/src/collection/kind/ordered_collection.rs deleted file mode 100644 index ab0fcdc..0000000 --- a/src/collection/kind/ordered_collection.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct OrderedCollectionType; - -impl Serialize for OrderedCollectionType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("OrderedCollection") - } -} - -pub struct OrderedCollectionTypeVisitor; - -impl<'de> Visitor<'de> for OrderedCollectionTypeVisitor { - type Value = OrderedCollectionType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'OrderedCollection'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "OrderedCollection" { - Ok(OrderedCollectionType) - } else { - Err(de::Error::custom("Type not OrderedCollection")) - } - } -} - -impl<'de> Deserialize<'de> for OrderedCollectionType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(OrderedCollectionTypeVisitor) - } -} diff --git a/src/collection/kind/ordered_collection_page.rs b/src/collection/kind/ordered_collection_page.rs deleted file mode 100644 index f873626..0000000 --- a/src/collection/kind/ordered_collection_page.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct OrderedCollectionPageType; - -impl Serialize for OrderedCollectionPageType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("OrderedCollectionPage") - } -} - -pub struct OrderedCollectionPageTypeVisitor; - -impl<'de> Visitor<'de> for OrderedCollectionPageTypeVisitor { - type Value = OrderedCollectionPageType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'OrderedCollectionPage'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "OrderedCollectionPage" { - Ok(OrderedCollectionPageType) - } else { - Err(de::Error::custom("Type not OrderedCollectionPage")) - } - } -} - -impl<'de> Deserialize<'de> for OrderedCollectionPageType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(OrderedCollectionPageTypeVisitor) - } -} diff --git a/src/collection/mod.rs b/src/collection/mod.rs index b6d774d..b3f4015 100644 --- a/src/collection/mod.rs +++ b/src/collection/mod.rs @@ -1,68 +1,78 @@ use serde_json; +use error::Result; +use link::Link; use object::Object; mod kind; mod properties; pub use self::kind::*; pub use self::properties::*; +use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Collection { - #[serde(rename = "@context")] - context: serde_json::Value, #[serde(rename = "type")] kind: CollectionType, + + #[activitystreams(ab(Object, Link))] items: Vec, + #[serde(flatten)] pub collection_props: CollectionProperties, } impl Object for Collection {} -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct OrderedCollection { - #[serde(rename = "@context")] - context: serde_json::Value, #[serde(rename = "type")] kind: OrderedCollectionType, + + #[activitystreams(ab(Object, Link))] items: Vec, + #[serde(flatten)] pub collection_props: CollectionProperties, } impl Object for OrderedCollection {} -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct CollectionPage { - #[serde(rename = "@context")] - context: serde_json::Value, #[serde(rename = "type")] kind: CollectionPageType, + + #[activitystreams(ab(Object, Link))] items: Vec, + #[serde(flatten)] pub collection_props: CollectionProperties, + #[serde(flatten)] pub collection_page_props: CollectionPageProperties, } impl Object for CollectionPage {} -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct OrderedCollectionPage { - #[serde(rename = "@context")] - context: serde_json::Value, #[serde(rename = "type")] kind: OrderedCollectionPageType, + + #[activitystreams(ab(Object, Link))] items: Vec, + #[serde(flatten)] pub collection_props: CollectionProperties, + #[serde(flatten)] pub collection_page_props: CollectionPageProperties, + #[serde(flatten)] pub ordered_collection_page_props: OrderedCollectionPageProperties, } diff --git a/src/collection/properties.rs b/src/collection/properties.rs index 83bd48a..6f9730a 100644 --- a/src/collection/properties.rs +++ b/src/collection/properties.rs @@ -5,101 +5,46 @@ use error::Result; use link::Link; use Properties; -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct CollectionProperties { #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(u64), functional)] total_items: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(CollectionPage), ab(Link), functional)] current: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(CollectionPage), ab(Link), functional)] first: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(CollectionPage), ab(Link), functional)] last: Option, } -impl Properties for CollectionProperties {} - -impl CollectionProperties { - pub fn total_items(&self) -> Result { - self.get_item(|c| &c.total_items) - } - - pub fn current(&self) -> Result { - self.get_item(|c| &c.current) - } - - pub fn current_link(&self) -> Result { - self.get_item(|c| &c.current) - } - - pub fn first(&self) -> Result { - self.get_item(|c| &c.first) - } - - pub fn first_link(&self) -> Result { - self.get_item(|c| &c.first) - } - - pub fn last(&self) -> Result { - self.get_item(|c| &c.last) - } - - pub fn last_link(&self) -> Result { - self.get_item(|c| &c.last) - } -} - -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct CollectionPageProperties { #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(Collection), ab(Link), functional)] part_of: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(CollectionPage), ab(Link), functional)] next: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(CollectionPage), ab(Link), functional)] prev: Option, } -impl Properties for CollectionPageProperties {} - -impl CollectionPageProperties { - pub fn part_of(&self) -> Result { - self.get_item(|c| &c.part_of) - } - - pub fn part_of_link(&self) -> Result { - self.get_item(|c| &c.part_of) - } - - pub fn next(&self) -> Result { - self.get_item(|c| &c.next) - } - - pub fn next_link(&self) -> Result { - self.get_item(|c| &c.next) - } - - pub fn prev(&self) -> Result { - self.get_item(|c| &c.prev) - } - - pub fn prev_link(&self) -> Result { - self.get_item(|c| &c.prev) - } -} - -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Properties)] #[serde(rename_all = "camelCase")] pub struct OrderedCollectionPageProperties { #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(u64), functional)] start_index: Option, } - -impl Properties for OrderedCollectionPageProperties {} - -impl OrderedCollectionPageProperties { - pub fn start_index(&self) -> Result { - self.get_item(|c| &c.start_index) - } -} diff --git a/src/lib.rs b/src/lib.rs index aa5031b..d1425d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,6 @@ #[macro_use] +extern crate activitystreams_derive; +#[macro_use] extern crate failure; extern crate mime; extern crate serde; @@ -14,6 +16,14 @@ pub fn context() -> serde_json::Value { } pub trait Properties { + fn get_value(&self, f: F) -> error::Result + where + F: FnOnce(&Self) -> &serde_json::Value, + I: serde::de::DeserializeOwned, + { + serde_json::from_value(f(self).clone()).map_err(|_| error::Error::Deserialize) + } + fn get_item(&self, f: F) -> error::Result where F: FnOnce(&Self) -> &Option, @@ -25,11 +35,29 @@ pub trait Properties { Err(error::Error::NotFound) } } + + fn get_vec(&self, f: F) -> error::Result> + where + F: FnOnce(&Self) -> &Vec, + I: serde::de::DeserializeOwned, + { + let item = f(self); + + item.iter().fold(Ok(Vec::new()), |acc, item| match acc { + Ok(mut acc) => match serde_json::from_value(item.clone()) { + Ok(item) => { + acc.push(item); + Ok(acc) + } + Err(_) => Err(error::Error::Deserialize), + }, + Err(e) => Err(e), + }) + } } pub mod activity; pub mod actor; -pub mod base; pub mod collection; pub mod error; pub mod link; diff --git a/src/link/kind.rs b/src/link/kind.rs index 34d3e7c..2cf0be1 100644 --- a/src/link/kind.rs +++ b/src/link/kind.rs @@ -4,44 +4,6 @@ use serde::{ de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, }; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Mention)] pub struct MentionType; - -impl Serialize for MentionType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Mention") - } -} - -pub struct MentionTypeVisitor; - -impl<'de> Visitor<'de> for MentionTypeVisitor { - type Value = MentionType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Mention'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Mention" { - Ok(MentionType) - } else { - Err(de::Error::custom("Type not Mention")) - } - } -} - -impl<'de> Deserialize<'de> for MentionType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(MentionTypeVisitor) - } -} diff --git a/src/link/mod.rs b/src/link/mod.rs index 0d34ade..2bc60b9 100644 --- a/src/link/mod.rs +++ b/src/link/mod.rs @@ -12,6 +12,7 @@ pub trait Link: DeserializeOwned + Serialize {} pub struct Mention { #[serde(rename = "type")] kind: MentionType, + #[serde(flatten)] pub link_props: LinkProperties, } diff --git a/src/link/properties.rs b/src/link/properties.rs index 960c0ba..547da0c 100644 --- a/src/link/properties.rs +++ b/src/link/properties.rs @@ -6,70 +6,50 @@ use link::Link; use object::Object; use Properties; -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Properties)] #[serde(rename_all = "camelCase")] pub struct LinkProperties { + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String), functional)] id: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String), functional)] href: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String))] rel: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String), functional)] media_type: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String))] name: Option, + + // TODO: Lang enum + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String), functional)] hreflang: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(u64), functional)] height: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(u64), functional)] width: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] preview: Option, } -impl Properties for LinkProperties {} - impl LinkProperties { - pub fn id(&self) -> Result { - self.get_item(|l| &l.id) - } - - pub fn href(&self) -> Result { - self.get_item(|l| &l.href) - } - - pub fn rel(&self) -> Result { - self.get_item(|l| &l.rel) - } - - pub fn rels(&self) -> Result> { - self.get_item(|l| &l.rel) - } - pub fn media_type(&self) -> Result { - self.get_item::<_, String>(|l| &l.media_type) + self.media_type_string() .and_then(|s| s.parse().map_err(|_| Error::Deserialize)) } - - pub fn name(&self) -> Result { - self.get_item(|l| &l.name) - } - - pub fn names(&self) -> Result> { - self.get_item(|l| &l.name) - } - - // TODO: Lang enum - pub fn hreflang(&self) -> Result { - self.get_item(|l| &l.hreflang) - } - - pub fn height(&self) -> Result { - self.get_item(|l| &l.height) - } - - pub fn width(&self) -> Result { - self.get_item(|l| &l.width) - } - - pub fn preview(&self) -> Result { - self.get_item(|l| &l.preview) - } - - pub fn preview_link(&self) -> Result { - self.get_item(|l| &l.preview) - } } diff --git a/src/object/kind.rs b/src/object/kind.rs new file mode 100644 index 0000000..ffc0a5d --- /dev/null +++ b/src/object/kind.rs @@ -0,0 +1,53 @@ +use std::fmt; + +use serde::{ + de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, +}; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Article)] +pub struct ArticleType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Audio)] +pub struct AudioType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Document)] +pub struct DocumentType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Event)] +pub struct EventType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Image)] +pub struct ImageType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Note)] +pub struct NoteType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Page)] +pub struct PageType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Place)] +pub struct PlaceType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Profile)] +pub struct ProfileType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Relationship)] +pub struct RelationshipType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Tombstone)] +pub struct TombstoneType; + +#[derive(Clone, Debug, UnitString)] +#[activitystreams(Video)] +pub struct VideoType; diff --git a/src/object/kind/article.rs b/src/object/kind/article.rs deleted file mode 100644 index c414d16..0000000 --- a/src/object/kind/article.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct ArticleType; - -impl Serialize for ArticleType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Article") - } -} - -pub struct ArticleTypeVisitor; - -impl<'de> Visitor<'de> for ArticleTypeVisitor { - type Value = ArticleType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Article'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Article" { - Ok(ArticleType) - } else { - Err(de::Error::custom("Type not Article")) - } - } -} - -impl<'de> Deserialize<'de> for ArticleType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(ArticleTypeVisitor) - } -} diff --git a/src/object/kind/audio.rs b/src/object/kind/audio.rs deleted file mode 100644 index 03ecc65..0000000 --- a/src/object/kind/audio.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct AudioType; - -impl Serialize for AudioType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Audio") - } -} - -pub struct AudioTypeVisitor; - -impl<'de> Visitor<'de> for AudioTypeVisitor { - type Value = AudioType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Audio'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Audio" { - Ok(AudioType) - } else { - Err(de::Error::custom("Type not Audio")) - } - } -} - -impl<'de> Deserialize<'de> for AudioType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(AudioTypeVisitor) - } -} diff --git a/src/object/kind/document.rs b/src/object/kind/document.rs deleted file mode 100644 index 6b8fbc6..0000000 --- a/src/object/kind/document.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct DocumentType; - -impl Serialize for DocumentType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Document") - } -} - -pub struct DocumentTypeVisitor; - -impl<'de> Visitor<'de> for DocumentTypeVisitor { - type Value = DocumentType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Document'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Document" { - Ok(DocumentType) - } else { - Err(de::Error::custom("Type not Document")) - } - } -} - -impl<'de> Deserialize<'de> for DocumentType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(DocumentTypeVisitor) - } -} diff --git a/src/object/kind/event.rs b/src/object/kind/event.rs deleted file mode 100644 index f3c3700..0000000 --- a/src/object/kind/event.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct EventType; - -impl Serialize for EventType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Event") - } -} - -pub struct EventTypeVisitor; - -impl<'de> Visitor<'de> for EventTypeVisitor { - type Value = EventType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Event'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Event" { - Ok(EventType) - } else { - Err(de::Error::custom("Type not Event")) - } - } -} - -impl<'de> Deserialize<'de> for EventType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(EventTypeVisitor) - } -} diff --git a/src/object/kind/image.rs b/src/object/kind/image.rs deleted file mode 100644 index 9944483..0000000 --- a/src/object/kind/image.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct ImageType; - -impl Serialize for ImageType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Image") - } -} - -pub struct ImageTypeVisitor; - -impl<'de> Visitor<'de> for ImageTypeVisitor { - type Value = ImageType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Image'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Image" { - Ok(ImageType) - } else { - Err(de::Error::custom("Type not Image")) - } - } -} - -impl<'de> Deserialize<'de> for ImageType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(ImageTypeVisitor) - } -} diff --git a/src/object/kind/mod.rs b/src/object/kind/mod.rs deleted file mode 100644 index 06c9c8f..0000000 --- a/src/object/kind/mod.rs +++ /dev/null @@ -1,25 +0,0 @@ -mod article; -mod audio; -mod document; -mod event; -mod image; -mod note; -mod page; -mod place; -mod profile; -mod relationship; -mod tombstone; -mod video; - -pub use self::article::*; -pub use self::audio::*; -pub use self::document::*; -pub use self::event::*; -pub use self::image::*; -pub use self::note::*; -pub use self::page::*; -pub use self::place::*; -pub use self::profile::*; -pub use self::relationship::*; -pub use self::tombstone::*; -pub use self::video::*; diff --git a/src/object/kind/note.rs b/src/object/kind/note.rs deleted file mode 100644 index ae914c9..0000000 --- a/src/object/kind/note.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct NoteType; - -impl Serialize for NoteType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Note") - } -} - -pub struct NoteTypeVisitor; - -impl<'de> Visitor<'de> for NoteTypeVisitor { - type Value = NoteType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Note'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Note" { - Ok(NoteType) - } else { - Err(de::Error::custom("Type not Note")) - } - } -} - -impl<'de> Deserialize<'de> for NoteType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(NoteTypeVisitor) - } -} diff --git a/src/object/kind/page.rs b/src/object/kind/page.rs deleted file mode 100644 index 0304658..0000000 --- a/src/object/kind/page.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct PageType; - -impl Serialize for PageType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Page") - } -} - -pub struct PageTypeVisitor; - -impl<'de> Visitor<'de> for PageTypeVisitor { - type Value = PageType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Page'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Page" { - Ok(PageType) - } else { - Err(de::Error::custom("Type not Page")) - } - } -} - -impl<'de> Deserialize<'de> for PageType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(PageTypeVisitor) - } -} diff --git a/src/object/kind/place.rs b/src/object/kind/place.rs deleted file mode 100644 index 16338d6..0000000 --- a/src/object/kind/place.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct PlaceType; - -impl Serialize for PlaceType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Place") - } -} - -pub struct PlaceTypeVisitor; - -impl<'de> Visitor<'de> for PlaceTypeVisitor { - type Value = PlaceType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Place'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Place" { - Ok(PlaceType) - } else { - Err(de::Error::custom("Type not Place")) - } - } -} - -impl<'de> Deserialize<'de> for PlaceType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(PlaceTypeVisitor) - } -} diff --git a/src/object/kind/profile.rs b/src/object/kind/profile.rs deleted file mode 100644 index dade813..0000000 --- a/src/object/kind/profile.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct ProfileType; - -impl Serialize for ProfileType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Profile") - } -} - -pub struct ProfileTypeVisitor; - -impl<'de> Visitor<'de> for ProfileTypeVisitor { - type Value = ProfileType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Profile'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Profile" { - Ok(ProfileType) - } else { - Err(de::Error::custom("Type not Profile")) - } - } -} - -impl<'de> Deserialize<'de> for ProfileType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(ProfileTypeVisitor) - } -} diff --git a/src/object/kind/relationship.rs b/src/object/kind/relationship.rs deleted file mode 100644 index 18b8901..0000000 --- a/src/object/kind/relationship.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct RelationshipType; - -impl Serialize for RelationshipType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Relationship") - } -} - -pub struct RelationshipTypeVisitor; - -impl<'de> Visitor<'de> for RelationshipTypeVisitor { - type Value = RelationshipType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Relationship'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Relationship" { - Ok(RelationshipType) - } else { - Err(de::Error::custom("Type not Relationship")) - } - } -} - -impl<'de> Deserialize<'de> for RelationshipType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(RelationshipTypeVisitor) - } -} diff --git a/src/object/kind/tombstone.rs b/src/object/kind/tombstone.rs deleted file mode 100644 index cdbabbf..0000000 --- a/src/object/kind/tombstone.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct TombstoneType; - -impl Serialize for TombstoneType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Tombstone") - } -} - -pub struct TombstoneTypeVisitor; - -impl<'de> Visitor<'de> for TombstoneTypeVisitor { - type Value = TombstoneType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Tombstone'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Tombstone" { - Ok(TombstoneType) - } else { - Err(de::Error::custom("Type not Tombstone")) - } - } -} - -impl<'de> Deserialize<'de> for TombstoneType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(TombstoneTypeVisitor) - } -} diff --git a/src/object/kind/video.rs b/src/object/kind/video.rs deleted file mode 100644 index 01c5376..0000000 --- a/src/object/kind/video.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - -#[derive(Clone, Debug)] -pub struct VideoType; - -impl Serialize for VideoType { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str("Video") - } -} - -pub struct VideoTypeVisitor; - -impl<'de> Visitor<'de> for VideoTypeVisitor { - type Value = VideoType; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "The string 'Video'") - } - - fn visit_str(self, v: &str) -> Result - where - E: de::Error, - { - if v == "Video" { - Ok(VideoType) - } else { - Err(de::Error::custom("Type not Video")) - } - } -} - -impl<'de> Deserialize<'de> for VideoType { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - deserializer.deserialize_str(VideoTypeVisitor) - } -} diff --git a/src/object/mod.rs b/src/object/mod.rs index 8c0ad1c..ff665ef 100644 --- a/src/object/mod.rs +++ b/src/object/mod.rs @@ -1,8 +1,9 @@ use serde::{de::DeserializeOwned, ser::Serialize}; use serde_json; -use error::{Error, Result}; +use error::Result; use link::Link; +use Properties; mod kind; mod properties; @@ -16,6 +17,7 @@ pub trait Object: DeserializeOwned + Serialize {} pub struct Article { #[serde(rename = "type")] kind: ArticleType, + #[serde(flatten)] pub object_props: ObjectProperties, } @@ -27,6 +29,7 @@ impl Object for Article {} pub struct Audio { #[serde(rename = "type")] kind: AudioType, + #[serde(flatten)] pub object_props: ObjectProperties, } @@ -38,6 +41,7 @@ impl Object for Audio {} pub struct Document { #[serde(rename = "type")] kind: DocumentType, + #[serde(flatten)] pub object_props: ObjectProperties, } @@ -49,6 +53,7 @@ impl Object for Document {} pub struct Event { #[serde(rename = "type")] kind: EventType, + #[serde(flatten)] pub object_props: ObjectProperties, } @@ -60,6 +65,7 @@ impl Object for Event {} pub struct Image { #[serde(rename = "type")] kind: ImageType, + #[serde(flatten)] pub object_props: ObjectProperties, } @@ -71,6 +77,7 @@ impl Object for Image {} pub struct Note { #[serde(rename = "type")] kind: NoteType, + #[serde(flatten)] pub object_props: ObjectProperties, } @@ -82,6 +89,7 @@ impl Object for Note {} pub struct Page { #[serde(rename = "type")] kind: PageType, + #[serde(flatten)] pub object_props: ObjectProperties, } @@ -93,70 +101,50 @@ impl Object for Page {} pub struct Place { #[serde(rename = "type")] kind: PlaceType, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub place: PlaceProperties, } impl Object for Place {} -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Profile { #[serde(rename = "type")] kind: ProfileType, + + #[activitystreams(ab(Object), functional)] describes: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, } -impl Profile { - pub fn describes(&self) -> Result { - serde_json::from_value(self.describes.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Profile {} -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Properties)] #[serde(rename_all = "camelCase")] pub struct Relationship { #[serde(rename = "type")] kind: RelationshipType, + + #[activitystreams(ab(Object, Link))] subject: serde_json::Value, + + #[activitystreams(ab(Object, Link))] object: serde_json::Value, + + #[activitystreams(ab(Object))] relationship: serde_json::Value, + #[serde(flatten)] pub object_props: ObjectProperties, } -impl Relationship { - pub fn subject(&self) -> Result { - serde_json::from_value(self.subject.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn relationship(&self) -> Result { - serde_json::from_value(self.relationship.clone()).map_err(|_| Error::Deserialize) - } - - pub fn subject_link(&self) -> Result { - serde_json::from_value(self.subject.clone()).map_err(|_| Error::Deserialize) - } - - pub fn object_link(&self) -> Result { - serde_json::from_value(self.object.clone()).map_err(|_| Error::Deserialize) - } - - pub fn relationship_link(&self) -> Result { - serde_json::from_value(self.relationship.clone()).map_err(|_| Error::Deserialize) - } -} - impl Object for Relationship {} #[derive(Clone, Debug, Serialize, Deserialize)] @@ -164,8 +152,10 @@ impl Object for Relationship {} pub struct Tombstone { #[serde(rename = "type")] kind: TombstoneType, + #[serde(flatten)] pub object_props: ObjectProperties, + #[serde(flatten)] pub tombstone_props: TombstoneProperties, } @@ -177,6 +167,7 @@ impl Object for Tombstone {} pub struct Video { #[serde(rename = "type")] kind: VideoType, + #[serde(flatten)] pub object_props: ObjectProperties, } diff --git a/src/object/properties.rs b/src/object/properties.rs index a546b0a..78c4535 100644 --- a/src/object/properties.rs +++ b/src/object/properties.rs @@ -1,6 +1,5 @@ use mime; -use serde::de::DeserializeOwned; -use serde_json; +use serde_json::{self, Value}; use collection::Collection; use error::{Error, Result}; @@ -8,435 +7,164 @@ use link::Link; use object::{Image, Object}; use Properties; -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Properties)] #[serde(rename_all = "camelCase")] pub struct ObjectProperties { #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String), functional)] id: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] attachment: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] attributed_to: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] audience: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String))] content: Option, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(rename = "@context")] + + #[serde(skip_serializing_if = "Option::is_none", rename = "@context")] + #[activitystreams(concrete(Value), functional)] context: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String))] name: Option, + + // TODO: DateTime #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String), functional)] end_time: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] generator: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Link), concrete(Image))] icon: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Link), concrete(Image))] image: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] in_reply_to: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] location: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] preview: Option, + + // TODO: DateTime #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String), functional)] published: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(Collection), functional)] replies: Option, + + // TODO: DateTime #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String), functional)] start_time: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String))] summary: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] tag: Option, + + // TODO: DateTime #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String), functional)] updated: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String), ab(Link))] url: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] to: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] bto: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] cc: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(ab(Object, Link))] bcc: Option, + + // TODO: mime #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String), functional)] media_type: Option, + + // TODO: xsd:duration #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String), functional)] duration: Option, } -impl Properties for ObjectProperties {} - impl ObjectProperties { - pub fn id(&self) -> Result { - self.get_item(|props| &props.id) - } - - pub fn attachment(&self) -> Result { - self.get_item(|props| &props.attachment) - } - - pub fn attachments(&self) -> Result> { - self.get_item(|props| &props.attachment) - } - - pub fn attachment_link(&self) -> Result { - self.get_item(|props| &props.attachment) - } - - pub fn attachment_links(&self) -> Result> { - self.get_item(|props| &props.attachment) - } - - pub fn attributed_to(&self) -> Result { - self.get_item(|props| &props.attributed_to) - } - - pub fn attributed_tos(&self) -> Result> { - self.get_item(|props| &props.attributed_to) - } - - pub fn attributed_to_link(&self) -> Result { - self.get_item(|props| &props.attributed_to) - } - - pub fn attributed_to_links(&self) -> Result> { - self.get_item(|props| &props.attributed_to) - } - - pub fn audience(&self) -> Result { - self.get_item(|props| &props.audience) - } - - pub fn audiences(&self) -> Result> { - self.get_item(|props| &props.audience) - } - - pub fn audience_link(&self) -> Result { - self.get_item(|props| &props.audience) - } - - pub fn audience_links(&self) -> Result> { - self.get_item(|props| &props.audience) - } - - pub fn content(&self) -> Result { - self.get_item(|props| &props.content) - } - - pub fn contents(&self) -> Result> { - self.get_item(|props| &props.content) - } - - pub fn context(&self) -> Result { - self.context.clone().ok_or(Error::NotFound) - } - - pub fn name(&self) -> Result { - self.get_item(|props| &props.name) - } - - pub fn names(&self) -> Result> { - self.get_item(|props| &props.name) - } - - // TODO: DateTime - pub fn end_time(&self) -> Result { - self.get_item(|props| &props.end_time) - } - - pub fn generator(&self) -> Result { - self.get_item(|props| &props.generator) - } - - pub fn generators(&self) -> Result> { - self.get_item(|props| &props.generator) - } - - pub fn generator_link(&self) -> Result { - self.get_item(|props| &props.generator) - } - - pub fn generator_links(&self) -> Result> { - self.get_item(|props| &props.generator) - } - - pub fn icon(&self) -> Result { - self.get_item(|props| &props.icon) - } - - pub fn icons(&self) -> Result> { - self.get_item(|props| &props.icon) - } - - pub fn icon_link(&self) -> Result { - self.get_item(|props| &props.icon) - } - - pub fn icon_links(&self) -> Result> { - self.get_item(|props| &props.icon) - } - - pub fn image(&self) -> Result { - self.get_item(|props| &props.image) - } - - pub fn images(&self) -> Result> { - self.get_item(|props| &props.image) - } - - pub fn image_link(&self) -> Result { - self.get_item(|props| &props.image) - } - - pub fn image_links(&self) -> Result> { - self.get_item(|props| &props.image) - } - - pub fn in_reply_to(&self) -> Result { - self.get_item(|props| &props.in_reply_to) - } - - pub fn in_reply_tos(&self) -> Result> { - self.get_item(|props| &props.in_reply_to) - } - - pub fn in_reply_to_link(&self) -> Result { - self.get_item(|props| &props.in_reply_to) - } - - pub fn in_reply_to_links(&self) -> Result> { - self.get_item(|props| &props.in_reply_to) - } - - pub fn location(&self) -> Result { - self.get_item(|props| &props.location) - } - - pub fn locations(&self) -> Result> { - self.get_item(|props| &props.location) - } - - pub fn location_link(&self) -> Result { - self.get_item(|props| &props.location) - } - - pub fn location_links(&self) -> Result> { - self.get_item(|props| &props.location) - } - - pub fn preview(&self) -> Result { - self.get_item(|props| &props.preview) - } - - pub fn previews(&self) -> Result> { - self.get_item(|props| &props.preview) - } - - pub fn preview_link(&self) -> Result { - self.get_item(|props| &props.preview) - } - - pub fn preview_links(&self) -> Result> { - self.get_item(|props| &props.preview) - } - - // TODO: DateTime - pub fn published(&self) -> Result { - self.get_item(|props| &props.published) - } - - pub fn replies(&self) -> Result { - self.get_item(|props| &props.replies) - } - - // TODO: DateTime - pub fn start_time(&self) -> Result { - self.get_item(|props| &props.start_time) - } - - pub fn summary(&self) -> Result { - self.get_item(|props| &props.summary) - } - - pub fn summaries(&self) -> Result> { - self.get_item(|props| &props.summary) - } - - pub fn tag(&self) -> Result { - self.get_item(|props| &props.tag) - } - - pub fn tags(&self) -> Result> { - self.get_item(|props| &props.tag) - } - - pub fn tag_link(&self) -> Result { - self.get_item(|props| &props.tag) - } - - pub fn tag_links(&self) -> Result> { - self.get_item(|props| &props.tag) - } - - // TODO: DateTime - pub fn updated(&self) -> Result { - self.get_item(|props| &props.updated) - } - - pub fn url(&self) -> Result { - self.get_item(|props| &props.url) - } - - pub fn urls(&self) -> Result> { - self.get_item(|props| &props.url) - } - - pub fn url_link(&self) -> Result { - self.get_item(|props| &props.url) - } - - pub fn url_links(&self) -> Result> { - self.get_item(|props| &props.url) - } - - pub fn to(&self) -> Result { - self.get_item(|props| &props.to) - } - - pub fn tos(&self) -> Result> { - self.get_item(|props| &props.to) - } - - pub fn to_link(&self) -> Result { - self.get_item(|props| &props.to) - } - - pub fn to_links(&self) -> Result> { - self.get_item(|props| &props.to) - } - - pub fn bto(&self) -> Result { - self.get_item(|props| &props.bto) - } - - pub fn btos(&self) -> Result> { - self.get_item(|props| &props.bto) - } - - pub fn bto_link(&self) -> Result { - self.get_item(|props| &props.bto) - } - - pub fn bto_links(&self) -> Result> { - self.get_item(|props| &props.bto) - } - - pub fn cc(&self) -> Result { - self.get_item(|props| &props.cc) - } - - pub fn ccs(&self) -> Result> { - self.get_item(|props| &props.cc) - } - - pub fn cc_link(&self) -> Result { - self.get_item(|props| &props.cc) - } - - pub fn cc_links(&self) -> Result> { - self.get_item(|props| &props.cc) - } - - pub fn bcc(&self) -> Result { - self.get_item(|props| &props.bcc) - } - - pub fn bccs(&self) -> Result> { - self.get_item(|props| &props.bcc) - } - - pub fn bcc_link(&self) -> Result { - self.get_item(|props| &props.bcc) - } - - pub fn bcc_links(&self) -> Result> { - self.get_item(|props| &props.bcc) - } - pub fn media_type(&self) -> Result { - self.get_item::<_, String>(|props| &props.media_type) + self.media_type_string() .and_then(|s| s.parse().map_err(|_| Error::Deserialize)) } - - // TODO: xsd:duration - pub fn duration(&self) -> Result { - self.get_item(|props| &props.duration) - } } -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Properties)] #[serde(rename_all = "camelCase")] pub struct PlaceProperties { #[serde(skip_serializing_if = "Option::is_none")] - accuracy: Option, + #[activitystreams(concrete(f64), functional)] + accuracy: Option, + #[serde(skip_serializing_if = "Option::is_none")] - altitude: Option, + #[activitystreams(concrete(f64), functional)] + altitude: Option, + #[serde(skip_serializing_if = "Option::is_none")] - latitude: Option, + #[activitystreams(concrete(f64), functional)] + latitude: Option, + #[serde(skip_serializing_if = "Option::is_none")] - longitude: Option, + #[activitystreams(concrete(f64), functional)] + longitude: Option, + #[serde(skip_serializing_if = "Option::is_none")] - radius: Option, + #[activitystreams(concrete(f64), functional)] + radius: Option, } -impl PlaceProperties { - pub fn accuracy(&self) -> Result { - self.accuracy.ok_or(Error::NotFound) - } - - pub fn altitude(&self) -> Result { - self.altitude.ok_or(Error::NotFound) - } - - pub fn latitude(&self) -> Result { - self.latitude.ok_or(Error::NotFound) - } - - pub fn longitude(&self) -> Result { - self.longitude.ok_or(Error::NotFound) - } - - pub fn radius(&self) -> Result { - self.radius.ok_or(Error::NotFound) - } -} - -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Properties)] #[serde(rename_all = "camelCase")] pub struct TombstoneProperties { #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String))] former_type: Option, - #[serde(skip_serializing_if = "Option::is_none")] - deleted: Option, -} - -impl Properties for TombstoneProperties {} - -impl TombstoneProperties { - pub fn former_type(&self) -> Result { - self.get_item(|t| &t.former_type) - } - - pub fn former_types(&self) -> Result> { - self.get_item(|t| &t.former_type) - } // TODO: DateTime - pub fn deleted(&self) -> Result { - self.get_item(|t| &t.deleted) - } + #[serde(skip_serializing_if = "Option::is_none")] + #[activitystreams(concrete(String), functional)] + deleted: Option, }