activitystreams/activitystreams-kinds/src/lib.rs
Aode (lion) e2aef84724
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Expose Object, Link, and Activity type values
2022-03-31 11:11:59 -05:00

217 lines
5.5 KiB
Rust

//! # activitystreams-kinds
//!
//! 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"
.parse()
.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
///
/// ```rust
/// # fn main() -> Result<(), anyhow::Error> {
/// use activitystreams_kinds::kind;
///
/// kind!(CustomType, Custom);
///
/// #[derive(serde::Deserialize)]
/// struct MyStruct {
/// #[serde(rename = "type")]
/// kind: CustomType,
/// }
///
/// let s: MyStruct = serde_json::from_str(r#"{"type":"Custom"}"#)?;
///
/// assert_eq!(s.kind, CustomType::Custom);
/// # Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! kind {
($x:ident, $y:ident) => {
#[derive(
Clone,
Debug,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd,
serde::Deserialize,
serde::Serialize,
)]
/// A type stand-in for the constant $y, deriving serde traits
pub enum $x {
$y,
}
impl std::fmt::Display for $x {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, stringify!($y))
}
}
impl Default for $x {
fn default() -> Self {
$x::$y
}
}
};
}
pub mod activity {
//! Kinds of activities defined by the spec
//!
//! These types exist only to be statically-typed versions of the associated string. e.g.
//! `CreateType` -> `"Create"`
use crate::kind;
kind!(ActivityType, Activity);
kind!(AcceptType, Accept);
kind!(AddType, Add);
kind!(AnnounceType, Announce);
kind!(ArriveType, Arrive);
kind!(BlockType, Block);
kind!(CreateType, Create);
kind!(DeleteType, Delete);
kind!(DislikeType, Dislike);
kind!(FlagType, Flag);
kind!(FollowType, Follow);
kind!(IgnoreType, Ignore);
kind!(InviteType, Invite);
kind!(JoinType, Join);
kind!(LeaveType, Leave);
kind!(LikeType, Like);
kind!(ListenType, Listen);
kind!(MoveType, Move);
kind!(OfferType, Offer);
kind!(QuestionType, Question);
kind!(ReadType, Read);
kind!(RejectType, Reject);
kind!(RemoveType, Remove);
kind!(TentativeAcceptType, TentativeAccept);
kind!(TentativeRejectType, TentativeReject);
kind!(TravelType, Travel);
kind!(UndoType, Undo);
kind!(UpdateType, Update);
kind!(ViewType, View);
}
pub mod actor {
//! Kinds of actors defined by the spec
//!
//! These types exist only to be statically-typed versions of the associated string. e.g.
//! `PersonType` -> `"Person"`
use crate::kind;
kind!(ApplicationType, Application);
kind!(GroupType, Group);
kind!(OrganizationType, Organization);
kind!(PersonType, Person);
kind!(ServiceType, Service);
}
pub mod collection {
//! Kinds of collections defined by the spec
//!
//! These types exist only to be statically-typed versions of the associated string. e.g.
//! `CollectionType` -> `"Collection"`
use crate::kind;
kind!(CollectionType, Collection);
kind!(OrderedCollectionType, OrderedCollection);
kind!(CollectionPageType, CollectionPage);
kind!(OrderedCollectionPageType, OrderedCollectionPage);
}
pub mod link {
//! Kinds of links defined by the spec
//!
//! These types exist only to be statically-typed versions of the associated string. e.g.
//! `MentionType` -> `"Mention"`
use crate::kind;
kind!(LinkType, Link);
kind!(MentionType, Mention);
}
pub mod object {
//! Kinds of objects defined by the spec
//!
//! These types exist only to be statically-typed versions of the associated string. e.g.
//! `PlaceType` -> `"Place"`
use crate::kind;
kind!(ObjectType, Object);
kind!(ArticleType, Article);
kind!(AudioType, Audio);
kind!(DocumentType, Document);
kind!(EventType, Event);
kind!(ImageType, Image);
kind!(NoteType, Note);
kind!(PageType, Page);
kind!(PlaceType, Place);
kind!(ProfileType, Profile);
kind!(RelationshipType, Relationship);
kind!(TombstoneType, Tombstone);
kind!(VideoType, Video);
}
#[cfg(test)]
mod tests {
use super::kind;
#[test]
fn to_string_works() {
kind!(MyType, My);
assert_eq!(MyType::My.to_string(), "My")
}
}