From a6690f59e46867aabac4e5231b93b19f768412f1 Mon Sep 17 00:00:00 2001 From: asonix Date: Thu, 2 Apr 2020 11:20:17 -0500 Subject: [PATCH] TryFrom for BaseBox, pluralize --- Cargo.toml | 4 +-- README.md | 4 +-- activitystreams-derive/Cargo.toml | 2 +- activitystreams-derive/README.md | 2 +- activitystreams-derive/src/lib.rs | 48 +++++++++++++++++++++++++------ examples/de.rs | 6 ++-- src/lib.rs | 4 +-- 7 files changed, 50 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ef047b6..cc47691 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "activitystreams" description = "Activity Streams in Rust" -version = "0.5.0-alpha.13" +version = "0.5.0-alpha.14" license = "GPL-3.0" authors = ["asonix "] repository = "https://git.asonix.dog/Aardwolf/activitystreams" @@ -17,7 +17,7 @@ primitives = ["chrono", "mime", "serde", "thiserror", "url"] types = ["derive", "kinds", "primitives"] [dependencies] -activitystreams-derive = { version = "0.5.0-alpha.5", path = "activitystreams-derive", optional = true} +activitystreams-derive = { version = "0.5.0-alpha.6", path = "activitystreams-derive", optional = true} chrono = { version = "0.4", optional = true } mime = { version = "0.3", optional = true } serde = { version = "1.0", features = ["derive"], optional = true } diff --git a/README.md b/README.md index 1c33826..b361a34 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ __A set of Traits and Types that make up the ActivityStreams and ActivityPub spe First, add ActivityStreams to your dependencies ```toml -activitystreams = "0.5.0-alpha.13" +activitystreams = "0.5.0-alpha.14" ``` ### Types @@ -177,7 +177,7 @@ There are a number of features that can be disabled in this crate. By default, e enabled. ```toml -activitystreams = { version = "0.5.0-alpha.13", default-features = "false", features = ["derive"] } +activitystreams = { version = "0.5.0-alpha.14", default-features = "false", features = ["derive"] } ``` | feature | what you get | diff --git a/activitystreams-derive/Cargo.toml b/activitystreams-derive/Cargo.toml index b79ea73..83becbd 100644 --- a/activitystreams-derive/Cargo.toml +++ b/activitystreams-derive/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "activitystreams-derive" description = "Derive macros for activitystreams" -version = "0.5.0-alpha.5" +version = "0.5.0-alpha.6" license = "GPL-3.0" authors = ["asonix "] repository = "https://git.asonix.dog/Aardwolf/activitystreams" diff --git a/activitystreams-derive/README.md b/activitystreams-derive/README.md index 1d8f46f..983dd0c 100644 --- a/activitystreams-derive/README.md +++ b/activitystreams-derive/README.md @@ -10,7 +10,7 @@ Add the required crates to your `Cargo.toml` ```toml # Cargo.toml -activitystreams = "0.5.0-alpha.12" +activitystreams = "0.5.0-alpha.14" serde = { version = "1.0", features = ["derive"] } ``` diff --git a/activitystreams-derive/src/lib.rs b/activitystreams-derive/src/lib.rs index f93c783..7042082 100644 --- a/activitystreams-derive/src/lib.rs +++ b/activitystreams-derive/src/lib.rs @@ -23,8 +23,8 @@ //! //! First, add `serde` and `activitystreams-derive` to your Cargo.toml //! ```toml -//! activitystreams-derive = "0.5.0-alpha.5" -//! # or activitystreams = "0.5.0-alpha.12" +//! activitystreams-derive = "0.5.0-alpha.6" +//! # or activitystreams = "0.5.0-alpha.14" //! serde = { version = "1.0", features = ["derive"] } //! ``` //! @@ -195,11 +195,10 @@ pub fn ref_derive(input: TokenStream) -> TokenStream { }; let name2 = name.clone(); - let name3 = name.clone(); let base_impl = quote! { - impl Base for #name3 {} + impl Base for #name {} - impl #name3 { + impl #name { /// Create from default pub fn new() -> Self { Default::default() @@ -213,6 +212,29 @@ pub fn ref_derive(input: TokenStream) -> TokenStream { BaseBox::from_concrete(s) } } + + impl std::convert::TryFrom> for BaseBox + where + T: serde::de::DeserializeOwned + serde::ser::Serialize + std::fmt::Debug, + { + type Error = std::io::Error; + + fn try_from(s: Ext<#name, T>) -> Result { + BaseBox::from_concrete(s) + } + } + + impl std::convert::TryFrom, U>> for BaseBox + where + T: serde::de::DeserializeOwned + serde::ser::Serialize + std::fmt::Debug, + U: serde::de::DeserializeOwned + serde::ser::Serialize + std::fmt::Debug, + { + type Error = std::io::Error; + + fn try_from(s: Ext, U>) -> Result { + BaseBox::from_concrete(s) + } + } }; let trait_impls: proc_macro2::TokenStream = input .attrs @@ -859,9 +881,9 @@ pub fn properties(tokens: TokenStream) -> TokenStream { let enum_ty = Ident::new(&camelize(&format!("{}_{}_enum", name, fname)), fname.span()); let set_many_ident = - Ident::new(&format!("set_many_{}s", fname), fname.span()); + Ident::new(&format!("set_many_{}", pluralize(fname.to_string())), fname.span()); let get_many_ident = - Ident::new(&format!("get_many_{}s", fname), fname.span()); + Ident::new(&format!("get_many_{}", pluralize(fname.to_string())), fname.span()); if field.description.required { if field.description.functional { @@ -1143,9 +1165,9 @@ pub fn properties(tokens: TokenStream) -> TokenStream { Ident::new(&format!("get_{}_{}", fname, snakize(&v_ty.to_token_stream().to_string())), fname.span()); let set_many_ident = - Ident::new(&format!("set_many_{}_{}s", fname, snakize(&v_ty.to_token_stream().to_string())), fname.span()); + Ident::new(&format!("set_many_{}_{}", fname, pluralize(snakize(&v_ty.to_token_stream().to_string()))), fname.span()); let get_many_ident = - Ident::new(&format!("get_many_{}_{}s", fname, snakize(&v_ty.to_token_stream().to_string())), fname.span()); + Ident::new(&format!("get_many_{}_{}", fname, pluralize(snakize(&v_ty.to_token_stream().to_string()))), fname.span()); if field.description.required { let doc_line = to_doc(&format!("Set `{}` with a value that can be converted into `{}`", fname, v_ty.to_token_stream())); @@ -1513,3 +1535,11 @@ fn snakize(s: &str) -> String { acc }) } + +fn pluralize(s: String) -> String { + if s.ends_with('s') || s.ends_with('x') { + s + "es" + } else { + s + "s" + } +} diff --git a/examples/de.rs b/examples/de.rs index ae2c5bf..c7797ca 100644 --- a/examples/de.rs +++ b/examples/de.rs @@ -54,13 +54,13 @@ fn main() -> Result<(), Error> { let cprops: &CollectionProperties = collection.as_ref(); let v: Vec> = cprops - .get_many_items_object_boxs() + .get_many_items_base_boxes() .unwrap() - .map(|object_box| object_box.clone().to_concrete()) + .map(|base_box| base_box.clone().to_concrete()) .collect::, std::io::Error>>()?; let cprops: &mut CollectionProperties = collection.as_mut(); - cprops.set_many_items_object_boxs(v.clone())?; + cprops.set_many_items_base_boxes(v.clone())?; println!("{:#?}", v); diff --git a/src/lib.rs b/src/lib.rs index 14c2cbc..8d8a8bf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,7 @@ //! //! First, add ActivityStreams to your dependencies //! ```toml -//! activitystreams = "0.5.0-alpha.13" +//! activitystreams = "0.5.0-alpha.14" //! ``` //! //! ### Types @@ -209,7 +209,7 @@ //! enabled. //! //! ```toml -//! activitystreams = { version = "0.5.0-alpha.13", default-features = "false", features = ["derive"] } +//! activitystreams = { version = "0.5.0-alpha.14", default-features = "false", features = ["derive"] } //! ``` //! //! | feature | what you get |