[][src]Struct activitystreams::primitives::OneOrMany

pub struct OneOrMany<T>(_);

A type representing at least one value

When translated to JSON, it can represent the following structures:

{
    "key": value
}
{
    "key": [],
}
{
    "key": [value, ...]
}

Implementations

impl OneOrMany<AnyBase>[src]

pub fn as_single_id(&self) -> Option<&Url>[src]

Get the ID from a single object if there is only one object

assert!(base.as_single_id() == Some(&id));

pub fn is_single_id(&self, id: &Url) -> bool[src]

Check if there's only one ID, and if it equals id

assert!(base.is_single_id(&id));

pub fn as_single_kind(&self) -> Option<&Value>[src]

Get the kind of a single object if there is only one object

assert!(base.as_single_kind_str() == Some("Person"));

pub fn as_single_kind_str(&self) -> Option<&str>[src]

Get the kind of a single object as an &str

This returns None if the kind is not present, or not a String

assert!(base.as_single_kind_str() == Some("Person"));

pub fn is_single_kind(&self, kind: &str) -> bool[src]

Checks the kind of the inner Base if the current object is a Base

This returns False if the kind is not present, or not a String

assert!(base.is_single_kind("Person"));

pub fn as_single_xsd_any_uri(&self) -> Option<&Url>[src]

Get a single Url from the object, if that is what is contained

let one = OneOrMany::from_xsd_any_uri(uri!("https://example.com"));

assert!(one.as_single_xsd_any_uri().is_some());

pub fn as_single_xsd_string(&self) -> Option<&str>[src]

Get a single &str from the object, if that is what is contained

let one = OneOrMany::<AnyBase>::from_xsd_string("hi".into());

assert!(one.as_single_xsd_string().is_some());

pub fn as_single_base(&self) -> Option<&Base<Value>>[src]

Get a single Base<serde_json::Value> from the object, if that is what is contained

let one = OneOrMany::from_base(base);

assert!(one.as_single_base().is_some());

pub fn single_xsd_any_uri(self) -> Option<Url>[src]

Take a single Url from the object, if that is what is contained

let one = OneOrMany::from_xsd_any_uri(uri!("https://example.com"));

assert!(one.single_xsd_any_uri().is_some());

pub fn single_xsd_string(self) -> Option<String>[src]

Take a single String from the object, if that is what is contained

let one = OneOrMany::<AnyBase>::from_xsd_string("hi".into());

assert!(one.single_xsd_string().is_some());

pub fn single_base(self) -> Option<Base<Value>>[src]

Take a single Base<serde_json::Value> from the object, if that is what is contained

let one = OneOrMany::from_base(base);

assert!(one.single_base().is_some());

pub fn from_xsd_any_uri(id: Url) -> Self[src]

Create a OneOrMany<AnyBase> from a Url

use activitystreams::{primitives::OneOrMany, uri};

let one = OneOrMany::from_xsd_any_uri(uri!("https://example.com"));

pub fn from_xsd_string(xsd_string: String) -> Self[src]

Create a OneOrMany<AnyBase> from a String

use activitystreams::{base::AnyBase, primitives::OneOrMany};

let one = OneOrMany::<AnyBase>::from_xsd_string("hi".into());

pub fn from_base(base: Base<Value>) -> Self[src]

Create a OneOrMany<AnyBase> from a Base<serde_json::Value>

use activitystreams::primitives::OneOrMany;

let one = OneOrMany::from_base(base);

pub fn set_single_xsd_any_uri(&mut self, id: Url) -> &mut Self[src]

Overwrite the current object with a Url

let mut one = OneOrMany::from_base(base);

one.set_single_xsd_any_uri(context());

assert!(one.as_single_xsd_any_uri().is_some());

pub fn set_single_xsd_string(&mut self, xsd_string: String) -> &mut Self[src]

Overwrite the current object with a String

let mut one = OneOrMany::from_base(base);

one.set_single_xsd_string("hi".into());

assert!(one.as_single_xsd_string().is_some());

pub fn set_single_base(&mut self, base: Base<Value>) -> &mut Self[src]

Overwrite the current object with a Base<serde_json::Value>

let mut one = OneOrMany::from_xsd_any_uri(context());

one.set_single_base(base);

assert!(one.as_single_base().is_some());

pub fn add_xsd_any_uri(&mut self, id: Url) -> &mut Self[src]

Append a Url to the current object

use activitystreams::{base::AnyBase, context, primitives::OneOrMany, security};

let mut many = OneOrMany::<AnyBase>::from_xsd_string("hi".into());

many.add_xsd_any_uri(security())
    .add_xsd_any_uri(context());

pub fn add_xsd_string(&mut self, xsd_string: String) -> &mut Self[src]

Append a String to the current object

use activitystreams::{context, primitives::OneOrMany};

