Add more functionality around kind checks
This commit is contained in:
parent
f62902a7aa
commit
d54c647518
1 changed files with 108 additions and 0 deletions
108
src/base.rs
108
src/base.rs
|
@ -1110,6 +1110,54 @@ impl AnyBase {
|
|||
self.as_base().and_then(|base| base.kind.as_ref())
|
||||
}
|
||||
|
||||
/// Get the kind from the current object as an &str
|
||||
///
|
||||
/// This method only produces a value if the current object is a `Base<serde_json::Value>`, and
|
||||
/// the kind is present, and a string
|
||||
///
|
||||
/// ```rust
|
||||
/// # fn main() -> Result<(), anyhow::Error> {
|
||||
/// # use activitystreams_new::{
|
||||
/// # object::{kind::VideoType, Video}, base::AnyBase, prelude::*,
|
||||
/// # };
|
||||
/// # let mut video = Video::new();
|
||||
/// #
|
||||
/// video.set_kind(VideoType);
|
||||
///
|
||||
/// let any_base = AnyBase::from_extended(video)?;
|
||||
///
|
||||
/// match any_base.kind_str() {
|
||||
/// Some("Video") => println!("yay!"),
|
||||
/// _ => return Err(anyhow::Error::msg("invalid type found")),
|
||||
/// }
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn kind_str(&self) -> Option<&str> {
|
||||
self.kind().and_then(|k| k.as_str())
|
||||
}
|
||||
|
||||
/// Check if the current object's kind matches the provided kind
|
||||
///
|
||||
/// ```rust
|
||||
/// # fn main() -> Result<(), anyhow::Error> {
|
||||
/// # use activitystreams_new::{
|
||||
/// # object::{kind::VideoType, Video}, base::AnyBase, prelude::*,
|
||||
/// # };
|
||||
/// # let mut video = Video::new();
|
||||
/// #
|
||||
/// video.set_kind(VideoType);
|
||||
///
|
||||
/// let any_base = AnyBase::from_extended(video)?;
|
||||
///
|
||||
/// assert!(any_base.is_kind("Video"));
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn is_kind(&self, kind: &str) -> bool {
|
||||
self.kind_str() == Some(kind)
|
||||
}
|
||||
|
||||
/// Get the object as an XsdAnyUri
|
||||
///
|
||||
/// ```rust
|
||||
|
@ -1332,6 +1380,66 @@ impl IdOrBase {
|
|||
}
|
||||
|
||||
impl OneOrMany<AnyBase> {
|
||||
/// Get the ID from a single object if there is only one object
|
||||
///
|
||||
/// ```rust
|
||||
/// # use activitystreams_new::{base::Base, primitives::{OneOrMany, XsdAnyUri}};
|
||||
/// # let mut base = Base::<String>::new();
|
||||
/// # let id: XsdAnyUri = "https://example.com".parse().unwrap();
|
||||
/// # base.id = Some(id.clone());
|
||||
/// # let base = OneOrMany::from_base(base.into_generic().unwrap().into());
|
||||
/// #
|
||||
/// assert!(base.as_single_id() == Some(&id));
|
||||
/// ```
|
||||
pub fn as_single_id(&self) -> Option<&XsdAnyUri> {
|
||||
self.as_one().and_then(|one| one.id())
|
||||
}
|
||||
|
||||
/// Get the kind of a single object if there is only one object
|
||||
///
|
||||
/// ```rust
|
||||
/// # use activitystreams_new::{base::Base, primitives::OneOrMany};
|
||||
/// # let mut base = Base::new();
|
||||
/// # base.kind = Some(String::from("Person"));
|
||||
/// # let base = OneOrMany::from_base(base.into_generic().unwrap().into());
|
||||
/// #
|
||||
/// assert!(base.as_single_kind_str() == Some("Person"));
|
||||
/// ```
|
||||
pub fn as_single_kind(&self) -> Option<&serde_json::Value> {
|
||||
self.as_one().and_then(|one| one.kind())
|
||||
}
|
||||
|
||||
/// Get the kind of a single object as an &str
|
||||
///
|
||||
/// This returns None if the kind is not present, or not a String
|
||||
/// ```
|
||||
/// # use activitystreams_new::{base::Base, primitives::OneOrMany};
|
||||
/// # let mut base = Base::new();
|
||||
/// # base.kind = Some(String::from("Person"));
|
||||
/// # let base = OneOrMany::from_base(base.into_generic().unwrap().into());
|
||||
/// #
|
||||
/// assert!(base.as_single_kind_str() == Some("Person"));
|
||||
/// ```
|
||||
pub fn as_single_kind_str(&self) -> Option<&str> {
|
||||
self.as_one().and_then(|one| one.kind_str())
|
||||
}
|
||||
|
||||
/// 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
|
||||
///
|
||||
/// ```
|
||||
/// # use activitystreams_new::{base::Base, primitives::OneOrMany};
|
||||
/// # let mut base = Base::new();
|
||||
/// # base.kind = Some(String::from("Person"));
|
||||
/// # let base = OneOrMany::from_base(base.into_generic().unwrap().into());
|
||||
/// #
|
||||
/// assert!(base.is_single_kind("Person"));
|
||||
/// ```
|
||||
pub fn is_single_kind(&self, kind: &str) -> bool {
|
||||
self.as_single_kind_str() == Some(kind)
|
||||
}
|
||||
|
||||
/// Get a single XsdAnyUri from the object, if that is what is contained
|
||||
///
|
||||
/// ```rust
|
||||
|
|
Loading…
Reference in a new issue