Return Option<Iterator<Item = #v_ty>>

This commit is contained in:
asonix 2020-03-14 15:27:43 -05:00
parent 617caa0e70
commit 416ac16af2
8 changed files with 25 additions and 15 deletions

View file

@ -1,7 +1,7 @@
[package]
name = "activitystreams"
description = "Activity Streams in Rust"
version = "0.5.0-alpha.3"
version = "0.5.0-alpha.4"
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", "serde", "serde_json"]
[dependencies]
activitystreams-derive = { version = "0.5.0-alpha.2", path = "activitystreams-derive", optional = true}
activitystreams-derive = { version = "0.5.0-alpha.3", 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.3"
activitystreams = "0.5.0-alpha.4"
```
### 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.0", default-features = "false", features = ["derive"] }
activitystreams = { version = "0.5.0-alpha.4", 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.2"
version = "0.5.0-alpha.3"
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.3"
activitystreams = "0.5.0-alpha.4"
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.2"
//! # or activitystreams = "0.5.0-alpha.3"
//! activitystreams-derive = "0.5.0-alpha.3"
//! # or activitystreams = "0.5.0-alpha.4"
//! serde = { version = "1.0", features = ["derive"] }
//! ```
//!
@ -1060,12 +1060,12 @@ pub fn properties(tokens: TokenStream) -> TokenStream {
///
/// The returned vec will be empty if no values match the requested
/// type, but values are present.
pub fn #get_many_ident(&self) -> Option<Vec<&#v_ty>> {
pub fn #get_many_ident(&self) -> Option<impl Iterator<Item = &#v_ty>> {
match self.#fname {
#ty::Array(ref array) => Some(array.iter().filter_map(|i| match i {
#term_ty::#v_ty(item) => Some(item),
_ => None,
}).collect()),
})),
_ => None,
}
}
@ -1130,12 +1130,12 @@ pub fn properties(tokens: TokenStream) -> TokenStream {
/// This returns `None` if
/// - There is no value present
/// - There is only one value present
pub fn #get_many_ident(&self) -> Option<Vec<&#v_ty>> {
pub fn #get_many_ident(&self) -> Option<impl Iterator<Item = &#v_ty>> {
match self.#fname {
Some(#ty::Array(ref array)) => Some(array.iter().filter_map(|i| match i {
#term_ty::#v_ty(item) => Some(item),
_ => None,
}).collect()),
})),
_ => None,
}
}

View file

@ -1,5 +1,5 @@
use activitystreams::{
collection::apub::OrderedCollection,
collection::{apub::OrderedCollection, properties::CollectionProperties},
object::{streams::Page, ObjectBox},
};
use anyhow::Error;
@ -51,5 +51,14 @@ fn main() -> Result<(), Error> {
let collection: OrderedCollection = serde_json::from_str(collection_json)?;
println!("{:#?}", collection);
let cprops: &CollectionProperties = collection.as_ref();
let v: Vec<Page> = cprops
.get_many_items_object_boxs()
.unwrap()
.map(|object_box| object_box.clone().to_concrete::<Page>())
.collect::<Result<Vec<_>, std::io::Error>>()?;
println!("{:#?}", v);
Ok(())
}

View file

@ -25,7 +25,7 @@
//!
//! First, add ActivityStreams to your dependencies
//! ```toml
//! activitystreams = "0.5.0-alpha.3"
//! activitystreams = "0.5.0-alpha.4"
//! ```
//!
//! ### Types
@ -193,7 +193,7 @@
//! enabled.
//!
//! ```toml
//! activitystreams = { version = "0.5.0-alpha.3", default-features = "false", features = ["derive"] }
//! activitystreams = { version = "0.5.0-alpha.4", default-features = "false", features = ["derive"] }
//! ```
//!
//! | feature | what you get |

View file

@ -53,6 +53,7 @@ pub struct AnyImage {
rest: std::collections::HashMap<String, serde_json::Value>,
}
#[cfg(feature = "types")]
impl AnyImage {
pub fn from_concrete<T>(t: T) -> Result<Self, serde_json::Error>
where