let mut many = OneOrMany::from_xsd_any_uri(context());

many.add_xsd_string("hi".into())
    .add_xsd_string("hello".into());

pub fn add_base(&mut self, base: Base<Value>) -> &mut Self[src]

Append a Base<serde_json::Value> to the current object

use activitystreams::{context, primitives::OneOrMany};

let mut many = OneOrMany::from_xsd_any_uri(context());

many.add_base(base1).add_base(base2);

impl OneOrMany<AnyString>[src]

pub fn as_single_xsd_string(&self) -> Option<&str>[src]

Try to borrow a single String from the current object

string
    .as_single_xsd_string()
    .ok_or(anyhow::Error::msg("Wrong string type"))?;

pub fn as_single_rdf_lang_string(&self) -> Option<&RdfLangString>[src]

Try to borrow a single RdfLangString from the current object

string
    .as_single_rdf_lang_string()
    .ok_or(anyhow::Error::msg("Wrong string type"))?;

pub fn single_xsd_string(self) -> Option<String>[src]

Try to take a single String from the current object

string
    .single_xsd_string()
    .ok_or(anyhow::Error::msg("Wrong string type"))?;

pub fn single_rdf_lang_string(self) -> Option<RdfLangString>[src]

Try to take a single RdfLangString from the current object

string
    .single_rdf_lang_string()
    .ok_or(anyhow::Error::msg("Wrong string type"))?;

pub fn from_xsd_string<T>(string: T) -> Self where
    T: Into<String>, 
[src]

Create the object from a single String

use activitystreams::primitives::{OneOrMany, AnyString};

let string = OneOrMany::<AnyString>::from_xsd_string("hi");

pub fn from_rdf_lang_string<T>(string: T) -> Self where
    T: Into<RdfLangString>, 
[src]

Create the object from a single RdfLangString

use activitystreams::primitives::{OneOrMany, RdfLangString};

let string = OneOrMany::from_rdf_lang_string(RdfLangString {
    value: "hi".into(),
    language: "en".into(),
});

pub fn add_xsd_string<T>(&mut self, string: T) -> &mut Self where
    T: Into<String>, 
[src]

Add a String to the object, appending to whatever is currently included

use activitystreams::primitives::{OneOrMany, AnyString};

let mut string = OneOrMany::<AnyString>::from_xsd_string("Hello");

string
    .add_xsd_string("Hey")
    .add_xsd_string("hi");

pub fn add_rdf_lang_string<T>(&mut self, string: T) -> &mut Self where
    T: Into<RdfLangString>, 
[src]

Add an RdfLangString to the object, appending to whatever is currently included

use activitystreams::primitives::{AnyString, OneOrMany, RdfLangString};

let mut string = OneOrMany::<AnyString>::from_xsd_string("Hello");

string
    .add_rdf_lang_string(RdfLangString {
        value: "Hey".into(),
        language: "en".into(),
    })
    .add_rdf_lang_string(RdfLangString {
        value: "hi".into(),
        language: "en".into(),
    });

impl<T> OneOrMany<T>[src]

pub fn iter<'a>(&'a self) -> Iter<'a, T>[src]

Construct an iterator over borrows of the OneOrMany's contents

use activitystreams::primitives::OneOrMany;

let value = OneOrMany::from_one(String::from("hi"));

for item in value.iter() {
    println!("{}", item);
}

pub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, T>[src]

Construct an iterator over mutable borrows of the OneOrMany's contents

use activitystreams::primitives::OneOrMany;

let mut value = OneOrMany::from_one(String::from("hi"));

for item in value.iter_mut() {
    item.push_str("hey");
}

pub fn into_iter(self) -> IntoIter<T>[src]

Construct an iterator over the OneOrMany's contents, consuming the OneOrMany

use activitystreams::primitives::OneOrMany;

let value = OneOrMany::from_one(String::from("hi"));
let vec = value.into_iter().map(|s| s + "hello").collect::<Vec<_>>();

pub fn as_ref(&self) -> OneOrMany<&T>[src]

Create a OneOrMany referencing the existing one

use activitystreams::primitives::OneOrMany;

let value = OneOrMany::from_one(String::from("hi"));
let value_ref = value.as_ref();

assert_eq!(value_ref.one(), Some(&String::from("hi")));

pub fn map<F, U>(self, f: F) -> OneOrMany<U> where
    F: Fn(T) -> U + Copy
[src]

Map the value inside the OneOrMany from T to U

use activitystreams::primitives::OneOrMany;

let value = OneOrMany::from_one("Jake from StateFarm");
let new_value = value.map(|s| format!("Hi, {}", s));

assert_eq!(new_value.one(), Some(String::from("Hi, Jake from StateFarm")));

pub fn as_mut(&mut self) -> OneOrMany<&mut T>[src]

