pub enum OptionalVec<T> {
NotPresent,
One(Option<T>),
Many(Vec<T>),
}
Expand description
A rather complicated relative of Option<Vec>
There are a couple places in JSON:API where you have keys with values that can either:
- Not Exist
- Be
null
- Contain a single object
- Contain an array of objects
This enum exists for those cases
OptionalVec::NotPresent
exists to specify that the key should not (or did not)
exist
OptionalVec::One
is None
when the value is null
and Some
when there is a
single object
OptionalVec::Many
will be used when the value is an array, empty or not
Variants§
Implementations§
Source§impl<T> OptionalVec<T>
impl<T> OptionalVec<T>
Sourcepub fn is_not_present(&self) -> bool
pub fn is_not_present(&self) -> bool
Checks for the OptionalVec::NotPresent
variant
Mostly provided to allow serialization to be skipped
Sourcepub fn map<U, F>(self, f: F) -> OptionalVec<U>where
F: Fn(T) -> U,
pub fn map<U, F>(self, f: F) -> OptionalVec<U>where
F: Fn(T) -> U,
Map each contained value with the given function
Sourcepub fn one_or<E>(self, e: E) -> Result<Option<T>, E>
pub fn one_or<E>(self, e: E) -> Result<Option<T>, E>
Error with e
if Many
, else build an Option
Sourcepub fn many_or<E>(self, e: E) -> Result<Vec<T>, E>
pub fn many_or<E>(self, e: E) -> Result<Vec<T>, E>
Return the contents of Many
, else error with e
Sourcepub fn into_vec(self) -> Vec<T>
pub fn into_vec(self) -> Vec<T>
Return all contained items as a Vec
Will map OptionalVec::One(Some(v))
into vec![v]
Sourcepub fn unwrap_one(self) -> T
pub fn unwrap_one(self) -> T
Sourcepub fn unwrap_many(self) -> Vec<T>
pub fn unwrap_many(self) -> Vec<T>
Trait Implementations§
Source§impl<T: Clone> Clone for OptionalVec<T>
impl<T: Clone> Clone for OptionalVec<T>
Source§fn clone(&self) -> OptionalVec<T>
fn clone(&self) -> OptionalVec<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T: Debug> Debug for OptionalVec<T>
impl<T: Debug> Debug for OptionalVec<T>
Source§impl<T> Default for OptionalVec<T>
Defaults to OptionalVec::NotPresent
impl<T> Default for OptionalVec<T>
Defaults to OptionalVec::NotPresent