Migrate to IriString

This commit is contained in:
Aode (lion) 2022-01-11 11:58:16 -06:00
parent fe84059d00
commit 245904d078
16 changed files with 808 additions and 1066 deletions

View file

@ -1,7 +1,7 @@
[package]
name = "activitystreams"
description = "A set of core types and traits for activitystreams data"
version = "0.7.0-alpha.14"
version = "0.7.0-alpha.15"
license = "GPL-3.0"
authors = ["asonix <asonix@asonix.dog>"]
repository = "https://git.asonix.dog/Aardwolf/activitystreams"
@ -19,13 +19,13 @@ members = [
]
[dependencies]
activitystreams-kinds = { version = "0.1.0", path = "./activitystreams-kinds/" }
chrono = "0.4"
activitystreams-kinds = { version = "0.1.0", path = "./activitystreams-kinds/", default-features = false, features = ["iri-string"] }
iri-string = { version = "0.4.1", features = ["serde", "serde-std"] }
mime = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0"
url = { version = "2.1", features = ["serde"] }
time = { version = "0.3.5", features = ["formatting", "parsing"] }
[dev-dependencies]
anyhow = "1.0"

View file

@ -11,9 +11,13 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["url"]
[dependencies]
serde = { version = "1", features = ["derive"] }
url = "2"
url = { version = "2", optional = true }
iri-string = { version = "0.4.1", optional = true }
[dev-dependencies]
anyhow = "1"

View file

