Add is_id methods

This commit is contained in:
asonix 2020-05-21 15:56:22 -05:00
parent cbd6902978
commit ffc8faf825
2 changed files with 81 additions and 1 deletions

View file

@ -26,7 +26,7 @@ use crate::{
base::{AnyBase, AsBase, Base, Extends},
markers,
object::{ApObject, AsObject, Object},
primitives::OneOrMany,
primitives::{OneOrMany, XsdAnyUri},
unparsed::{Unparsed, UnparsedMut, UnparsedMutExt},
};
use std::convert::TryFrom;
@ -431,6 +431,28 @@ pub trait ActorAndObjectRefExt: ActorAndObjectRef {
self.actor_field_ref()
}
/// Check if the actor's ID is `id`
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams_new::{
/// # context,
/// # activity::Create,
/// # primitives::XsdAnyUri
/// # };
/// # let mut create = Create::new(context(), context());
/// use activitystreams_new::prelude::*;
///
/// create.set_actor("https://example.com".parse::<XsdAnyUri>()?);
///
/// assert!(create.actor_is(&"https://example.com".parse()?));
/// # Ok(())
/// # }
/// ```
fn actor_is(&self, id: &XsdAnyUri) -> bool {
self.actor().is_single_id(id)
}
/// Set the actor for the current activity
///
/// This overwrites the contents of actor
@ -531,6 +553,28 @@ pub trait ActorAndObjectRefExt: ActorAndObjectRef {
self.object_field_ref()
}
/// Check if the object's ID is `id`
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams_new::{
/// # context,
/// # activity::Create,
/// # primitives::XsdAnyUri
/// # };
/// # let mut create = Create::new(context(), context());
/// use activitystreams_new::prelude::*;
///
/// create.set_object("https://example.com".parse::<XsdAnyUri>()?);
///
/// assert!(create.object_is(&"https://example.com".parse()?));
/// # Ok(())
/// # }
/// ```
fn object_is(&self, id: &XsdAnyUri) -> bool {
self.object().is_single_id(id)
}
/// Set the object for the current activity
///
/// This overwrites the contents of object

View file

@ -1125,6 +1125,27 @@ impl AnyBase {
.or_else(|| self.as_base().and_then(|base| base.id.as_ref()))
}
/// Check if the current object's id matches the provided id
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams_new::{
/// # object::{kind::VideoType, Video}, base::AnyBase, prelude::*,
/// # };
/// # let mut video = Video::new();
/// #
/// video.set_id("https://example.com".parse()?);
///
/// let any_base = AnyBase::from_extended(video)?;
///
/// assert!(any_base.is_id(&"https://example.com".parse()?));
/// # Ok(())
/// # }
/// ```
pub fn is_id(&self, id: &XsdAnyUri) -> bool {
self.id() == Some(id)
}
/// Get the kind from the current object
///
/// This method only produces a value if the current object is a `Base<serde_json::Value>`
@ -1436,6 +1457,21 @@ impl OneOrMany<AnyBase> {
self.as_one().and_then(|one| one.id())
}
/// Check if there's only one ID, and if it equals `id`
///
/// ```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.is_single_id(&id));
/// ```
pub fn is_single_id(&self, id: &XsdAnyUri) -> bool {
self.as_single_id() == Some(id)
}
/// Get the kind of a single object if there is only one object
///
/// ```rust