Add checked actor method

This commit is contained in:
asonix 2020-06-19 22:32:52 -05:00
parent 33c262461d
commit df49fcd1a7
1 changed files with 33 additions and 4 deletions

View File

@ -23,7 +23,8 @@
//! # }
//! ```
use crate::{
base::{AnyBase, AsBase, Base, Extends},
base::{AnyBase, AsBase, Base, BaseExt, Extends},
error::DomainError,
markers,
object::{ApObject, AsObject, Object},
primitives::OneOrMany,
@ -445,7 +446,8 @@ pub trait ActivityExt<Kind>: AsActivity<Kind> {
/// Documentation for the fields related to these methods can be found on the
/// `ActorAndObject` struct
pub trait ActorAndObjectRefExt: ActorAndObjectRef {
/// Fetch the actor for the current activity
/// Fetch the actor for the current activity, erroring if the actor's domain does not match the
/// ID's domain
///
/// ```rust
/// # use activitystreams_new::{context, activity::Create};
@ -456,7 +458,34 @@ pub trait ActorAndObjectRefExt: ActorAndObjectRef {
/// let actor_ref = create.actor();
/// println!("{:?}", actor_ref);
/// ```
fn actor(&self) -> &OneOrMany<AnyBase> {
fn actor<'a, Kind>(&'a self) -> Result<&OneOrMany<AnyBase>, DomainError>
where
Self: BaseExt<Kind>,
Kind: 'a,
{
let unchecked = self.actor_unchecked();
if unchecked.as_single_id().and_then(|id| id.domain())
!= self.id_unchecked().and_then(|id| id.domain())
{
return Err(DomainError);
}
Ok(unchecked)
}
/// Fetch the actor for the current activity
///
/// ```rust
/// # use activitystreams_new::{context, activity::Create};
/// # let mut create = Create::new(context(), context());
/// #
/// use activitystreams_new::prelude::*;
///
/// let actor_ref = create.actor_unchecked();
/// println!("{:?}", actor_ref);
/// ```
fn actor_unchecked(&self) -> &OneOrMany<AnyBase> {
self.actor_field_ref()
}
@ -475,7 +504,7 @@ pub trait ActorAndObjectRefExt: ActorAndObjectRef {
/// # }
/// ```
fn actor_is(&self, id: &Url) -> bool {
self.actor().is_single_id(id)
self.actor_unchecked().is_single_id(id)
}
/// Set the actor for the current activity