TryFrom for BaseBox, pluralize

This commit is contained in:
asonix 2020-04-02 11:20:17 -05:00
parent 7213c7966f
commit a6690f59e4
7 changed files with 50 additions and 20 deletions

View file

@ -1,7 +1,7 @@
[package] [package]
name = "activitystreams" name = "activitystreams"
description = "Activity Streams in Rust" description = "Activity Streams in Rust"
version = "0.5.0-alpha.13" version = "0.5.0-alpha.14"
license = "GPL-3.0" license = "GPL-3.0"
authors = ["asonix <asonix@asonix.dog>"] authors = ["asonix <asonix@asonix.dog>"]
repository = "https://git.asonix.dog/Aardwolf/activitystreams" repository = "https://git.asonix.dog/Aardwolf/activitystreams"
@ -17,7 +17,7 @@ primitives = ["chrono", "mime", "serde", "thiserror", "url"]
types = ["derive", "kinds", "primitives"] types = ["derive", "kinds", "primitives"]
[dependencies] [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 } chrono = { version = "0.4", optional = true }
mime = { version = "0.3", optional = true } mime = { version = "0.3", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true } serde = { version = "1.0", features = ["derive"], optional = true }

View file

@ -9,7 +9,7 @@ __A set of Traits and Types that make up the ActivityStreams and ActivityPub spe
First, add ActivityStreams to your dependencies First, add ActivityStreams to your dependencies
```toml ```toml
activitystreams = "0.5.0-alpha.13" activitystreams = "0.5.0-alpha.14"
``` ```
### Types ### Types
@ -177,7 +177,7 @@ There are a number of features that can be disabled in this crate. By default, e
enabled. enabled.
```toml ```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 | | feature | what you get |

View file

@ -1,7 +1,7 @@
[package] [package]
name = "activitystreams-derive" name = "activitystreams-derive"
description = "Derive macros for activitystreams" description = "Derive macros for activitystreams"
version = "0.5.0-alpha.5" version = "0.5.0-alpha.6"
license = "GPL-3.0" license = "GPL-3.0"
authors = ["asonix <asonix.dev@gmail.com>"] authors = ["asonix <asonix.dev@gmail.com>"]
repository = "https://git.asonix.dog/Aardwolf/activitystreams" repository = "https://git.asonix.dog/Aardwolf/activitystreams"

View file

@ -10,7 +10,7 @@ Add the required crates to your `Cargo.toml`
```toml ```toml
# Cargo.toml # Cargo.toml
activitystreams = "0.5.0-alpha.12" activitystreams = "0.5.0-alpha.14"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
``` ```

View file

@ -23,8 +23,8 @@
//! //!
//! First, add `serde` and `activitystreams-derive` to your Cargo.toml //! First, add `serde` and `activitystreams-derive` to your Cargo.toml
//! ```toml //! ```toml
//! activitystreams-derive = "0.5.0-alpha.5" //! activitystreams-derive = "0.5.0-alpha.6"
//! # or activitystreams = "0.5.0-alpha.12" //! # or activitystreams = "0.5.0-alpha.14"
//! serde = { version = "1.0", features = ["derive"] } //! serde = { version = "1.0", features = ["derive"] }
//! ``` //! ```
//! //!
@ -195,11 +195,10 @@ pub fn ref_derive(input: TokenStream) -> TokenStream {
}; };
let name2 = name.clone(); let name2 = name.clone();
let name3 = name.clone();
let base_impl = quote! { let base_impl = quote! {
impl Base for #name3 {} impl Base for #name {}
impl #name3 { impl #name {
/// Create from default /// Create from default
pub fn new() -> Self { pub fn new() -> Self {
Default::default() Default::default()
@ -213,6 +212,29 @@ pub fn ref_derive(input: TokenStream) -> TokenStream {
BaseBox::from_concrete(s) BaseBox::from_concrete(s)
} }
} }
impl<T> std::convert::TryFrom<Ext<#name, T>> 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<Self, Self::Error> {
BaseBox::from_concrete(s)
}
}
impl<T, U> std::convert::TryFrom<Ext<Ext<#name, T>, 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<Ext<#name, T>, U>) -> Result<Self, Self::Error> {
BaseBox::from_concrete(s)
}
}
}; };
let trait_impls: proc_macro2::TokenStream = input let trait_impls: proc_macro2::TokenStream = input
.attrs .attrs
@ -859,9 +881,9 @@ pub fn properties(tokens: TokenStream) -> TokenStream {
let enum_ty = Ident::new(&camelize(&format!("{}_{}_enum", name, fname)), fname.span()); let enum_ty = Ident::new(&camelize(&format!("{}_{}_enum", name, fname)), fname.span());
let set_many_ident = 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 = 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.required {
if field.description.functional { 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()); Ident::new(&format!("get_{}_{}", fname, snakize(&v_ty.to_token_stream().to_string())), fname.span());
let set_many_ident = 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 = 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 { 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())); 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 acc
}) })
} }
fn pluralize(s: String) -> String {
if s.ends_with('s') || s.ends_with('x') {
s + "es"
} else {
s + "s"
}
}

View file

@ -54,13 +54,13 @@ fn main() -> Result<(), Error> {
let cprops: &CollectionProperties = collection.as_ref(); let cprops: &CollectionProperties = collection.as_ref();
let v: Vec<Ext<Page, ApObjectProperties>> = cprops let v: Vec<Ext<Page, ApObjectProperties>> = cprops
.get_many_items_object_boxs() .get_many_items_base_boxes()
.unwrap() .unwrap()
.map(|object_box| object_box.clone().to_concrete()) .map(|base_box| base_box.clone().to_concrete())
.collect::<Result<Vec<_>, std::io::Error>>()?; .collect::<Result<Vec<_>, std::io::Error>>()?;
let cprops: &mut CollectionProperties = collection.as_mut(); 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); println!("{:#?}", v);

View file

@ -25,7 +25,7 @@
//! //!
//! First, add ActivityStreams to your dependencies //! First, add ActivityStreams to your dependencies
//! ```toml //! ```toml
//! activitystreams = "0.5.0-alpha.13" //! activitystreams = "0.5.0-alpha.14"
//! ``` //! ```
//! //!
//! ### Types //! ### Types
@ -209,7 +209,7 @@
//! enabled. //! enabled.
//! //!
//! ```toml //! ```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 | //! | feature | what you get |