@ -2,13 +2,16 @@
//!
//! Enums representing typed versions of activitypub 'type' fields.
#[cfg(feature = "url")]
use url::Url;
#[cfg(feature = "url")]
/// Returns the `https://www.w3.org/ns/activitystreams` Url
pub fn context() -> Url {
"https://www.w3.org/ns/activitystreams".parse().unwrap()
}
#[cfg(feature = "url")]
/// Returns the `https://www.w3.org/ns/activitystreams#Public` Url
pub fn public() -> Url {
"https://www.w3.org/ns/activitystreams#Public"
@ -16,11 +19,32 @@ pub fn public() -> Url {
.unwrap()
}
#[cfg(feature = "url")]
/// Returns the `https://w3id.org/security/v1` Url
pub fn security() -> Url {
"https://w3id.org/security/v1".parse().unwrap()
}
#[cfg(feature = "iri-string")]
/// Returns the `https://www.w3.org/ns/activitystreams` IRI
pub fn context_iri() -> iri_string::types::IriString {
"https://www.w3.org/ns/activitystreams".parse().unwrap()
}
#[cfg(feature = "iri-string")]
/// Returns the `https://www.w3.org/ns/activitystreams#Public` IRI
pub fn public_iri() -> iri_string::types::IriString {
"https://www.w3.org/ns/activitystreams#Public"
.parse()
.unwrap()
}
#[cfg(feature = "iri-string")]
/// Returns the `https://w3id.org/security/v1` IRI
pub fn security_iri() -> iri_string::types::IriString {
"https://w3id.org/security/v1".parse().unwrap()
}
/// Generate an enum implementing serde's Serialize and Deserialize with a single variant
///
/// This is useful for describing constants

View file

@ -1,22 +1,21 @@
use activitystreams::{
context,
context, iri,
object::{ApObject, Video},
prelude::*,
uri,
};
use chrono::Duration;
use time::Duration;
fn main() -> Result<(), anyhow::Error> {
let mut video = ApObject::new(Video::new());
video
.set_context(context())
.set_id(uri!("https://example.com/@example/lions"))
.set_id(iri!("https://example.com/@example/lions"))
.set_media_type("video/webm".parse()?)
.set_url(uri!("https://example.com/@example/lions/video.webm"))
.set_url(iri!("https://example.com/@example/lions/video.webm"))
.set_summary("A cool video".to_owned())
.set_duration(Duration::minutes(4) + Duration::seconds(20))
.set_shares(uri!("https://example.com/@example/lions/video.webm#shares"));
.set_shares(iri!("https://example.com/@example/lions/video.webm#shares"));
println!("Video, {:#?}", video);

View file

@ -6,32 +6,31 @@
//! activity::Create,
//! context,
//! prelude::*,
//! uri,
//! iri,
//! };
//!
//! let mut create = Create::new(
//! uri!("https://example.com/actors/abcd"),
//! uri!("https://example.com/notes/1234"),
//! iri!("https://example.com/actors/abcd"),
//! iri!("https://example.com/notes/1234"),
//! );
//!
//! create
//! .set_result(uri!("https://example.com/"))
//! .set_instrument(uri!("https://example.com/"))
//! .set_id(uri!("https://example.com/activities/abcd"))
//! .set_result(iri!("https://example.com/"))
//! .set_instrument(iri!("https://example.com/"))
//! .set_id(iri!("https://example.com/activities/abcd"))
//! .set_context(context());
//! # Ok(())
//! # }
//! ```
use crate::{
base::{AnyBase, AsBase, Base, BaseExt, Extends},
error::DomainError,
base::{AnyBase, AsBase, Base, Extends},
markers,
object::{ApObject, AsObject, Object},
primitives::OneOrMany,
unparsed::{Unparsed, UnparsedMut, UnparsedMutExt},
};
use iri_string::types::IriString;
use std::convert::TryFrom;
use url::Url;
pub use activitystreams_kinds::activity as kind;
@ -158,10 +157,10 @@ pub trait ActivityExt<Kind>: AsActivity<Kind> {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{activity::Question, uri};
/// # use activitystreams::{activity::Question, iri};
/// # let mut question = Question::new();
///
/// question.set_result(uri!("https://example.com"));
/// question.set_result(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
@ -180,12 +179,12 @@ pub trait ActivityExt<Kind>: AsActivity<Kind> {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{activity::Question, uri};
/// # use activitystreams::{activity::Question, iri};
/// # let mut question = Question::new();
///
/// question.set_many_results(vec![
/// uri!("https://example.com/one"),
/// uri!("https://example.com/two"),
/// iri!("https://example.com/one"),
/// iri!("https://example.com/two"),
/// ]);
/// # Ok(())
/// # }
@ -206,13 +205,13 @@ pub trait ActivityExt<Kind>: AsActivity<Kind> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::{prelude::*, uri};
/// use activitystreams::{prelude::*, iri};
/// # use activitystreams::{activity::Question};
/// # let mut question = Question::new();
///
/// question
/// .add_result(uri!("https://example.com/one"))
/// .add_result(uri!("https://example.com/two"));
/// .add_result(iri!("https://example.com/one"))
/// .add_result(iri!("https://example.com/two"));
/// # Ok(())
/// # }
/// ```
@ -251,9 +250,9 @@ pub trait ActivityExt<Kind>: AsActivity<Kind> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{activity::Question, uri};
/// # use activitystreams::{activity::Question, iri};
/// # let mut question = Question::new();
/// # question.set_result(uri!("https://example.com"));
/// # question.set_result(iri!("https://example.com"));
/// #
/// use activitystreams::prelude::*;
///
@ -294,10 +293,10 @@ pub trait ActivityExt<Kind>: AsActivity<Kind> {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{activity::Question, uri};
/// # use activitystreams::{activity::Question, iri};
/// # let mut question = Question::new();
///
/// question.set_instrument(uri!("https://example.com"));
/// question.set_instrument(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
@ -316,12 +315,12 @@ pub trait ActivityExt<Kind>: AsActivity<Kind> {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{activity::Question, uri};
/// # use activitystreams::{activity::Question, iri};
/// # let mut question = Question::new();
///
/// question.set_many_instruments(vec![
/// uri!("https://example.com/one"),
/// uri!("https://example.com/two"),
/// iri!("https://example.com/one"),
/// iri!("https://example.com/two"),
/// ]);
/// # Ok(())
/// # }
@ -343,12 +342,12 @@ pub trait ActivityExt<Kind>: AsActivity<Kind> {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{activity::Question, uri};
/// # use activitystreams::{activity::Question, iri};
/// # let mut question = Question::new();
///
/// question
/// .add_instrument(uri!("https://example.com/one"))
/// .add_instrument(uri!("https://example.com/two"));
/// .add_instrument(iri!("https://example.com/one"))
/// .add_instrument(iri!("https://example.com/two"));
/// # Ok(())
/// # }
/// ```
@ -387,9 +386,9 @@ pub trait ActivityExt<Kind>: AsActivity<Kind> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{activity::Question, uri};
/// # use activitystreams::{activity::Question, iri};
/// # let mut question = Question::new();
/// # question.set_instrument(uri!("https://example.com"));
/// # question.set_instrument(iri!("https://example.com"));
/// #
/// use activitystreams::prelude::*;
///
@ -410,8 +409,7 @@ 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, erroring if the actor's domain does not match the
/// ID's domain
/// Fetch the actor for the current activity
///
/// ```rust
/// # use activitystreams::{context, activity::Create};
@ -422,34 +420,7 @@ pub trait ActorAndObjectRefExt: ActorAndObjectRef {
/// let actor_ref = create.actor();
/// println!("{:?}", actor_ref);
/// ```
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::{context, activity::Create};
/// # let mut create = Create::new(context(), context());
/// #
/// use activitystreams::prelude::*;
///
/// let actor_ref = create.actor_unchecked();
/// println!("{:?}", actor_ref);
/// ```
fn actor_unchecked(&self) -> &OneOrMany<AnyBase> {
fn actor(&self) -> &OneOrMany<AnyBase> {
self.actor_field_ref()
}
@ -457,18 +428,18 @@ pub trait ActorAndObjectRefExt: ActorAndObjectRef {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{context, activity::Create, uri};
/// # use activitystreams::{context, activity::Create, iri};
/// # let mut create = Create::new(context(), context());
/// use activitystreams::prelude::*;
///
/// create.set_actor(uri!("https://example.com"));
/// create.set_actor(iri!("https://example.com"));
///
/// assert!(create.actor_is(&uri!("https://example.com")));
/// assert!(create.actor_is(&iri!("https://example.com")));
/// # Ok(())
/// # }
/// ```
fn actor_is(&self, id: &Url) -> bool {
self.actor_unchecked().is_single_id(id)
fn actor_is(&self, id: &IriString) -> bool {
self.actor().is_single_id(id)
}
/// Set the actor for the current activity
@ -478,10 +449,10 @@ pub trait ActorAndObjectRefExt: ActorAndObjectRef {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{context, activity::Create, uri};
/// # use activitystreams::{context, activity::Create, iri};
/// # let mut create = Create::new(context(), context());
///
/// create.set_actor(uri!("https://example.com"));
/// create.set_actor(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
@ -500,12 +471,12 @@ pub trait ActorAndObjectRefExt: ActorAndObjectRef {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{context, activity::Create, uri};
/// # use activitystreams::{context, activity::Create, iri};
/// # let mut create = Create::new(context(), context());
///
/// create.set_many_actors(vec![
/// uri!("https://example.com/one"),
/// uri!("https://example.com/two"),
/// iri!("https://example.com/one"),
/// iri!("https://example.com/two"),
/// ]);
/// # Ok(())
/// # }
@ -527,12 +498,12 @@ pub trait ActorAndObjectRefExt: ActorAndObjectRef {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{context, activity::Create, uri};
/// # use activitystreams::{context, activity::Create, iri};
/// # let mut create = Create::new(context(), context());
///
/// create
/// .add_actor(uri!("https://example.com/one"))
/// .add_actor(uri!("https://example.com/two"));
/// .add_actor(iri!("https://example.com/one"))
/// .add_actor(iri!("https://example.com/two"));
/// # Ok(())
/// # }
/// ```
@ -563,17 +534,17 @@ pub trait ActorAndObjectRefExt: ActorAndObjectRef {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{context, activity::Create, uri};
/// # use activitystreams::{context, activity::Create, iri};
/// # let mut create = Create::new(context(), context());
/// use activitystreams::prelude::*;
///
/// create.set_object(uri!("https://example.com"));
/// create.set_object(iri!("https://example.com"));
///
/// assert!(create.object_is(&uri!("https://example.com")));
/// assert!(create.object_is(&iri!("https://example.com")));
/// # Ok(())
/// # }
/// ```
fn object_is(&self, id: &Url) -> bool {
fn object_is(&self, id: &IriString) -> bool {
self.object().is_single_id(id)
}
@ -584,10 +555,10 @@ pub trait ActorAndObjectRefExt: ActorAndObjectRef {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{context, activity::Create, uri};
/// # use activitystreams::{context, activity::Create, iri};
/// # let mut create = Create::new(context(), context());
///
/// create.set_object(uri!("https://example.com"));
/// create.set_object(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
@ -606,12 +577,12 @@ pub trait ActorAndObjectRefExt: ActorAndObjectRef {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{context, activity::Create, uri};
/// # use activitystreams::{context, activity::Create, iri};
/// # let mut create = Create::new(context(), context());
///
/// create.set_many_objects(vec![
/// uri!("https://example.com/one"),
/// uri!("https://example.com/two"),
/// iri!("https://example.com/one"),
/// iri!("https://example.com/two"),
/// ]);
/// # Ok(())
/// # }
@ -633,12 +604,12 @@ pub trait ActorAndObjectRefExt: ActorAndObjectRef {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{context, activity::Create, uri};
/// # use activitystreams::{context, activity::Create, iri};
/// # let mut create = Create::new(context(), context());
///
/// create
/// .add_object(uri!("https://example.com/one"))
/// .add_object(uri!("https://example.com/two"));
/// .add_object(iri!("https://example.com/one"))
/// .add_object(iri!("https://example.com/two"));
/// # Ok(())
/// # }
/// ```
@ -677,10 +648,10 @@ pub trait TargetRefExt: TargetRef {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{context, activity::Invite, uri};
/// # use activitystreams::{context, activity::Invite, iri};
/// # let mut invite = Invite::new(context(), context(), context());
///
/// invite.set_target(uri!("https://example.com"));
/// invite.set_target(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
@ -699,12 +670,12 @@ pub trait TargetRefExt: TargetRef {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{context, activity::Invite, uri};
/// # use activitystreams::{context, activity::Invite, iri};
/// # let mut invite = Invite::new(context(), context(), context());
///
/// invite.set_many_targets(vec![
/// uri!("https://example.com/one"),
/// uri!("https://example.com/two"),
/// iri!("https://example.com/one"),
/// iri!("https://example.com/two"),
/// ]);
/// # Ok(())
/// # }
@ -726,12 +697,12 @@ pub trait TargetRefExt: TargetRef {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{context, activity::Invite, uri};
/// # use activitystreams::{context, activity::Invite, iri};
/// # let mut invite = Invite::new(context(), context(), context());
///
/// invite
/// .add_target(uri!("https://example.com/one"))
/// .add_target(uri!("https://example.com/two"));
/// .add_target(iri!("https://example.com/one"))
/// .add_target(iri!("https://example.com/two"));
/// # Ok(())
/// # }
/// ```
@ -770,10 +741,10 @@ pub trait OriginRefExt: OriginRef {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{context, activity::Arrive, uri};
/// # use activitystreams::{context, activity::Arrive, iri};
/// # let mut arrive = Arrive::new(context(), context());
///
/// arrive.set_origin(uri!("https://example.com"));
/// arrive.set_origin(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
@ -792,12 +763,12 @@ pub trait OriginRefExt: OriginRef {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{context, activity::Arrive, uri};
/// # use activitystreams::{context, activity::Arrive, iri};
/// # let mut arrive = Arrive::new(context(), context());
///
/// arrive.set_many_origins(vec![
/// uri!("https://example.com/one"),
/// uri!("https://example.com/two"),
/// iri!("https://example.com/one"),
/// iri!("https://example.com/two"),
/// ]);
/// # Ok(())
/// # }
@ -819,12 +790,12 @@ pub trait OriginRefExt: OriginRef {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{context, activity::Arrive, uri};
/// # use activitystreams::{context, activity::Arrive, iri};
/// # let mut arrive = Arrive::new(context(), context());
///
/// arrive
/// .add_origin(uri!("https://example.com/one"))
/// .add_origin(uri!("https://example.com/two"));
/// .add_origin(iri!("https://example.com/one"))
/// .add_origin(iri!("https://example.com/two"));
/// # Ok(())
/// # }
/// ```
@ -865,10 +836,10 @@ pub trait OptTargetRefExt: OptTargetRef {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{context, activity::Announce, uri};
/// # use activitystreams::{context, activity::Announce, iri};
/// # let mut announce = Announce::new(context(), context());
///
/// announce.set_target(uri!("https://example.com"));
/// announce.set_target(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
@ -887,12 +858,12 @@ pub trait OptTargetRefExt: OptTargetRef {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{context, activity::Announce, uri};
/// # use activitystreams::{context, activity::Announce, iri};
/// # let mut announce = Announce::new(context(), context());
///
/// announce.set_many_targets(vec![
/// uri!("https://example.com/one"),
/// uri!("https://example.com/two"),
/// iri!("https://example.com/one"),
/// iri!("https://example.com/two"),
/// ]);
/// # Ok(())
/// # }
@ -914,12 +885,12 @@ pub trait OptTargetRefExt: OptTargetRef {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{context, activity::Announce, uri};
/// # use activitystreams::{context, activity::Announce, iri};
/// # let mut announce = Announce::new(context(), context());
///
/// announce
/// .add_target(uri!("https://example.com/one"))
/// .add_target(uri!("https://example.com/two"));
/// .add_target(iri!("https://example.com/one"))
/// .add_target(iri!("https://example.com/two"));
/// # Ok(())
/// # }
/// ```
@ -1011,10 +982,10 @@ pub trait OptOriginRefExt: OptOriginRef {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{context, activity::Delete, uri};
/// # use activitystreams::{context, activity::Delete, iri};
/// # let mut delete = Delete::new(context(), context());
///
/// delete.set_origin(uri!("https://example.com"));
/// delete.set_origin(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
@ -1033,12 +1004,12 @@ pub trait OptOriginRefExt: OptOriginRef {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{context, activity::Delete, uri};
/// # use activitystreams::{context, activity::Delete, iri};
/// # let mut delete = Delete::new(context(), context());
///
/// delete.set_many_origins(vec![
/// uri!("https://example.com/one"),
/// uri!("https://example.com/two"),
/// iri!("https://example.com/one"),
/// iri!("https://example.com/two"),
/// ]);
/// # Ok(())
/// # }
@ -1060,12 +1031,12 @@ pub trait OptOriginRefExt: OptOriginRef {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{context, activity::Delete, uri};
/// # use activitystreams::{context, activity::Delete, iri};
/// # let mut delete = Delete::new(context(), context());
///
/// delete
/// .add_origin(uri!("https://example.com/one"))
/// .add_origin(uri!("https://example.com/two"));
/// .add_origin(iri!("https://example.com/one"))
/// .add_origin(iri!("https://example.com/two"));
/// # Ok(())
/// # }
/// ```
@ -1153,10 +1124,10 @@ pub trait QuestionExt: AsQuestion {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{activity::Question, uri};
/// # use activitystreams::{activity::Question, iri};
/// # let mut question = Question::new();
///
/// question.set_one_of(uri!("https://example.com"));
/// question.set_one_of(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
@ -1175,12 +1146,12 @@ pub trait QuestionExt: AsQuestion {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{activity::Question, uri};
/// # use activitystreams::{activity::Question, iri};
/// # let mut question = Question::new();
///
/// question.set_many_one_ofs(vec![
/// uri!("https://example.com/one"),
/// uri!("https://example.com/two"),
/// iri!("https://example.com/one"),
/// iri!("https://example.com/two"),
/// ]);
/// # Ok(())
/// # }
@ -1202,12 +1173,12 @@ pub trait QuestionExt: AsQuestion {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{activity::Question, uri};
/// # use activitystreams::{activity::Question, iri};
/// # let mut question = Question::new();
///
/// question
/// .add_one_of(uri!("https://example.com/one"))
/// .add_one_of(uri!("https://example.com/two"));
/// .add_one_of(iri!("https://example.com/one"))
/// .add_one_of(iri!("https://example.com/two"));
/// # Ok(())
/// # }
/// ```
@ -1246,9 +1217,9 @@ pub trait QuestionExt: AsQuestion {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{activity::Question, uri};
/// # use activitystreams::{activity::Question, iri};
/// # let mut question = Question::new();
/// # question.set_one_of(uri!("https://example.com"));
/// # question.set_one_of(iri!("https://example.com"));
/// #
/// use activitystreams::prelude::*;
///
@ -1286,10 +1257,10 @@ pub trait QuestionExt: AsQuestion {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{activity::Question, uri};
/// # use activitystreams::{activity::Question, iri};
/// # let mut question = Question::new();
///
/// question.set_any_of(uri!("https://example.com"));
/// question.set_any_of(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
@ -1308,12 +1279,12 @@ pub trait QuestionExt: AsQuestion {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{activity::Question, uri};
/// # use activitystreams::{activity::Question, iri};
/// # let mut question = Question::new();
///
/// question.set_many_any_ofs(vec![
/// uri!("https://example.com/one"),
/// uri!("https://example.com/two"),
/// iri!("https://example.com/one"),
/// iri!("https://example.com/two"),
/// ]);
/// # Ok(())
/// # }
@ -1335,12 +1306,12 @@ pub trait QuestionExt: AsQuestion {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{activity::Question, uri};
/// # use activitystreams::{activity::Question, iri};
/// # let mut question = Question::new();
///
/// question
/// .add_any_of(uri!("https://example.com/one"))
/// .add_any_of(uri!("https://example.com/two"));
/// .add_any_of(iri!("https://example.com/one"))
/// .add_any_of(iri!("https://example.com/two"));
/// # Ok(())
/// # }
/// ```
@ -1379,9 +1350,9 @@ pub trait QuestionExt: AsQuestion {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{activity::Question, uri};
/// # use activitystreams::{activity::Question, iri};
/// # let mut question = Question::new();
/// # question.set_any_of(uri!("https://example.com"));
/// # question.set_any_of(iri!("https://example.com"));
/// #
/// use activitystreams::prelude::*;
///
@ -2341,6 +2312,7 @@ impl<Kind> ActorAndObjectOptOriginAndTarget<Kind> {
}
}
#[allow(clippy::type_complexity)]
/// Deconstruct the ActorAndObjectOptOriginAndTarget into its parts
///
/// ```rust
@ -2530,6 +2502,7 @@ impl Travel {
}
}
#[allow(clippy::type_complexity)]
/// Deconstruct the Travel into its parts
///
/// ```rust

View file

@ -5,31 +5,30 @@
//! use activitystreams::{
//! actor::{ApActor, Person},
//! prelude::*,
//! uri,
//! iri,
//! };
//!
//! let mut person = ApActor::new(
//! uri!("https://example.com/actor/inbox"),
//! iri!("https://example.com/actor/inbox"),
//! Person::new(),
//! );
//!
//! person
//! .set_outbox(uri!("https://example.com/actor/outbox"))
//! .set_following(uri!("https://example.com/actor/following"))
//! .set_followers(uri!("https://example.com/actor/followers"));
//! .set_outbox(iri!("https://example.com/actor/outbox"))
//! .set_following(iri!("https://example.com/actor/following"))
//! .set_followers(iri!("https://example.com/actor/followers"));
//! #
//! # Ok(())
//! # }
//! ```
use crate::{
base::{AsBase, Base, BaseExt, Extends},
error::DomainError,
base::{AsBase, Base, Extends},
markers,
object::{ApObject, AsApObject, AsObject, Object},
primitives::OneOrMany,
unparsed::{Unparsed, UnparsedMut, UnparsedMutExt},
};
use url::Url;
use iri_string::types::IriString;
pub use activitystreams_kinds::actor as kind;
@ -52,36 +51,6 @@ pub trait AsApActor<Inner>: markers::Actor {
///
/// Documentation for the fields related to these methods can be found on the `ApActor` struct
pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// Fetch the inbox for the current actor, erroring if the inbox's domain does not match the
/// ID's domain
///
/// ```
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context};
/// # let mut person = ApActor::new(context(), Person::new());
/// # person.set_id(context());
/// use activitystreams::prelude::*;
///
/// let inbox = person.inbox()?;
/// println!("{:?}", inbox);
/// # Ok(())
/// # }
/// ```
fn inbox<'a, Kind>(&'a self) -> Result<&'a Url, DomainError>
where
Self: BaseExt<Kind> + 'a,
Inner: 'a,
Kind: 'a,
{
let unchecked = self.inbox_unchecked();
if unchecked.domain() != self.id_unchecked().and_then(|id| id.domain()) {
return Err(DomainError);
}
Ok(unchecked)
}
/// Fetch the inbox for the current actor
///
/// ```rust
@ -89,9 +58,9 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// let inbox_ref = person.inbox_unchecked();
/// let inbox_ref = person.inbox();
/// ```
fn inbox_unchecked<'a>(&'a self) -> &'a Url
fn inbox<'a>(&'a self) -> &'a IriString
where
Inner: 'a,
{
@ -106,9 +75,8 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// use activitystreams::prelude::*;
///
/// let inbox_mut = person.inbox_mut();
/// inbox_mut.set_path("/inbox");
/// ```
fn inbox_mut<'a>(&'a mut self) -> &'a mut Url
fn inbox_mut<'a>(&'a mut self) -> &'a mut IriString
where
Inner: 'a,
{
@ -119,49 +87,19 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context, uri};
/// # use activitystreams::{actor::{ApActor, Person}, context, iri};
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// person.set_inbox(uri!("https://example.com/inbox"));
/// person.set_inbox(iri!("https://example.com/inbox"));
/// # Ok(())
/// # }
/// ```
fn set_inbox(&mut self, inbox: Url) -> &mut Self {
fn set_inbox(&mut self, inbox: IriString) -> &mut Self {
self.ap_actor_mut().inbox = inbox;
self
}
/// Fetch the outbox for the current user, erroring if the oubox's domain does not match the
/// ID's domain
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context};
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// let outbox_ref = person.outbox()?;
/// # Ok(())
/// # }
/// ```
fn outbox<'a, Kind>(&'a self) -> Result<Option<&'a Url>, DomainError>
where
Self: BaseExt<Kind>,
Inner: 'a,
Kind: 'a,
{
if let Some(unchecked) = self.outbox_unchecked() {
if unchecked.domain() != self.id_unchecked().and_then(|id| id.domain()) {
return Err(DomainError);
}
return Ok(Some(unchecked));
}
Ok(None)
}
/// Fetch the outbox for the current actor
///
/// ```rust
@ -169,9 +107,9 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// let outbox_ref = person.outbox_unchecked();
/// let outbox_ref = person.outbox();
/// ```
fn outbox_unchecked<'a>(&'a self) -> Option<&'a Url>
fn outbox<'a>(&'a self) -> Option<&'a IriString>
where
Inner: 'a,
{
@ -186,11 +124,10 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// use activitystreams::prelude::*;
///
/// if let Some(outbox) = person.outbox_mut() {
/// outbox.set_path("/outbox");
/// println!("{:?}", outbox);
/// }
/// ```
fn outbox_mut<'a>(&'a mut self) -> Option<&'a mut Url>
fn outbox_mut<'a>(&'a mut self) -> Option<&'a mut IriString>
where
Inner: 'a,
{
@ -201,15 +138,15 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context, uri};
/// # use activitystreams::{actor::{ApActor, Person}, context, iri};
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// person.set_outbox(uri!("https://example.com/outbox"));
/// person.set_outbox(iri!("https://example.com/outbox"));
/// # Ok(())
/// # }
/// ```
fn set_outbox(&mut self, outbox: Url) -> &mut Self {
fn set_outbox(&mut self, outbox: IriString) -> &mut Self {
self.ap_actor_mut().outbox = Some(outbox);
self
}
@ -225,7 +162,7 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// println!("{:?}", outbox);
/// }
/// ```
fn take_outbox(&mut self) -> Option<Url> {
fn take_outbox(&mut self) -> Option<IriString> {
self.ap_actor_mut().outbox.take()
}
@ -233,14 +170,14 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context, uri};
/// # use activitystreams::{actor::{ApActor, Person}, context, iri};
/// # let mut person = ApActor::new(context(), Person::new());
/// # person.set_outbox(uri!("https://example.com/outbox"));
/// # person.set_outbox(iri!("https://example.com/outbox"));
/// use activitystreams::prelude::*;
///
/// assert!(person.outbox_unchecked().is_some());
/// assert!(person.outbox().is_some());
/// person.delete_outbox();
/// assert!(person.outbox_unchecked().is_none());
/// assert!(person.outbox().is_none());
/// # Ok(())
/// # }
/// ```
@ -249,38 +186,6 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
self
}
/// Fetch the following link for the current user, erroring if the following link's domain does
/// not match the ID's domain
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context};
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// if let Some(following) = person.following()? {
/// println!("{:?}", following);
/// }
/// # Ok(())
/// # }
/// ```
fn following<'a, Kind>(&'a self) -> Result<Option<&'a Url>, DomainError>
where
Self: BaseExt<Kind>,
Inner: 'a,
Kind: 'a,
{
if let Some(unchecked) = self.following_unchecked() {
if unchecked.domain() != self.id_unchecked().and_then(|id| id.domain()) {
return Err(DomainError);
}
return Ok(Some(unchecked));
}
Ok(None)
}
/// Fetch the following link for the current actor
///
/// ```rust
@ -288,11 +193,11 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// if let Some(following) = person.following_unchecked() {
/// if let Some(following) = person.following() {
/// println!("{:?}", following);
/// }
/// ```
fn following_unchecked<'a>(&'a self) -> Option<&'a Url>
fn following<'a>(&'a self) -> Option<&'a IriString>
where
Inner: 'a,
{
@ -307,11 +212,10 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// use activitystreams::prelude::*;
///
/// if let Some(following) = person.following_mut() {
/// following.set_path("/following");
/// println!("{:?}", following);
/// }
/// ```
fn following_mut<'a>(&'a mut self) -> Option<&'a mut Url>
fn following_mut<'a>(&'a mut self) -> Option<&'a mut IriString>
where
Inner: 'a,
{
@ -322,15 +226,15 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context, uri};
/// # use activitystreams::{actor::{ApActor, Person}, context, iri};
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// person.set_following(uri!("https://example.com/following"));
/// person.set_following(iri!("https://example.com/following"));
/// # Ok(())
/// # }
/// ```
fn set_following(&mut self, following: Url) -> &mut Self {
fn set_following(&mut self, following: IriString) -> &mut Self {
self.ap_actor_mut().following = Some(following);
self
}
@ -346,7 +250,7 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// println!("{:?}", following);
/// }
/// ```
fn take_following(&mut self) -> Option<Url> {
fn take_following(&mut self) -> Option<IriString> {
self.ap_actor_mut().following.take()
}
@ -354,14 +258,14 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context, uri};
/// # use activitystreams::{actor::{ApActor, Person}, context, iri};
/// # let mut person = ApActor::new(context(), Person::new());
/// # person.set_following(uri!("https://example.com/following"));
/// # person.set_following(iri!("https://example.com/following"));
/// use activitystreams::prelude::*;
///
/// assert!(person.following_unchecked().is_some());
/// assert!(person.following().is_some());
/// person.delete_following();
/// assert!(person.following_unchecked().is_none());
/// assert!(person.following().is_none());
/// # Ok(())
/// # }
/// ```
@ -370,38 +274,6 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
self
}
/// Fetch the followers link for the current actor, erroring if the followers link's domain
/// does not match the ID's domain
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context};
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// if let Some(followers) = person.followers()? {
/// println!("{:?}", followers);
/// }
/// # Ok(())
/// # }
/// ```
fn followers<'a, Kind>(&'a self) -> Result<Option<&'a Url>, DomainError>
where
Self: BaseExt<Kind>,
Inner: 'a,
Kind: 'a,
{
if let Some(unchecked) = self.followers_unchecked() {
if unchecked.domain() != self.id_unchecked().and_then(|id| id.domain()) {
return Err(DomainError);
}
return Ok(Some(unchecked));
}
Ok(None)
}
/// Fetch the followers link for the current actor
///
/// ```rust
@ -409,11 +281,11 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// if let Some(followers) = person.followers_unchecked() {
/// if let Some(followers) = person.followers() {
/// println!("{:?}", followers);
/// }
/// ```
fn followers_unchecked<'a>(&'a self) -> Option<&'a Url>
fn followers<'a>(&'a self) -> Option<&'a IriString>
where
Inner: 'a,
{
@ -428,11 +300,10 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// use activitystreams::prelude::*;
///
/// if let Some(followers) = person.followers_mut() {
/// followers.set_path("/followers");
/// println!("{:?}", followers);
/// }
/// ```
fn followers_mut<'a>(&'a mut self) -> Option<&'a mut Url>
fn followers_mut<'a>(&'a mut self) -> Option<&'a mut IriString>
where
Inner: 'a,
{
@ -443,15 +314,15 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context, uri};
/// # use activitystreams::{actor::{ApActor, Person}, context, iri};
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// person.set_followers(uri!("https://example.com/followers"));
/// person.set_followers(iri!("https://example.com/followers"));
/// # Ok(())
/// # }
/// ```
fn set_followers(&mut self, followers: Url) -> &mut Self {
fn set_followers(&mut self, followers: IriString) -> &mut Self {
self.ap_actor_mut().followers = Some(followers);
self
}
@ -467,7 +338,7 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// println!("{:?}", followers);
/// }
/// ```
fn take_followers(&mut self) -> Option<Url> {
fn take_followers(&mut self) -> Option<IriString> {
self.ap_actor_mut().followers.take()
}
@ -475,14 +346,14 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context, uri};
/// # use activitystreams::{actor::{ApActor, Person}, context, iri};
/// # let mut person = ApActor::new(context(), Person::new());
/// # person.set_followers(uri!("https://example.com/followers"));
/// # person.set_followers(iri!("https://example.com/followers"));
/// use activitystreams::prelude::*;
///
/// assert!(person.followers_unchecked().is_some());
/// assert!(person.followers().is_some());
/// person.delete_followers();
/// assert!(person.followers_unchecked().is_none());
/// assert!(person.followers().is_none());
/// # Ok(())
/// # }
/// ```
@ -491,38 +362,6 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
self
}
/// Fetch the liked link for the current actor, erroring if the liked link's domain does not
/// match the ID's domain
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context};
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// if let Some(liked) = person.liked()? {
/// println!("{:?}", liked);
/// }
/// # Ok(())
/// # }
/// ```
fn liked<'a, Kind>(&'a self) -> Result<Option<&'a Url>, DomainError>
where
Self: BaseExt<Kind>,
Inner: 'a,
Kind: 'a,
{
if let Some(unchecked) = self.liked_unchecked() {
if unchecked.domain() != self.id_unchecked().and_then(|id| id.domain()) {
return Err(DomainError);
}
return Ok(Some(unchecked));
}
Ok(None)
}
/// Fetch the liked link for the current actor
///
/// ```rust
@ -530,11 +369,11 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// if let Some(liked) = person.liked_unchecked() {
/// if let Some(liked) = person.liked() {
/// println!("{:?}", liked);
/// }
/// ```
fn liked_unchecked<'a>(&'a self) -> Option<&'a Url>
fn liked<'a>(&'a self) -> Option<&'a IriString>
where
Inner: 'a,
{
@ -549,11 +388,10 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// use activitystreams::prelude::*;
///
/// if let Some(liked) = person.liked_mut() {
/// liked.set_path("/liked");
/// println!("{:?}", liked);
/// }
/// ```
fn liked_mut<'a>(&'a mut self) -> Option<&'a mut Url>
fn liked_mut<'a>(&'a mut self) -> Option<&'a mut IriString>
where
Inner: 'a,
{
@ -564,15 +402,15 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context, uri};
/// # use activitystreams::{actor::{ApActor, Person}, context, iri};
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// person.set_stream(uri!("https://example.com/liked"));
/// person.set_stream(iri!("https://example.com/liked"));
/// # Ok(())
/// # }
/// ```
fn set_liked(&mut self, liked: Url) -> &mut Self {
fn set_liked(&mut self, liked: IriString) -> &mut Self {
self.ap_actor_mut().liked = Some(liked);
self
}
@ -588,7 +426,7 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// println!("{:?}", liked);
/// }
/// ```
fn take_liked(&mut self) -> Option<Url> {
fn take_liked(&mut self) -> Option<IriString> {
self.ap_actor_mut().liked.take()
}
@ -596,14 +434,14 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context, uri};
/// # use activitystreams::{actor::{ApActor, Person}, context, iri};
/// # let mut person = ApActor::new(context(), Person::new());
/// # person.set_liked(uri!("https://example.com/liked"));
/// # person.set_liked(iri!("https://example.com/liked"));
/// use activitystreams::prelude::*;
///
/// assert!(person.liked_unchecked().is_some());
/// assert!(person.liked().is_some());
/// person.delete_liked();
/// assert!(person.liked_unchecked().is_none());
/// assert!(person.liked().is_none());
/// # Ok(())
/// # }
/// ```
@ -612,50 +450,6 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
self
}
/// Fetch the streams links for the current actor, erroring if the streams links's domains do
/// not match the ID's domains
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context};
/// # let mut person = ApActor::new(context(), Person::new());
/// # person.set_id(context()).add_stream(context()).add_stream(context());
/// use activitystreams::prelude::*;
///
/// if let Some(streams) = person.streams()? {
/// println!("{:?}", streams);
/// }
/// # Ok(())
/// # }
/// ```
fn streams<'a, Kind>(&'a self) -> Result<Option<OneOrMany<&'a Url>>, DomainError>
where
Self: BaseExt<Kind>,
Inner: 'a,
Kind: 'a,
{
if let Some(unchecked) = self.streams_unchecked() {
let domain_opt = self.id_unchecked().and_then(|id| id.domain());
let one = unchecked
.as_one()
.map(|url| url.domain() == domain_opt)
.unwrap_or(false);
let many = unchecked
.as_many()
.map(|urls| urls.iter().all(|url| url.domain() == domain_opt))
.unwrap_or(false);
if !one && !many {
return Err(DomainError);
}
return Ok(Some(unchecked));
}
Ok(None)
}
/// Fetch the streams links for the current actor
///
/// ```rust
@ -663,11 +457,11 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// if let Some(streams) = person.streams_unchecked() {
/// if let Some(streams) = person.streams() {
/// println!("{:?}", streams);
/// }
/// ```
fn streams_unchecked<'a>(&'a self) -> Option<OneOrMany<&'a Url>>
fn streams<'a>(&'a self) -> Option<OneOrMany<&'a IriString>>
where
Inner: 'a,
{
@ -682,16 +476,13 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// use activitystreams::prelude::*;
///
/// if let Some(mut streams) = person.streams_mut() {
/// streams.one_mut().map(|url| url.set_path("/streams"));
/// streams.many_mut().map(|urls| {
/// for url in urls.iter_mut() {
/// url.set_path("/streams");
/// for url in streams.iter_mut() {
/// // whatever
/// }
/// });
/// println!("{:?}", streams);
/// }
/// ```
fn streams_mut<'a>(&'a mut self) -> Option<OneOrMany<&'a mut Url>>
fn streams_mut<'a>(&'a mut self) -> Option<OneOrMany<&'a mut IriString>>
where
Inner: 'a,
{
@ -702,15 +493,15 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context, uri};
/// # use activitystreams::{actor::{ApActor, Person}, context, iri};
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// person.set_stream(uri!("https://example.com/streams"));
/// person.set_stream(iri!("https://example.com/streams"));
/// # Ok(())
/// # }
/// ```
fn set_stream(&mut self, streams: Url) -> &mut Self {
fn set_stream(&mut self, streams: IriString) -> &mut Self {
self.ap_actor_mut().streams = Some(streams.into());
self
}
@ -719,13 +510,13 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context, uri};
/// # use activitystreams::{actor::{ApActor, Person}, context, iri};
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// person.set_many_streams(vec![
/// uri!("https://example.com/streams1"),
/// uri!("https://example.com/streams2"),
/// iri!("https://example.com/streams1"),
/// iri!("https://example.com/streams2"),
/// ]);
/// # Ok(())
/// # }
@ -733,9 +524,9 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
fn set_many_streams<I, U>(&mut self, items: I) -> &mut Self
where
I: IntoIterator<Item = U>,
U: Into<Url>,
U: Into<IriString>,
{
let v: Vec<Url> = items.into_iter().map(|u| u.into()).collect();
let v: Vec<IriString> = items.into_iter().map(|u| u.into()).collect();
self.ap_actor_mut().streams = Some(v.into());
self
}
@ -744,17 +535,17 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context, uri};
/// # use activitystreams::{actor::{ApActor, Person}, context, iri};
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// person
/// .add_stream(uri!("https://example.com/streams1"))
/// .add_stream(uri!("https://example.com/streams2"));
/// .add_stream(iri!("https://example.com/streams1"))
/// .add_stream(iri!("https://example.com/streams2"));
/// # Ok(())
/// # }
/// ```
fn add_stream(&mut self, stream: Url) -> &mut Self {
fn add_stream(&mut self, stream: IriString) -> &mut Self {
let v = match self.ap_actor_mut().streams.take() {
Some(mut v) => {
v.add(stream);
@ -777,7 +568,7 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// println!("{:?}", streams);
/// }
/// ```
fn take_streams(&mut self) -> Option<OneOrMany<Url>> {
fn take_streams(&mut self) -> Option<OneOrMany<IriString>> {
self.ap_actor_mut().streams.take()
}
@ -785,14 +576,14 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Person}, context, uri};
/// # use activitystreams::{actor::{ApActor, Person}, context, iri};
/// # let mut person = ApActor::new(context(), Person::new());
/// # person.set_stream(uri!("https://example.com/streams"));
/// # person.set_stream(iri!("https://example.com/streams"));
/// use activitystreams::prelude::*;
///
/// assert!(person.streams_unchecked().is_some());
/// assert!(person.streams().is_some());
/// person.delete_streams();
/// assert!(person.streams_unchecked().is_none());
/// assert!(person.streams().is_none());
/// # Ok(())
/// # }
/// ```
@ -871,71 +662,6 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
self
}
/// Fetch the endpoints for the current actor, erroring if the Endpoints' domains do not
/// match the ID's domain
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Endpoints, Person}, context};
/// # let mut person = ApActor::new(context(), Person::new());
/// # person.set_id(context()).set_endpoints(Endpoints {
/// # shared_inbox: Some(context()),
/// # ..Default::default()
/// # });
/// use activitystreams::prelude::*;
///
/// if let Some(endpoints) = person.endpoints()? {
/// println!("{:?}", endpoints);
/// }
/// # Ok(())
/// # }
/// ```
fn endpoints<'a, Kind>(&'a self) -> Result<Option<Endpoints<&'a Url>>, DomainError>
where
Self: BaseExt<Kind>,
Inner: 'a,
Kind: 'a,
{
if let Some(endpoints) = self.endpoints_unchecked() {
let domain_opt = self.id_unchecked().and_then(|id| id.domain());
let mut any_failed = false;
any_failed |= endpoints
.proxy_url
.map(|u| u.domain() != domain_opt)
.unwrap_or(false);
any_failed |= endpoints
.oauth_authorization_endpoint
.map(|u| u.domain() != domain_opt)
.unwrap_or(false);
any_failed |= endpoints
.oauth_token_endpoint
.map(|u| u.domain() != domain_opt)
.unwrap_or(false);
any_failed |= endpoints
.provide_client_key
.map(|u| u.domain() != domain_opt)
.unwrap_or(false);
any_failed |= endpoints
.sign_client_key
.map(|u| u.domain() != domain_opt)
.unwrap_or(false);
any_failed |= endpoints
.shared_inbox
.map(|u| u.domain() != domain_opt)
.unwrap_or(false);
if any_failed {
return Err(DomainError);
}
return Ok(Some(endpoints));
}
Ok(None)
}
/// Fetch the endpoints for the current actor
///
/// ```rust
@ -943,11 +669,11 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// if let Some(endpoints) = person.endpoints_unchecked() {
/// if let Some(endpoints) = person.endpoints() {
/// println!("{:?}", endpoints);
/// }
/// ```
fn endpoints_unchecked<'a>(&'a self) -> Option<Endpoints<&'a Url>>
fn endpoints<'a>(&'a self) -> Option<Endpoints<&'a IriString>>
where
Inner: 'a,
{
@ -962,11 +688,13 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// use activitystreams::prelude::*;
///
/// if let Some(mut endpoints) = person.endpoints_mut() {
/// endpoints.shared_inbox.as_mut().map(|url| url.set_path("/inbox"));
/// if let Some(url) = endpoints.shared_inbox.as_mut() {
/// // whatever
/// }
/// println!("{:?}", endpoints);
/// }
/// ```
fn endpoints_mut<'a>(&'a mut self) -> Option<Endpoints<&'a mut Url>>
fn endpoints_mut<'a>(&'a mut self) -> Option<Endpoints<&'a mut IriString>>
where
Inner: 'a,
{
@ -977,18 +705,18 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{actor::{ApActor, Endpoints, Person}, context, uri};
/// # use activitystreams::{actor::{ApActor, Endpoints, Person}, context, iri};
/// # let mut person = ApActor::new(context(), Person::new());
/// use activitystreams::prelude::*;
///
/// person.set_endpoints(Endpoints {
/// shared_inbox: Some(uri!("https://example.com/inbox")),
/// shared_inbox: Some(iri!("https://example.com/inbox")),
/// ..Default::default()
/// });
/// # Ok(())
/// # }
/// ```
fn set_endpoints(&mut self, endpoints: Endpoints<Url>) -> &mut Self {
fn set_endpoints(&mut self, endpoints: Endpoints<IriString>) -> &mut Self {
self.ap_actor_mut().endpoints = Some(endpoints);
self
}
@ -1004,7 +732,7 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// println!("{:?}", endpoints);
/// }
/// ```
fn take_endpoints(&mut self) -> Option<Endpoints<Url>> {
fn take_endpoints(&mut self) -> Option<Endpoints<IriString>> {
self.ap_actor_mut().endpoints.take()
}
@ -1016,9 +744,9 @@ pub trait ApActorExt<Inner>: AsApActor<Inner> {
/// # person.set_endpoints(Default::default());
/// use activitystreams::prelude::*;
///
/// assert!(person.endpoints_unchecked().is_some());
/// assert!(person.endpoints().is_some());
/// person.delete_endpoints();
/// assert!(person.endpoints_unchecked().is_none());
/// assert!(person.endpoints().is_none());
/// ```
fn delete_endpoints(&mut self) -> &mut Self {
self.ap_actor_mut().endpoints = None;
@ -1081,42 +809,42 @@ pub struct ApActor<Inner> {
///
/// - Range: xsd:anyUri
/// - Functional: true
inbox: Url,
inbox: IriString,
/// An ActivityStreams] OrderedCollection comprised of all the messages produced by the actor.
///
/// - Range: xsd:anyUri
/// - Functional: true
#[serde(skip_serializing_if = "Option::is_none")]
outbox: Option<Url>,
outbox: Option<IriString>,
/// A link to an [ActivityStreams] collection of the actors that this actor is following.
///
/// - Range: xsd:anyUri
/// - Functional: true
#[serde(skip_serializing_if = "Option::is_none")]
following: Option<Url>,
following: Option<IriString>,
/// A link to an [ActivityStreams] collection of the actors that follow this actor.
///
/// - Range: xsd:anyUri
/// - Functional: true
#[serde(skip_serializing_if = "Option::is_none")]
followers: Option<Url>,
followers: Option<IriString>,
/// A link to an [ActivityStreams] collection of objects this actor has liked.
///
/// - Range: xsd:anyUri
/// - Functional: true
#[serde(skip_serializing_if = "Option::is_none")]
liked: Option<Url>,
liked: Option<IriString>,
/// A list of supplementary Collections which may be of interest.
///
/// - Range: xsd:anyUri
/// - Functional: false
#[serde(skip_serializing_if = "Option::is_none")]
streams: Option<OneOrMany<Url>>,
streams: Option<OneOrMany<IriString>>,
/// A short username which may be used to refer to the actor, with no uniqueness guarantees.
///
@ -1134,7 +862,7 @@ pub struct ApActor<Inner> {
/// - Range: Endpoint
/// - Functional: true
#[serde(skip_serializing_if = "Option::is_none")]
endpoints: Option<Endpoints<Url>>,
endpoints: Option<Endpoints<IriString>>,
/// base fields and unparsed json ends up here
#[serde(flatten)]
@ -1265,16 +993,16 @@ impl<Inner> ApActor<Inner> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::{actor::{ApActor, Person}, uri};
/// use activitystreams::{actor::{ApActor, Person}, iri};
///
/// let actor = ApActor::new(
/// uri!("https://example.com/inbox"),
/// iri!("https://example.com/inbox"),
/// Person::new(),
/// );
/// # Ok(())
/// # }
/// ```
pub fn new(inbox: Url, inner: Inner) -> Self
pub fn new(inbox: IriString, inner: Inner) -> Self
where
Inner: markers::Actor,
{
@ -1291,13 +1019,14 @@ impl<Inner> ApActor<Inner> {
}
}
#[allow(clippy::type_complexity)]
/// Deconstruct the ApActor into its parts
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::{actor::{ApActor, Person}, uri};
/// use activitystreams::{actor::{ApActor, Person}, iri};
///
/// let actor = ApActor::new(uri!("https://inbox.url"), Person::new());
/// let actor = ApActor::new(iri!("https://inbox.url"), Person::new());
///
/// let (
/// inbox,
@ -1316,14 +1045,14 @@ impl<Inner> ApActor<Inner> {
pub fn into_parts(
self,
) -> (
Url,
Option<Url>,
Option<Url>,
Option<Url>,
Option<Url>,
Option<OneOrMany<Url>>,
IriString,
Option<IriString>,
Option<IriString>,
Option<IriString>,
Option<IriString>,
Option<OneOrMany<IriString>>,
Option<String>,
Option<Endpoints<Url>>,
Option<Endpoints<IriString>>,
Inner,
) {
(
@ -1400,12 +1129,12 @@ impl<T> Endpoints<T> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::{actor::Endpoints, uri};
/// use url::Url;
/// use activitystreams::{actor::Endpoints, iri};
/// use iri_string::types::IriString;
///
/// let uri = uri!("https://example.com");
/// let uri = iri!("https://example.com");
///
/// let endpoints: Endpoints<Url> = Endpoints {
/// let endpoints: Endpoints<IriString> = Endpoints {
/// shared_inbox: Some(uri.clone()),
/// ..Default::default()
/// };
@ -1441,17 +1170,17 @@ impl<T> Endpoints<T> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::{actor::Endpoints, uri};
/// use url::Url;
/// use activitystreams::{actor::Endpoints, iri};
/// use iri_string::types::IriString;
///
/// let endpoints: Endpoints<Url> = Endpoints {
/// shared_inbox: Some(uri!("https://example.com")),
/// let endpoints: Endpoints<IriString> = Endpoints {
/// shared_inbox: Some(iri!("https://example.com")),
/// ..Default::default()
/// };
///
/// let endpoint_strings = endpoints.map(|u| u.to_string());
///
/// assert_eq!(endpoint_strings.shared_inbox, Some(String::from("https://example.com/")));
/// assert_eq!(endpoint_strings.shared_inbox, Some(String::from("https://example.com")));
/// # Ok(())
/// # }
/// ```

View file

@ -8,12 +8,12 @@
//! object::Video,
//! prelude::*,
//! security,
//! uri,
//! iri,
//! };
//! let mut video = Video::new();
//!
//! video
//! .set_id(uri!("https://example.com"))
//! .set_id(iri!("https://example.com"))
//! .set_context(context())
//! .add_context(security())
//! .set_name("Hello");
@ -29,13 +29,12 @@
//! ```
use crate::{
either::Either,
error::DomainError,
markers,
primitives::{AnyString, MimeMediaType, OneOrMany},
unparsed::{Unparsed, UnparsedMut},
};
use iri_string::types::IriString;
use mime::Mime;
use url::Url;
/// Implements conversion between `Base<Kind>` and other ActivityStreams objects defined in this
/// crate
@ -246,34 +245,6 @@ pub trait BaseExt<Kind>: AsBase<Kind> {
self
}
/// Fetch the id for the current object, checking it against the provided domain
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{object::Video, uri};
/// # let mut video = Video::new();
/// # video.set_id(uri!("https://example.com"));
/// use activitystreams::prelude::*;
///
/// assert_eq!(video.id("example.com")?, Some(&uri!("https://example.com")));
/// # Ok(())
/// # }
/// ```
fn id<'a>(&'a self, domain: &str) -> Result<Option<&'a Url>, DomainError>
where
Kind: 'a,
{
if let Some(unchecked) = self.id_unchecked() {
if unchecked.domain() != Some(domain) {
return Err(DomainError);
}
return Ok(Some(unchecked));
}
Ok(None)
}
/// Fetch the id for the current object
///
/// ```rust
@ -282,11 +253,11 @@ pub trait BaseExt<Kind>: AsBase<Kind> {
/// #
/// use activitystreams::prelude::*;
///
/// if let Some(id) = video.id_unchecked() {
/// if let Some(id) = video.id() {
/// println!("{:?}", id);
/// }
/// ```
fn id_unchecked<'a>(&'a self) -> Option<&'a Url>
fn id<'a>(&'a self) -> Option<&'a IriString>
where
Kind: 'a,
{
@ -302,11 +273,10 @@ pub trait BaseExt<Kind>: AsBase<Kind> {
/// use activitystreams::prelude::*;
///
/// if let Some(id) = video.id_mut() {
/// id.set_path("/actor");
/// println!("{:?}", id);
/// }
/// ```
fn id_mut<'a>(&'a mut self) -> Option<&'a mut Url>
fn id_mut<'a>(&'a mut self) -> Option<&'a mut IriString>
where
Kind: 'a,
{
@ -317,16 +287,16 @@ pub trait BaseExt<Kind>: AsBase<Kind> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::{object::Video, prelude::*, uri};
/// use activitystreams::{object::Video, prelude::*, iri};
///
/// let video: Video = serde_json::from_str(r#"{"type":"Video","id":"https://example.com"}"#)?;
///
/// assert!(video.is_id(&uri!("https://example.com")));
/// assert!(video.is_id(&iri!("https://example.com")));
/// # Ok(())
/// # }
/// ```
fn is_id(&self, id: &Url) -> bool {
self.id_unchecked() == Some(id)
fn is_id(&self, id: &IriString) -> bool {
self.id() == Some(id)
}
/// Set the id for the current object
@ -338,13 +308,13 @@ pub trait BaseExt<Kind>: AsBase<Kind> {
/// # use activitystreams::object::Video;
/// # let mut video = Video::new();
/// #
/// use activitystreams::{prelude::*, uri};
/// use activitystreams::{prelude::*, iri};
///
/// video.set_id(uri!("https://example.com"));
/// video.set_id(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
fn set_id(&mut self, id: Url) -> &mut Self {
fn set_id(&mut self, id: IriString) -> &mut Self {
self.base_mut().id = Some(id);
self
}
@ -361,7 +331,7 @@ pub trait BaseExt<Kind>: AsBase<Kind> {
/// println!("{:?}", id);
/// }
/// ```
fn take_id(&mut self) -> Option<Url> {
fn take_id(&mut self) -> Option<IriString> {
self.base_mut().id.take()
}
@ -374,9 +344,9 @@ pub trait BaseExt<Kind>: AsBase<Kind> {
/// #
/// use activitystreams::prelude::*;
///
/// assert!(video.id_unchecked().is_some());
/// assert!(video.id().is_some());
/// video.delete_id();
/// assert!(video.id_unchecked().is_none());
/// assert!(video.id().is_none());
/// ```
fn delete_id(&mut self) -> &mut Self {
self.base_mut().id = None;
@ -699,10 +669,10 @@ pub trait BaseExt<Kind>: AsBase<Kind> {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{object::Video, uri};
/// # use activitystreams::{object::Video, iri};
/// # let mut video = Video::new();
///
/// video.set_preview(uri!("https://example.com"));
/// video.set_preview(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
@ -721,12 +691,12 @@ pub trait BaseExt<Kind>: AsBase<Kind> {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{object::Video, uri};
/// # use activitystreams::{object::Video, iri};
/// # let mut video = Video::new();
///
/// video.set_many_previews(vec![
/// uri!("https://example.com/one"),
/// uri!("https://example.com/two"),
/// iri!("https://example.com/one"),
/// iri!("https://example.com/two"),
/// ]);
/// # Ok(())
/// # }
@ -748,12 +718,12 @@ pub trait BaseExt<Kind>: AsBase<Kind> {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{object::Video, uri};
/// # use activitystreams::{object::Video, iri};
/// # let mut video = Video::new();
///
/// video
/// .add_preview(uri!("https://example.com/one"))
/// .add_preview(uri!("https://example.com/two"));
/// .add_preview(iri!("https://example.com/one"))
/// .add_preview(iri!("https://example.com/two"));
/// # Ok(())
/// # }
/// ```
@ -792,9 +762,9 @@ pub trait BaseExt<Kind>: AsBase<Kind> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{object::Video, uri};
/// # use activitystreams::{object::Video, iri};
/// # let mut video = Video::new();
/// # video.set_preview(uri!("https://example.com"));
/// # video.set_preview(iri!("https://example.com"));
/// #
/// use activitystreams::prelude::*;
///
@ -812,7 +782,7 @@ pub trait BaseExt<Kind>: AsBase<Kind> {
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(transparent)]
struct IdOrBase(Either<Url, Box<Base<serde_json::Value>>>);
struct IdOrBase(Either<IriString, Box<Base<serde_json::Value>>>);
/// A type that can represent Any ActivityStreams type
///
@ -858,7 +828,7 @@ pub struct Base<Kind> {
/// When processing Activity Streams 1.0 documents and converting those to 2.0, implementations
/// ought to treat id as an alias for the JSON-LD @id key word[.]
#[serde(skip_serializing_if = "Option::is_none")]
id: Option<Url>,
id: Option<IriString>,
/// The `type` field
///
@ -1162,12 +1132,12 @@ impl AnyBase {
Ok(Base::retract(extended)?.into_generic()?.into())
}
/// Check if this object is a Url
/// Check if this object is a IriString
///
/// ```rust
/// # use activitystreams::{base::AnyBase, uri};
/// # use activitystreams::{base::AnyBase, iri};
/// # fn main() -> Result<(), anyhow::Error> {
/// let any_base = AnyBase::from_xsd_any_uri(uri!("https://example.com"));
/// let any_base = AnyBase::from_xsd_any_uri(iri!("https://example.com"));
/// assert!(any_base.is_xsd_any_uri());
/// # Ok(())
/// # }
@ -1217,9 +1187,9 @@ impl AnyBase {
/// #### Get the ID from the nested video
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{object::Video, base::AnyBase, prelude::*, uri};
/// # use activitystreams::{object::Video, base::AnyBase, prelude::*, iri};
/// # let mut video = Video::new();
/// let id = uri!("https://example.com");
/// let id = iri!("https://example.com");
///
/// video.set_id(id.clone());
///
@ -1232,15 +1202,15 @@ impl AnyBase {
/// #### Get the ID from the AnyBase
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{base::AnyBase, prelude::*, uri};
/// let id = uri!("https://example.com");
/// # use activitystreams::{base::AnyBase, prelude::*, iri};
/// let id = iri!("https://example.com");
///
/// let any_base = AnyBase::from_xsd_any_uri(id.clone());
/// assert!(any_base.id().unwrap() == &id);
/// # Ok(())
/// # }
/// ```
pub fn id(&self) -> Option<&Url> {
pub fn id(&self) -> Option<&IriString> {
self.as_xsd_any_uri()
.or_else(|| self.as_base().and_then(|base| base.id.as_ref()))
}
@ -1250,19 +1220,19 @@ impl AnyBase {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{
/// # object::{kind::VideoType, Video}, base::AnyBase, prelude::*, uri
/// # object::{kind::VideoType, Video}, base::AnyBase, prelude::*, iri,
/// # };
/// # let mut video = Video::new();
/// #
/// video.set_id(uri!("https://example.com"));
/// video.set_id(iri!("https://example.com"));
///
/// let any_base = AnyBase::from_extended(video)?;
///
/// assert!(any_base.is_id(&uri!("https://example.com")));
/// assert!(any_base.is_id(&iri!("https://example.com")));
/// # Ok(())
/// # }
/// ```
pub fn is_id(&self, id: &Url) -> bool {
pub fn is_id(&self, id: &IriString) -> bool {
self.id() == Some(id)
}
@ -1340,20 +1310,20 @@ impl AnyBase {
self.kind_str() == Some(kind)
}
/// Get the object as a Url
/// Get the object as a IriString
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{base::AnyBase, uri};
/// # use activitystreams::{base::AnyBase, iri};
/// #
/// let any_base = AnyBase::from_xsd_any_uri(uri!("https://example.com"));
/// let any_base = AnyBase::from_xsd_any_uri(iri!("https://example.com"));
///
/// assert!(any_base.as_xsd_any_uri().is_some());
/// #
/// # Ok(())
/// # }
/// ```
pub fn as_xsd_any_uri(&self) -> Option<&Url> {
pub fn as_xsd_any_uri(&self) -> Option<&IriString> {
self.0.as_ref().left().and_then(|l| l.as_xsd_any_uri())
}
@ -1388,20 +1358,20 @@ impl AnyBase {
self.0.as_ref().left().and_then(|l| l.as_base())
}
/// Take the Url from the Object
/// Take the IriString from the Object
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{base::AnyBase, uri};
/// # use activitystreams::{base::AnyBase, iri};
/// #
/// let any_base = AnyBase::from_xsd_any_uri(uri!("https://example.com"));
/// let any_base = AnyBase::from_xsd_any_uri(iri!("https://example.com"));
///
/// assert!(any_base.take_xsd_any_uri().is_some());
/// #
/// # Ok(())
/// # }
/// ```
pub fn take_xsd_any_uri(self) -> Option<Url> {
pub fn take_xsd_any_uri(self) -> Option<IriString> {
self.0.left().and_then(|l| l.id())
}
@ -1436,22 +1406,22 @@ impl AnyBase {
self.0.left().and_then(|l| l.base())
}
/// Replace the object with the provided Url
/// Replace the object with the provided IriString
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{base::AnyBase, uri};
/// # use activitystreams::{base::AnyBase, iri};
/// #
/// let mut any_base = AnyBase::from_xsd_string("hi".into());
///
/// any_base.set_xsd_any_uri(uri!("https://example.com"));
/// any_base.set_xsd_any_uri(iri!("https://example.com"));
///
/// assert!(any_base.take_xsd_any_uri().is_some());
/// #
/// # Ok(())
/// # }
/// ```
pub fn set_xsd_any_uri(&mut self, id: Url) {
pub fn set_xsd_any_uri(&mut self, id: IriString) {
self.0 = Either::Left(IdOrBase::from_xsd_any_uri(id));
}
@ -1459,9 +1429,9 @@ impl AnyBase {
///
/// ```
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{base::AnyBase, uri};
/// # use activitystreams::{base::AnyBase, iri};
/// #
/// let mut any_base = AnyBase::from_xsd_any_uri(uri!("https://example.com"));
/// let mut any_base = AnyBase::from_xsd_any_uri(iri!("https://example.com"));
///
/// any_base.set_xsd_string("hi");
///
@ -1497,16 +1467,16 @@ impl AnyBase {
self.0 = Either::Left(IdOrBase::from_base(base));
}
/// Create an AnyBase from a Url
/// Create an AnyBase from a IriString
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::{base::AnyBase, uri};
/// let any_base = AnyBase::from_xsd_any_uri(uri!("https://example.com"));
/// use activitystreams::{base::AnyBase, iri};
/// let any_base = AnyBase::from_xsd_any_uri(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
pub fn from_xsd_any_uri(id: Url) -> Self {
pub fn from_xsd_any_uri(id: IriString) -> Self {
AnyBase(Either::Left(IdOrBase::from_xsd_any_uri(id)))
}
@ -1539,7 +1509,7 @@ impl AnyBase {
}
impl IdOrBase {
fn as_xsd_any_uri(&self) -> Option<&Url> {
fn as_xsd_any_uri(&self) -> Option<&IriString> {
self.0.as_ref().left()
}
@ -1547,7 +1517,7 @@ impl IdOrBase {
self.0.as_ref().right().map(|b| b.as_ref())
}
fn id(self) -> Option<Url> {
fn id(self) -> Option<IriString> {
self.0.left()
}
@ -1555,7 +1525,7 @@ impl IdOrBase {
self.0.right().map(|b| *b)
}
fn from_xsd_any_uri(id: Url) -> Self {
fn from_xsd_any_uri(id: IriString) -> Self {
IdOrBase(Either::Left(id))
}
@ -1569,9 +1539,9 @@ impl OneOrMany<AnyBase> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{base::{Base, BaseExt}, primitives::OneOrMany, uri};
/// # use activitystreams::{base::{Base, BaseExt}, primitives::OneOrMany, iri};
/// # let mut base = Base::<String>::new();
/// # let id = uri!("https://example.com");
/// # let id = iri!("https://example.com");
/// # base.set_id(id.clone());
/// # let base = OneOrMany::from_base(base.into_generic()?.into());
/// #
@ -1579,7 +1549,7 @@ impl OneOrMany<AnyBase> {
/// # Ok(())
/// # }
/// ```
pub fn as_single_id(&self) -> Option<&Url> {
pub fn as_single_id(&self) -> Option<&IriString> {
self.as_one().and_then(|one| one.id())
}
@ -1587,9 +1557,9 @@ impl OneOrMany<AnyBase> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{base::{Base, BaseExt}, primitives::OneOrMany, uri};
/// # use activitystreams::{base::{Base, BaseExt}, primitives::OneOrMany, iri};
/// # let mut base = Base::<String>::new();
/// # let id = uri!("https://example.com");
/// # let id = iri!("https://example.com");
/// # base.set_id(id.clone());
/// # let base = OneOrMany::from_base(base.into_generic()?.into());
/// #
@ -1597,7 +1567,7 @@ impl OneOrMany<AnyBase> {
/// # Ok(())
/// # }
/// ```
pub fn is_single_id(&self, id: &Url) -> bool {
pub fn is_single_id(&self, id: &IriString) -> bool {
self.as_single_id() == Some(id)
}
@ -1655,20 +1625,20 @@ impl OneOrMany<AnyBase> {
self.as_single_kind_str() == Some(kind)
}
/// Get a single Url from the object, if that is what is contained
/// Get a single IriString from the object, if that is what is contained
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{primitives::OneOrMany, uri};
/// # use activitystreams::{primitives::OneOrMany, iri};
/// #
/// let one = OneOrMany::from_xsd_any_uri(uri!("https://example.com"));
/// let one = OneOrMany::from_xsd_any_uri(iri!("https://example.com"));
///
/// assert!(one.as_single_xsd_any_uri().is_some());
/// #
/// # Ok(())
/// # }
/// ```
pub fn as_single_xsd_any_uri(&self) -> Option<&Url> {
pub fn as_single_xsd_any_uri(&self) -> Option<&IriString> {
self.as_one().and_then(|inner| inner.as_xsd_any_uri())
}
@ -1699,20 +1669,20 @@ impl OneOrMany<AnyBase> {
self.as_one().and_then(|inner| inner.as_base())
}
/// Take a single Url from the object, if that is what is contained
/// Take a single IriString from the object, if that is what is contained
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{primitives::OneOrMany, uri};
/// # use activitystreams::{primitives::OneOrMany, iri};
/// #
/// let one = OneOrMany::from_xsd_any_uri(uri!("https://example.com"));
/// let one = OneOrMany::from_xsd_any_uri(iri!("https://example.com"));
///
/// assert!(one.single_xsd_any_uri().is_some());
/// #
/// # Ok(())
/// # }
/// ```
pub fn single_xsd_any_uri(self) -> Option<Url> {
pub fn single_xsd_any_uri(self) -> Option<IriString> {
self.one().and_then(|inner| inner.take_xsd_any_uri())
}
@ -1743,17 +1713,17 @@ impl OneOrMany<AnyBase> {
self.one().and_then(|inner| inner.take_base())
}
/// Create a `OneOrMany<AnyBase>` from a Url
/// Create a `OneOrMany<AnyBase>` from a IriString
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::{primitives::OneOrMany, uri};
/// use activitystreams::{primitives::OneOrMany, iri};
///
/// let one = OneOrMany::from_xsd_any_uri(uri!("https://example.com"));
/// let one = OneOrMany::from_xsd_any_uri(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
pub fn from_xsd_any_uri(id: Url) -> Self {
pub fn from_xsd_any_uri(id: IriString) -> Self {
OneOrMany(Either::Left([AnyBase::from_xsd_any_uri(id)]))
}
@ -1782,7 +1752,7 @@ impl OneOrMany<AnyBase> {
OneOrMany(Either::Left([AnyBase::from_base(base)]))
}
/// Overwrite the current object with a Url
/// Overwrite the current object with a IriString
///
/// ```rust
/// # use activitystreams::{base::Base, context, primitives::OneOrMany};
@ -1794,7 +1764,7 @@ impl OneOrMany<AnyBase> {
///
/// assert!(one.as_single_xsd_any_uri().is_some());
/// ```
pub fn set_single_xsd_any_uri(&mut self, id: Url) -> &mut Self {
pub fn set_single_xsd_any_uri(&mut self, id: IriString) -> &mut Self {
self.0 = Either::Left([AnyBase::from_xsd_any_uri(id)]);
self
}
@ -1833,7 +1803,7 @@ impl OneOrMany<AnyBase> {
self
}
/// Append a Url to the current object
/// Append a IriString to the current object
///
/// ```rust
/// use activitystreams::{base::AnyBase, context, primitives::OneOrMany, security};
@ -1843,7 +1813,7 @@ impl OneOrMany<AnyBase> {
/// many.add_xsd_any_uri(security())
/// .add_xsd_any_uri(context());
/// ```
pub fn add_xsd_any_uri(&mut self, id: Url) -> &mut Self {
pub fn add_xsd_any_uri(&mut self, id: IriString) -> &mut Self {
self.add(AnyBase::from_xsd_any_uri(id))
}
@ -1922,8 +1892,8 @@ impl From<Base<serde_json::Value>> for AnyBase {
}
}
impl From<Url> for AnyBase {
fn from(id: Url) -> Self {
impl From<IriString> for AnyBase {
fn from(id: IriString) -> Self {
Self::from_xsd_any_uri(id)
}
}
@ -1940,8 +1910,8 @@ impl From<Base<serde_json::Value>> for OneOrMany<AnyBase> {
}
}
impl From<Url> for OneOrMany<AnyBase> {
fn from(xsd_any_uri: Url) -> Self {
impl From<IriString> for OneOrMany<AnyBase> {
fn from(xsd_any_uri: IriString) -> Self {
Self::from_xsd_any_uri(xsd_any_uri)
}
}

View file

@ -6,18 +6,18 @@
//! collection::OrderedCollection,
//! context,
//! prelude::*,
//! uri,
//! iri,
//! };
//!
//! let mut collection = OrderedCollection::new();
//!
//! collection
//! .set_item(uri!("https://example.com/notes/1234"))
//! .set_item(iri!("https://example.com/notes/1234"))
//! .set_total_items(1u64)
//! .set_current(uri!("https://example.com/notes/1234"))
//! .set_first(uri!("https://example.com/notes/1234"))
//! .set_last(uri!("https://example.com/notes/1234"))
//! .set_id(uri!("https://example.com/collections/1234"))
//! .set_current(iri!("https://example.com/notes/1234"))
//! .set_first(iri!("https://example.com/notes/1234"))
//! .set_last(iri!("https://example.com/notes/1234"))
//! .set_id(iri!("https://example.com/collections/1234"))
//! .set_context(context());
//! # Ok(())
//! # }
@ -92,10 +92,10 @@ pub trait CollectionExt<Kind>: AsCollection<Kind> {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{collection::UnorderedCollection, uri};
/// # use activitystreams::{collection::UnorderedCollection, iri};
/// # let mut collection = UnorderedCollection::new();
///
/// collection.set_item(uri!("https://example.com"));
/// collection.set_item(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
@ -114,12 +114,12 @@ pub trait CollectionExt<Kind>: AsCollection<Kind> {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{collection::UnorderedCollection, uri};
/// # use activitystreams::{collection::UnorderedCollection, iri};
/// # let mut collection = UnorderedCollection::new();
///
/// collection.set_many_items(vec![
/// uri!("https://example.com/one"),
/// uri!("https://example.com/two"),
/// iri!("https://example.com/one"),
/// iri!("https://example.com/two"),
/// ]);
/// # Ok(())
/// # }
@ -141,12 +141,12 @@ pub trait CollectionExt<Kind>: AsCollection<Kind> {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{collection::UnorderedCollection, uri};
/// # use activitystreams::{collection::UnorderedCollection, iri};
/// # let mut collection = UnorderedCollection::new();
///
/// collection
/// .add_item(uri!("https://example.com/one"))
/// .add_item(uri!("https://example.com/two"));
/// .add_item(iri!("https://example.com/one"))
/// .add_item(iri!("https://example.com/two"));
/// # Ok(())
/// # }
/// ```
@ -221,10 +221,10 @@ pub trait CollectionExt<Kind>: AsCollection<Kind> {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{collection::OrderedCollection, uri};
/// # use activitystreams::{collection::OrderedCollection, iri};
/// # let mut collection = OrderedCollection::new();
///
/// collection.set_ordered_item(uri!("https://example.com"));
/// collection.set_ordered_item(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
@ -243,12 +243,12 @@ pub trait CollectionExt<Kind>: AsCollection<Kind> {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{collection::OrderedCollection, uri};
/// # use activitystreams::{collection::OrderedCollection, iri};
/// # let mut collection = OrderedCollection::new();
///
/// collection.set_many_ordered_items(vec![
/// uri!("https://example.com/one"),
/// uri!("https://example.com/two"),
/// iri!("https://example.com/one"),
/// iri!("https://example.com/two"),
/// ]);
/// # Ok(())
/// # }
@ -270,12 +270,12 @@ pub trait CollectionExt<Kind>: AsCollection<Kind> {
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams::prelude::*;
/// # use activitystreams::{collection::OrderedCollection, uri};
/// # use activitystreams::{collection::OrderedCollection, iri};
/// # let mut collection = OrderedCollection::new();
///
/// collection
/// .add_ordered_item(uri!("https://example.com/one"))
/// .add_ordered_item(uri!("https://example.com/two"));
/// .add_ordered_item(iri!("https://example.com/one"))
/// .add_ordered_item(iri!("https://example.com/two"));
/// # Ok(())
/// # }
/// ```
@ -419,12 +419,12 @@ pub trait CollectionExt<Kind>: AsCollection<Kind> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{collection::UnorderedCollection, uri};
/// # use activitystreams::{collection::UnorderedCollection, iri};
/// # let mut collection = UnorderedCollection::new();
/// #
/// use activitystreams::prelude::*;
///
/// collection.set_current(uri!("https://example.com"));
/// collection.set_current(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
@ -498,9 +498,9 @@ pub trait CollectionExt<Kind>: AsCollection<Kind> {
/// # use activitystreams::{collection::UnorderedCollection};
/// # let mut collection = UnorderedCollection::new();
/// #
/// use activitystreams::{prelude::*, uri};
/// use activitystreams::{prelude::*, iri};
///
/// collection.set_first(uri!("https://example.com"));
/// collection.set_first(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
@ -574,9 +574,9 @@ pub trait CollectionExt<Kind>: AsCollection<Kind> {
/// # use activitystreams::{collection::UnorderedCollection};
/// # let mut collection = UnorderedCollection::new();
/// #
/// use activitystreams::{prelude::*, uri};
/// use activitystreams::{prelude::*, iri};
///
/// collection.set_last(uri!("https://example.com"));
/// collection.set_last(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
@ -658,9 +658,9 @@ pub trait CollectionPageExt<Kind>: AsCollectionPage<Kind> {
/// # use activitystreams::{collection::UnorderedCollectionPage};
/// # let mut collection = UnorderedCollectionPage::new();
/// #
/// use activitystreams::{prelude::*, uri};
/// use activitystreams::{prelude::*, iri};
///
/// collection.set_part_of(uri!("https://example.com"));
/// collection.set_part_of(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
@ -734,9 +734,9 @@ pub trait CollectionPageExt<Kind>: AsCollectionPage<Kind> {
/// # use activitystreams::{collection::UnorderedCollectionPage};
/// # let mut collection = UnorderedCollectionPage::new();
/// #
/// use activitystreams::{prelude::*, uri};
/// use activitystreams::{prelude::*, iri};
///
/// collection.set_next(uri!("https://example.com"));
/// collection.set_next(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
@ -810,9 +810,9 @@ pub trait CollectionPageExt<Kind>: AsCollectionPage<Kind> {
/// # use activitystreams::{collection::UnorderedCollectionPage};
/// # let mut collection = UnorderedCollectionPage::new();
/// #
/// use activitystreams::{prelude::*, uri};
/// use activitystreams::{prelude::*, iri};
///
/// collection.set_prev(uri!("https://example.com"));
/// collection.set_prev(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```

View file

@ -1,3 +0,0 @@
#[derive(Clone, Debug, thiserror::Error)]
#[error("URL did not match expected domain")]
pub struct DomainError;

View file

@ -143,9 +143,9 @@
//!
//! For fields with more specific bounds, like `id`,
//! ```rust,ignore
//! fn id(&self) -> Option<&Url>;
//! fn set_id(&mut self, Url) -> &mut Self;
//! fn take_id(&self) -> Option<Url>;
//! fn id(&self) -> Option<&IriString>;
//! fn set_id(&mut self, IriString) -> &mut Self;
//! fn take_id(&self) -> Option<IriString>;
//! fn delete_id(&mut self) -> &mut Self;
//! ```
//!
@ -164,14 +164,14 @@
//! bound the function like so:
//!
//! ```rust
//! use activitystreams::{base::BaseExt, context, markers::Activity, uri};
//! use activitystreams::{base::BaseExt, context, markers::Activity, iri};
//!
//! fn manipulator<T, Kind>(mut activity: T) -> Result<(), anyhow::Error>
//! where
//! T: Activity + BaseExt<Kind>,
//! {
//! activity
//! .set_id(uri!("https://example.com"))
//! .set_id(iri!("https://example.com"))
//! .set_context(context());
//! Ok(())
//! }
@ -205,21 +205,21 @@
//! context,
//! object::{ApObject, Video},
//! prelude::*,
//! uri,
//! iri,
//! };
//! use chrono::Duration;
//! use time::Duration;
//!
//! fn main() -> Result<(), anyhow::Error> {
//! let mut video = ApObject::new(Video::new());
//!
//! video
//! .set_context(context())
//! .set_id(uri!("https://example.com/@example/lions"))
//! .set_id(iri!("https://example.com/@example/lions"))
//! .set_media_type("video/webm".parse()?)
//! .set_url(uri!("https://example.com/@example/lions/video.webm"))
//! .set_url(iri!("https://example.com/@example/lions/video.webm"))
//! .set_summary("A cool video")
//! .set_duration(Duration::minutes(4) + Duration::seconds(20))
//! .set_shares(uri!("https://example.com/@example/lions/video.webm#shares"));
//! .set_shares(iri!("https://example.com/@example/lions/video.webm#shares"));
//!
//! println!("Video, {:#?}", video);
//!
@ -300,7 +300,6 @@ pub mod actor;
pub mod base;
pub mod collection;
mod either;
pub mod error;
pub mod link;
mod macros;
pub mod markers;
@ -308,11 +307,13 @@ pub mod object;
pub mod primitives;
pub mod unparsed;
pub extern crate chrono;
pub extern crate iri_string;
pub extern crate mime;
pub extern crate url;
pub extern crate time;
pub use activitystreams_kinds::{context, kind, public, security};
pub use activitystreams_kinds::{
context_iri as context, kind, public_iri as public, security_iri as security,
};
pub mod prelude {
//! Extension traits that provide the majority of the helper methods of the crate
@ -327,38 +328,38 @@ pub mod prelude {
//! public,
//! object::{ApObject, Image, Video},
//! security,
//! uri,
//! iri,
//! };
//! use chrono::Duration;
//! use time::Duration;
//!
//! let mut person = ApActor::new(
//! uri!("http://localhost:8080/inbox"),
//! iri!("http://localhost:8080/inbox"),
//! Person::new(),
//! );
//! person
//! .set_outbox(uri!("http:/localhost:8080/outbox"))
//! .set_outbox(iri!("http:/localhost:8080/outbox"))
//! .set_name("Demo Account")
//! .set_preferred_username("demo")
//! .set_id(uri!("https://localhost:8080/actor"))
//! .set_url(uri!("https://localhost:8080/actor"));
//! .set_id(iri!("https://localhost:8080/actor"))
//! .set_url(iri!("https://localhost:8080/actor"));
//!
//! let mut preview = Image::new();
//!
//! preview
//! .set_url(uri!("https://localhost:8080/preview.png"))
//! .set_url(iri!("https://localhost:8080/preview.png"))
//! .set_media_type("image/png".parse()?)
//! .set_id(uri!("https://localhostst:8080/preview.png"));
//! .set_id(iri!("https://localhostst:8080/preview.png"));
//!
//! let mut video = ApObject::new(Video::new());
//!
//! video
//! .set_id(uri!("http://localhost:8080/video.webm"))
//! .set_url(uri!("http://localhost:8080/video.webm"))
//! .set_id(iri!("http://localhost:8080/video.webm"))
//! .set_url(iri!("http://localhost:8080/video.webm"))
//! .set_media_type("video/webm".parse()?)
//! .set_summary("A cool video")
//! .set_preview(preview.into_any_base()?)
//! .set_duration(Duration::minutes(4) + Duration::seconds(20))
//! .set_shares(uri!("http://localhost:8080/video.webm#shares"));
//! .set_shares(iri!("http://localhost:8080/video.webm#shares"));
//!
//! let mut activity = Create::new(
//! person.into_any_base()?,

View file

@ -6,13 +6,13 @@
//! link::Mention,
//! object::Image,
//! prelude::*,
//! uri,
//! iri,
//! };
//!
//! let mut mention = Mention::new();
//!
//! mention
//! .set_href(uri!("https://example.com"))
//! .set_href(iri!("https://example.com"))
//! .set_hreflang("en")
//! .set_rel("link")
//! .set_preview(Image::new().into_any_base()?);
@ -26,8 +26,8 @@ use crate::{
primitives::OneOrMany,
unparsed::{Unparsed, UnparsedMut, UnparsedMutExt},
};
use iri_string::types::IriString;
use std::convert::TryFrom;
use url::Url;
pub use activitystreams_kinds::link as kind;
@ -60,7 +60,7 @@ pub trait LinkExt<Kind>: AsLink<Kind> {
///
/// let mention_href = mention.href();
/// ```
fn href<'a>(&'a self) -> Option<&'a Url>
fn href<'a>(&'a self) -> Option<&'a IriString>
where
Kind: 'a,
{
@ -73,16 +73,16 @@ pub trait LinkExt<Kind>: AsLink<Kind> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{link::Mention, uri};
/// # use activitystreams::{link::Mention, iri};
/// # let mut mention = Mention::new();
/// #
/// use activitystreams::prelude::*;
///
/// mention.set_href(uri!("https://example.com"));
/// mention.set_href(iri!("https://example.com"));
/// # Ok(())
/// # }
/// ```
fn set_href(&mut self, href: Url) -> &mut Self {
fn set_href(&mut self, href: IriString) -> &mut Self {
self.link_mut().href = Some(href);
self
}
@ -99,7 +99,7 @@ pub trait LinkExt<Kind>: AsLink<Kind> {
/// println!("{:?}", href);
/// }
/// ```
fn take_href(&mut self) -> Option<Url> {
fn take_href(&mut self) -> Option<IriString> {
self.link_mut().href.take()
}
@ -107,9 +107,9 @@ pub trait LinkExt<Kind>: AsLink<Kind> {
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// # use activitystreams::{link::Mention, uri};
/// # use activitystreams::{link::Mention, iri};
/// # let mut mention = Mention::new();
/// # mention.set_href(uri!("https://example.com"));
/// # mention.set_href(iri!("https://example.com"));
/// #
/// use activitystreams::prelude::*;
///
@ -493,7 +493,7 @@ pub struct Link<Kind> {
/// - Range: xsd:anyUri
/// - Functional: true
#[serde(skip_serializing_if = "Option::is_none")]
href: Option<Url>,
href: Option<IriString>,
/// Hints as to the language used by the target resource.
///

View file

@ -1,20 +1,58 @@
/// A macro to shorten the `string.parse::<Url>()?` calls inevitably made in downstream code
///
/// ```rust
/// use activitystreams::uri;
/// use activitystreams::iri;
///
/// fn fallible() -> Result<(), anyhow::Error> {
/// let my_uri = uri!("https://example.com");
/// let my_iri = iri!("https://example.com");
/// Ok(())
/// }
///
/// # fn main() -> Result<(), anyhow::Error> { fallible() }
/// ```
#[macro_export]
macro_rules! uri {
macro_rules! iri {
( $x:expr ) => {{
use activitystreams::url::Url;
use activitystreams::iri_string::types::IriString;
$x.parse::<Url>()?
$x.parse::<IriString>()?
}};
}
/// A macro to parse IRI fragments
///
/// ```rust
/// use activitystreams::fragment;
///
/// fn fallible() -> Result<(), anyhow::Error> {
/// let my_fragment = fragment!("main-key");
/// Ok(())
/// }
/// ```
#[macro_export]
macro_rules! fragment {
( $x:expr ) => {{
use activitystreams::iri_string::types::IriFragmentString;
$x.parse::<IriFragmentString>()?
}};
}
/// A macro to parse Rfc3339 datetimes
///
/// ```
/// use activitystreams::datetime;
///
/// fn fallible() -> Result<(), anyhow::Error> {
/// let my_datetime = datetime!("2020-04-20T04:20:00Z");
/// Ok(())
/// }
/// ```
#[macro_export]
macro_rules! datetime {
( $x:expr ) => {{
use activitystreams::time::{format_description::well_known::Rfc3339, OffsetDateTime};
OffsetDateTime::parse($x, &Rfc3339)?
}};
}

File diff suppressed because it is too large Load diff

View file

@ -31,56 +31,56 @@
/// UTC, is represented as -05:00. If no time zone value is present, it is considered unknown; it
/// is not assumed to be UTC.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct XsdDateTime(pub chrono::DateTime<chrono::FixedOffset>);
pub struct XsdDateTime(pub time::OffsetDateTime);
impl XsdDateTime {
/// Create a XsdDateTime from a chrono::DateTime
pub fn new(d: chrono::DateTime<chrono::FixedOffset>) -> Self {
/// Create a XsdDateTime from a time::OffsetDateTime
pub fn new(d: time::OffsetDateTime) -> Self {
XsdDateTime(d)
}
/// Extract the chrono::DateTime from XsdDateTime
pub fn into_inner(self) -> chrono::DateTime<chrono::FixedOffset> {
/// Extract the time::OffsetDateTime from XsdDateTime
pub fn into_inner(self) -> time::OffsetDateTime {
self.0
}
/// Borrow the underlying `chrono::DateTime<chrono::FixedOffset>`
pub fn as_datetime(&self) -> &chrono::DateTime<chrono::FixedOffset> {
/// Borrow the underlying `time::OffsetDateTime`
pub fn as_datetime(&self) -> &time::OffsetDateTime {
self.as_ref()
}
/// Mutably borrow the underlying `chrono::DateTime<chrono::FixedOffset>`
pub fn as_datetime_mut(&mut self) -> &mut chrono::DateTime<chrono::FixedOffset> {
/// Mutably borrow the underlying `time::OffsetDateTime`
pub fn as_datetime_mut(&mut self) -> &mut time::OffsetDateTime {
self.as_mut()
}
}
impl From<chrono::DateTime<chrono::FixedOffset>> for XsdDateTime {
fn from(d: chrono::DateTime<chrono::FixedOffset>) -> Self {
impl From<time::OffsetDateTime> for XsdDateTime {
fn from(d: time::OffsetDateTime) -> Self {
XsdDateTime(d)
}
}
impl From<XsdDateTime> for chrono::DateTime<chrono::FixedOffset> {
impl From<XsdDateTime> for time::OffsetDateTime {
fn from(d: XsdDateTime) -> Self {
d.0
}
}
impl AsRef<chrono::DateTime<chrono::FixedOffset>> for XsdDateTime {
fn as_ref(&self) -> &chrono::DateTime<chrono::FixedOffset> {
impl AsRef<time::OffsetDateTime> for XsdDateTime {
fn as_ref(&self) -> &time::OffsetDateTime {
&self.0
}
}
impl AsMut<chrono::DateTime<chrono::FixedOffset>> for XsdDateTime {
fn as_mut(&mut self) -> &mut chrono::DateTime<chrono::FixedOffset> {
impl AsMut<time::OffsetDateTime> for XsdDateTime {
fn as_mut(&mut self) -> &mut time::OffsetDateTime {
&mut self.0
}
}
impl std::convert::TryFrom<String> for XsdDateTime {
type Error = chrono::format::ParseError;
type Error = time::error::Parse;
fn try_from(s: String) -> Result<Self, Self::Error> {
s.parse()
@ -88,7 +88,7 @@ impl std::convert::TryFrom<String> for XsdDateTime {
}
impl std::convert::TryFrom<&str> for XsdDateTime {
type Error = chrono::format::ParseError;
type Error = time::error::Parse;
fn try_from(s: &str) -> Result<Self, Self::Error> {
s.parse()
@ -96,7 +96,7 @@ impl std::convert::TryFrom<&str> for XsdDateTime {
}
impl std::convert::TryFrom<&mut str> for XsdDateTime {
type Error = chrono::format::ParseError;
type Error = time::error::Parse;
fn try_from(s: &mut str) -> Result<Self, Self::Error> {
s.parse()
@ -104,16 +104,22 @@ impl std::convert::TryFrom<&mut str> for XsdDateTime {
}
impl std::str::FromStr for XsdDateTime {
type Err = chrono::format::ParseError;
type Err = time::error::Parse;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(XsdDateTime(chrono::DateTime::parse_from_rfc3339(s)?))
Ok(XsdDateTime(time::OffsetDateTime::parse(
s,
&time::format_description::well_known::Rfc3339,
)?))
}
}
impl std::fmt::Display for XsdDateTime {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let s = self.0.to_rfc3339();
let s = self
.0
.format(&time::format_description::well_known::Rfc3339)
.map_err(|_| std::fmt::Error)?;
std::fmt::Display::fmt(&s, f)
}
}

View file

@ -42,7 +42,7 @@
/// multiplying by 365. If this is an issue for your application, look into specifying days
/// directly.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct XsdDuration(pub chrono::Duration);
pub struct XsdDuration(pub time::Duration);
/// The error type produced when an XsdDuration cannot be parsed
#[derive(Clone, Debug, thiserror::Error)]
@ -50,47 +50,47 @@ pub struct XsdDuration(pub chrono::Duration);
pub struct XsdDurationError;
impl XsdDuration {
/// Create a new XsdDuration from a chrono::Duration
pub fn new(duration: chrono::Duration) -> Self {
/// Create a new XsdDuration from a time::Duration
pub fn new(duration: time::Duration) -> Self {
XsdDuration(duration)
}
/// Extract the chrono::Duration from an XsdDuration
pub fn into_inner(self) -> chrono::Duration {
/// Extract the time::Duration from an XsdDuration
pub fn into_inner(self) -> time::Duration {
self.0
}
/// Borrow the underlying `chrono::Duration`
pub fn as_duration(&self) -> &chrono::Duration {
/// Borrow the underlying `time::Duration`
pub fn as_duration(&self) -> &time::Duration {
self.as_ref()
}
/// Mutably borrow the underlying `chrono::Duration`
pub fn as_duration_mut(&mut self) -> &mut chrono::Duration {
/// Mutably borrow the underlying `time::Duration`
pub fn as_duration_mut(&mut self) -> &mut time::Duration {
self.as_mut()
}
}
impl From<chrono::Duration> for XsdDuration {
fn from(d: chrono::Duration) -> Self {
impl From<time::Duration> for XsdDuration {
fn from(d: time::Duration) -> Self {
XsdDuration(d)
}
}
impl From<XsdDuration> for chrono::Duration {
impl From<XsdDuration> for time::Duration {
fn from(d: XsdDuration) -> Self {
d.0
}
}
impl AsRef<chrono::Duration> for XsdDuration {
fn as_ref(&self) -> &chrono::Duration {
impl AsRef<time::Duration> for XsdDuration {
fn as_ref(&self) -> &time::Duration {
&self.0
}
}
impl AsMut<chrono::Duration> for XsdDuration {
fn as_mut(&mut self) -> &mut chrono::Duration {
impl AsMut<time::Duration> for XsdDuration {
fn as_mut(&mut self) -> &mut time::Duration {
&mut self.0
}
}
@ -147,12 +147,12 @@ impl std::str::FromStr for XsdDuration {
let (minutes, small) = parse_next(small, 'M')?;
let (seconds, _) = parse_next(small, 'S')?;
let mut duration = chrono::Duration::days(365 * years);
duration = duration + chrono::Duration::days(31 * months);
duration = duration + chrono::Duration::days(days);
duration = duration + chrono::Duration::hours(hours);
duration = duration + chrono::Duration::minutes(minutes);
duration = duration + chrono::Duration::seconds(seconds);
let mut duration = time::Duration::days(365 * years);
duration += time::Duration::days(31 * months);
duration += time::Duration::days(days);
duration += time::Duration::hours(hours);
duration += time::Duration::minutes(minutes);
duration += time::Duration::seconds(seconds);
duration = if negative { duration * -1 } else { duration };
@ -174,44 +174,44 @@ fn parse_next(s: &str, c: char) -> Result<(i64, &str), XsdDurationError> {
impl std::fmt::Display for XsdDuration {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let (s, mut duration) = if chrono::Duration::seconds(0) > self.0 {
let (s, mut duration) = if time::Duration::seconds(0) > self.0 {
("P-".to_string(), self.0 * -1)
} else {
("P".to_string(), self.0)
};
let s = if duration.num_days() > 0 {
format!("{}{}D", s, duration.num_days())
let s = if duration.whole_days() > 0 {
format!("{}{}D", s, duration.whole_days())
} else {
s
};
duration = duration - chrono::Duration::days(duration.num_days());
duration -= time::Duration::days(duration.whole_days());
let s = if duration.num_seconds() > 0 {
let s = if duration.whole_seconds() > 0 {
format!("{}T", s)
} else {
s
};
let s = if duration.num_hours() > 0 {
format!("{}{}H", s, duration.num_hours())
let s = if duration.whole_hours() > 0 {
format!("{}{}H", s, duration.whole_hours())
} else {
s
};
duration = duration - chrono::Duration::hours(duration.num_hours());
duration -= time::Duration::hours(duration.whole_hours());
let s = if duration.num_minutes() > 0 {
format!("{}{}M", s, duration.num_minutes())
let s = if duration.whole_minutes() > 0 {
format!("{}{}M", s, duration.whole_minutes())
} else {
s
};
duration = duration - chrono::Duration::minutes(duration.num_minutes());
duration -= time::Duration::minutes(duration.whole_minutes());
let s = if duration.num_seconds() > 0 {
format!("{}{}S", s, duration.num_seconds())
let s = if duration.whole_seconds() > 0 {
format!("{}{}S", s, duration.whole_seconds())
} else {
s
};

View file

@ -23,7 +23,7 @@
//! prelude::*,
//! primitives::*,
//! unparsed::*,
//! url::Url,
//! iri_string::types::IriString,
//! };
//!
//! /// First, we'll define our public key types
@ -31,8 +31,8 @@
//! #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
//! #[serde(rename_all = "camelCase")]
//! pub struct PublicKeyValues {
//! pub id: Url,
//! pub owner: Url,
//! pub id: IriString,
//! pub owner: IriString,
//! pub public_key_pem: String,
//! }
//!
@ -168,7 +168,7 @@
//! /// And now create helper methods
//! pub trait PublicKeyExt<Inner>: AsPublicKey<Inner> {
//! /// Borrow the public key's ID
//! fn key_id<'a>(&'a self) -> &'a Url
//! fn key_id<'a>(&'a self) -> &'a IriString
//! where
//! Inner: 'a,
//! {
@ -176,13 +176,13 @@
//! }
//!
//! /// Set the public key's ID
//! fn set_key_id(&mut self, id: Url) -> &mut Self {
//! fn set_key_id(&mut self, id: IriString) -> &mut Self {
//! self.public_key_mut().public_key.id = id;
//! self
//! }
//!
//! /// Borrow the public key's Owner
//! fn key_owner<'a>(&'a self) -> &'a Url
//! fn key_owner<'a>(&'a self) -> &'a IriString
//! where
//! Inner: 'a,
//! {
@ -190,7 +190,7 @@
//! }
//!
//! /// Set the public key's Owner
//! fn set_key_owner(&mut self, owner: Url) -> &mut Self {
//! fn set_key_owner(&mut self, owner: IriString) -> &mut Self {
//! self.public_key_mut().public_key.owner = owner;
//! self
//! }
@ -221,30 +221,30 @@
//!
//!
//! /// Now that eveything is implemented, we can use it like so:
//! use activitystreams::{actor::{kind::PersonType, Person}, uri};
//! use activitystreams::{actor::{kind::PersonType, Person}, fragment, iri};
//!
//! pub type ExtendedPerson = PublicKey<ApActor<Person>>;
//!
//! impl ExtendedPerson {
//! pub fn new(inbox: Url, mut owner: Url) -> Self {
//! pub fn new(inbox: IriString, mut owner: IriString) -> Result<Self, anyhow::Error> {
//! let id = owner.clone();
//! owner.set_fragment(Some("main-key"));
//! PublicKey {
//! owner.set_fragment(Some(fragment!("main-key").as_ref()));
//! Ok(PublicKey {
//! public_key: PublicKeyValues {
//! id,
//! owner,
//! public_key_pem: String::new(),
//! },
//! inner: ApActor::new(inbox, Person::new()),
//! }
//! })
//! }
//! }
//!
//! fn main() -> Result<(), anyhow::Error> {
//! let mut extended_person = ExtendedPerson::new(
//! uri!("https://example.com/user/inbox"),
//! uri!("https://example.com/user"),
//! );
//! iri!("https://example.com/user/inbox"),
//! iri!("https://example.com/user"),
//! )?;
//!
//! extended_person
//! .set_kind(PersonType::Person)