Create a OneOrMany mutably referencing the existing one

use activitystreams::primitives::OneOrMany;

let mut value = OneOrMany::from_one(5);
let value_mut = value.as_mut();

pub fn as_one(&self) -> Option<&T>[src]

Get a reference to a single value

if let Some(v) = value.as_one() {
    println!("{:?}", v);
}

pub fn one_mut(&mut self) -> Option<&mut T>[src]

Borrow one as mutable

use activitystreams::primitives::OneOrMany;

let mut value = OneOrMany::from_one(1);

if let Some(i) = value.one_mut() {
    *i += 1;
}

assert_eq!(value.one(), Some(2));

pub fn one(self) -> Option<T>[src]

Take a single value

if let Some(v) = value.one() {
    println!("{:?}", v);
}

pub fn as_many(&self) -> Option<&[T]>[src]

Get a slice of values

if let Some(v) = value.as_many() {
    for item in v.iter() {
        println!("{:?}", item);
    }
}

pub fn many_mut(&mut self) -> Option<&mut [T]>[src]

Borrow many as mutable

use activitystreams::primitives::OneOrMany;

let mut value = OneOrMany::from_many(vec![1, 2, 3]);

if let Some(v) = value.many_mut() {
    for i in v.iter_mut() {
        *i += 3;
    }
}

assert_eq!(value.many(), Some(vec![4, 5, 6]));

pub fn many(self) -> Option<Vec<T>>[src]

Take a Vec of values

if let Some(v) = value.many() {
    for item in v.into_iter() {
        println!("{:?}", item);
    }
}

pub fn unwrap_to_vec(self) -> Vec<T>[src]

Consume the type, returning a vec

for item in value.unwrap_to_vec() {
    println!("{:?}", item);
}

pub fn from_one(t: T) -> Self[src]

Produce a new object from one value

use activitystreams::primitives::OneOrMany;
let v = OneOrMany::from_one(1234);

pub fn from_many(items: Vec<T>) -> Self[src]

Produce a new object from a vec of values

use activitystreams::primitives::OneOrMany;
let v = OneOrMany::from_many(vec![1, 2, 3, 4]);

pub fn set_one<U>(&mut self, u: U) -> &mut Self where
    U: Into<T>, 
[src]

Overwrite the contents with a single value

value.set_one(3);

assert!(value.as_one().is_some());

pub fn set_many<U>(&mut self, items: impl IntoIterator<Item = U>) -> &mut Self where
    U: Into<T>, 
[src]

Overwrite the contents with vec of values

value.set_many(vec![1, 2, 3, 4]);

assert!(value.as_many().is_some());

pub fn add<U>(&mut self, u: U) -> &mut Self where
    U: Into<T>, 
[src]

Add a value to the object

This appends the value to the existing vec, or converts the single value into a vec, and then appends the new value

use activitystreams::primitives::OneOrMany;
let mut value = OneOrMany::from_one(1234);
value.add(4321);

assert!(value.as_many().is_some());

pub fn add_many<U>(&mut self, items: impl IntoIterator<Item = U>) -> &mut Self where
    U: Into<T>, 
[src]

Add many values to the object

This appends the values to the existing vec, or converts the single value into a vec, and then appends the new values

use activitystreams::primitives::OneOrMany;
let mut value = OneOrMany::from_one(1234);
value.add_many(vec![4321, 2345]);

assert!(value.as_many().is_some());

Trait Implementations

impl<T: Clone> Clone for OneOrMany<T>[src]

impl<T: Debug> Debug for OneOrMany<T>[src]

impl<'de, T> Deserialize<'de> for OneOrMany<T> where
    T: Deserialize<'de>, 
[src]

impl<'_> From<&'_ str> for OneOrMany<AnyBase>[src]

impl<'_> From<&'_ str> for OneOrMany<AnyString>[src]

impl From<Base<Value>> for OneOrMany<AnyBase>[src]

impl From<RdfLangString> for OneOrMany<AnyString>[src]

impl From<String> for OneOrMany<AnyBase>[src]

impl From<String> for OneOrMany<AnyString>[src]

impl<T> From<T> for OneOrMany<T>[src]

impl From<Url> for OneOrMany<AnyBase>[src]

impl<T> From<Vec<T>> for OneOrMany<T>[src]

impl<T> IntoIterator for OneOrMany<T>[src]

type Item = T

The type of the elements being iterated over.

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?

impl<T> Serialize for OneOrMany<T> where
    T: Serialize
[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for OneOrMany<T> where
    T: RefUnwindSafe

impl<T> Send for OneOrMany<T> where
    T: Send

impl<T> Sync for OneOrMany<T> where
    T: Sync

impl<T> Unpin for OneOrMany<T> where
    T: Unpin

impl<T> UnwindSafe for OneOrMany<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]

impl<T> From<!> for T[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.