diff --git a/activitystreams-derive/Cargo.toml b/activitystreams-derive/Cargo.toml index 78fd54b..fb49087 100644 --- a/activitystreams-derive/Cargo.toml +++ b/activitystreams-derive/Cargo.toml @@ -12,5 +12,11 @@ quote = "0.5" syn = "0.13" proc-macro2 = "0.3" +[dev-dependencies] +activitystreams-traits = { version = "0.1", path = "../activitystreams-traits" } +serde = "1.0" +serde_derive = "1.0" +serde_json = "1.0" + [lib] proc-macro = true diff --git a/activitystreams-derive/src/lib.rs b/activitystreams-derive/src/lib.rs index 171a696..4f4ca9c 100644 --- a/activitystreams-derive/src/lib.rs +++ b/activitystreams-derive/src/lib.rs @@ -17,6 +17,55 @@ * along with ActivityStreams Derive. If not, see . */ +//! Derive macros for Activity Streams +//! +//! ## Examples +//! +//! ```rust +//! #[macro_use] +//! extern crate activitystreams_derive; +//! extern crate activitystreams_traits; +//! extern crate serde; +//! #[macro_use] +//! extern crate serde_derive; +//! extern crate serde_json; +//! +//! use activitystreams_traits::{Link, Object}; +//! +//! /// Using the UnitString derive macro +//! /// +//! /// This macro implements Serialize and Deserialize for the given type, making this type +//! /// represent the string "SomeKind" in JSON. +//! #[derive(Clone, Debug, Default, UnitString)] +//! #[activitystreams(SomeKind)] +//! pub struct MyKind; +//! +//! /// Using the Properties derive macro +//! /// +//! /// This macro generates getters and setters for the associated fields. +//! #[derive(Clone, Debug, Default, Deserialize, Serialize, Properties)] +//! #[serde(rename_all = "camelCase")] +//! pub struct MyProperties { +//! /// Derive getters and setters for @context with Link and Object traits. +//! #[serde(rename = "@context")] +//! #[activitystreams(ab(Object, Link))] +//! pub context: Option, +//! +//! /// Derive getters and setters for context with Link and Object traits. +//! pub kind: MyKind, +//! +//! /// Derive getters and setters for required_key with String type. +//! /// +//! /// In the Activity Streams spec, 'functional' means there can only be one item for this +//! /// key. This means all fields not labeled 'functional' can also be serialized/deserialized +//! /// as Vec. +//! #[activitystreams(concrete(String), functional)] +//! pub required_key: serde_json::Value, +//! } +//! # +//! # fn main () {} +//! ``` + extern crate proc_macro; extern crate proc_macro2; extern crate syn; @@ -57,10 +106,10 @@ pub fn unit_string(input: TokenStream) -> TokenStream { let visitor_name = Ident::from(format!("{}Visitor", value)); let serialize = quote! { - impl Serialize for #name { + impl ::serde::ser::Serialize for #name { fn serialize(&self, serializer: S) -> Result where - S: Serializer, + S: ::serde::ser::Serializer, { serializer.serialize_str(#value) } @@ -68,7 +117,7 @@ pub fn unit_string(input: TokenStream) -> TokenStream { }; let expecting = quote! { - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { write!(formatter, "The string '{}'", #value) } }; @@ -76,12 +125,12 @@ pub fn unit_string(input: TokenStream) -> TokenStream { let visit = quote! { fn visit_str(self, v: &str) -> Result where - E: de::Error, + E: ::serde::de::Error, { if v == #value { Ok(#name) } else { - Err(de::Error::custom("Invalid type")) + Err(::serde::de::Error::custom("Invalid type")) } } }; @@ -89,7 +138,7 @@ pub fn unit_string(input: TokenStream) -> TokenStream { let visitor = quote! { struct #visitor_name; - impl<'de> Visitor<'de> for #visitor_name { + impl<'de> ::serde::de::Visitor<'de> for #visitor_name { type Value = #name; #expecting @@ -99,10 +148,10 @@ pub fn unit_string(input: TokenStream) -> TokenStream { }; let deserialize = quote! { - impl<'de> Deserialize<'de> for #name { + impl<'de> ::serde::de::Deserialize<'de> for #name { fn deserialize(deserializer: D) -> Result<#name, D::Error> where - D: Deserializer<'de>, + D: ::serde::de::Deserializer<'de>, { deserializer.deserialize_str(#visitor_name) } diff --git a/activitystreams-types/src/activity/kind.rs b/activitystreams-types/src/activity/kind.rs index a9b9418..49835e9 100644 --- a/activitystreams-types/src/activity/kind.rs +++ b/activitystreams-types/src/activity/kind.rs @@ -17,12 +17,6 @@ * along with ActivityStreams Types. If not, see . */ -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - #[derive(Clone, Debug, Default, UnitString)] #[activitystreams(Accept)] pub struct AcceptType; diff --git a/activitystreams-types/src/actor/kind.rs b/activitystreams-types/src/actor/kind.rs index c2c2717..bb86127 100644 --- a/activitystreams-types/src/actor/kind.rs +++ b/activitystreams-types/src/actor/kind.rs @@ -19,12 +19,6 @@ //! Namespace for Unit Structs that serialize to strings -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - #[derive(Clone, Debug, Default, UnitString)] #[activitystreams(Application)] pub struct ApplicationType; diff --git a/activitystreams-types/src/collection/kind.rs b/activitystreams-types/src/collection/kind.rs index 5d5db8a..1b8545a 100644 --- a/activitystreams-types/src/collection/kind.rs +++ b/activitystreams-types/src/collection/kind.rs @@ -19,12 +19,6 @@ //! Namespace for Unit Structs that serialize to strings -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - #[derive(Clone, Debug, Default, UnitString)] #[activitystreams(Collection)] pub struct CollectionType; diff --git a/activitystreams-types/src/link/kind.rs b/activitystreams-types/src/link/kind.rs index fdb9662..3782136 100644 --- a/activitystreams-types/src/link/kind.rs +++ b/activitystreams-types/src/link/kind.rs @@ -19,12 +19,6 @@ //! Namespace for Unit Structs that serialize to strings -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - /// A Unit Struct that represents the string "Mention" #[derive(Clone, Debug, Default, UnitString)] #[activitystreams(Mention)] diff --git a/activitystreams-types/src/object/kind.rs b/activitystreams-types/src/object/kind.rs index 3feb730..8494c45 100644 --- a/activitystreams-types/src/object/kind.rs +++ b/activitystreams-types/src/object/kind.rs @@ -19,12 +19,6 @@ //! Namespace for Unit Structs that serialize to strings -use std::fmt; - -use serde::{ - de::{self, Deserialize, Deserializer, Visitor}, ser::{Serialize, Serializer}, -}; - /// A Unit Struct that represents the string "Article" #[derive(Clone, Debug, Default, UnitString)] #[activitystreams(Article)]