diff --git a/src/activity.rs b/src/activity.rs index d62e255..7da1c0e 100644 --- a/src/activity.rs +++ b/src/activity.rs @@ -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::()?); + /// + /// 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::()?); + /// + /// 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 diff --git a/src/base.rs b/src/base.rs index 5df8444..1734bab 100644 --- a/src/base.rs +++ b/src/base.rs @@ -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` @@ -1436,6 +1457,21 @@ impl OneOrMany { 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::::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