Rust activitystreams patterns
Setting ID(IRI)
code:rust
// or
Setting icon
code:rust
let mut avatar = Image::new();
actor.set_icon(avatar.into_any_base()?);
Creating activity from existing actor and object
code:rust
let mut person = Person::new();
let mut note = Note::new();
let mut act = Create::new(
Base::retract(person)?.into_generic()?,
Base::retract(note)?.into_generic()?,
);
Trait bound for Activity arguments
code:rust
fn f<A: activitystreams::markers::Activity + serde::de::DeserializeOwned>(activity: A) {
do_something_with(activity);
}
fn f<A: activitystreams::markers::Activity + serde::Serialize>(activity: A) {
do_something_with(activity);
}
Choosing one from OneOrMany
code:rust
let chosen_one: Option<Something> =
one_or_many.iter()
.next()
.map(|prop| prop.get_something());
// or
let chosen_one: Option<Something> =
one_or_many.iter()
.next()
.and_then(|prop| prop.get_something()); // .and_then is useful when .get_something() returns Option
Ignores rest of "many".
Getting &str from &AnyString
code:rust
let s: &str = any_string.as_str();
Ignores language RDF string specifying.
Combining them
code:rust
trait ToAsString {
fn to_as_string(&self) -> Option<String>;
}
impl ToAsString for OneOrMany<&AnyString> {
fn to_as_string(&self) -> Option<String> {
self.as_as_str().map(|s| s.to_string())
}
}
trait AsAsStr {
fn as_as_str(&self) -> Option<&str>;
}
impl AsAsStr for OneOrMany<&AnyString> {
fn as_as_str(&self) -> Option<&str> {
self.iter()
.next()
.map(|prop| prop.as_str())
}
}
as here is not "as" but ActivityStreams.
Getting URI as String
code:rust
trait ToAsUri {
fn to_as_uri(&self) -> Option<String>;
}
impl ToAsUri for OneOrMany<AnyBase> {
fn to_as_uri(&self) -> Option<String> {
self.iter()
.next()
.and_then(|prop| prop.as_xsd_any_uri().map(|uri| uri.to_string()))
}
}
Custom properties like signature