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]
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 <asonix@asonix.dog>"]
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 }

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
```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 |

View file

@ -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 <asonix.dev@gmail.com>"]
repository = "https://git.asonix.dog/Aardwolf/activitystreams"

View file

@ -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"] }
```

View file

@ -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<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
.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"
}
}

View file

@ -54,13 +54,13 @@ fn main() -> Result<(), Error> {
let cprops: &CollectionProperties = collection.as_ref();
let v: Vec<Ext<Page, ApObjectProperties>> = 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::<Result<Vec<_>, 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);

View file

@ -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 |