activitystreams_kinds/
lib.rs

1//! # activitystreams-kinds
2//!
3//! Enums representing typed versions of activitypub 'type' fields.
4
5#[cfg(feature = "url")]
6use url::Url;
7
8#[cfg(feature = "url")]
9/// Returns the `https://www.w3.org/ns/activitystreams` Url
10pub fn context() -> Url {
11    "https://www.w3.org/ns/activitystreams".parse().unwrap()
12}
13
14#[cfg(feature = "url")]
15/// Returns the `https://www.w3.org/ns/activitystreams#Public` Url
16pub fn public() -> Url {
17    "https://www.w3.org/ns/activitystreams#Public"
18        .parse()
19        .unwrap()
20}
21
22#[cfg(feature = "url")]
23/// Returns the `https://w3id.org/security/v1` Url
24pub fn security() -> Url {
25    "https://w3id.org/security/v1".parse().unwrap()
26}
27
28#[cfg(feature = "iri-string")]
29/// Returns the `https://www.w3.org/ns/activitystreams` IRI
30pub fn context_iri() -> iri_string::types::IriString {
31    "https://www.w3.org/ns/activitystreams".parse().unwrap()
32}
33
34#[cfg(feature = "iri-string")]
35/// Returns the `https://www.w3.org/ns/activitystreams#Public` IRI
36pub fn public_iri() -> iri_string::types::IriString {
37    "https://www.w3.org/ns/activitystreams#Public"
38        .parse()
39        .unwrap()
40}
41
42#[cfg(feature = "iri-string")]
43/// Returns the `https://w3id.org/security/v1` IRI
44pub fn security_iri() -> iri_string::types::IriString {
45    "https://w3id.org/security/v1".parse().unwrap()
46}
47
48/// Generate an enum implementing serde's Serialize and Deserialize with a single variant
49///
50/// This is useful for describing constants
51///
52/// ```rust
53/// # fn main() -> Result<(), anyhow::Error> {
54/// use activitystreams_kinds::kind;
55///
56/// kind!(CustomType, Custom);
57///
58/// #[derive(serde::Deserialize)]
59/// struct MyStruct {
60///     #[serde(rename = "type")]
61///     kind: CustomType,
62/// }
63///
64/// let s: MyStruct = serde_json::from_str(r#"{"type":"Custom"}"#)?;
65///
66/// assert_eq!(s.kind, CustomType::Custom);
67/// # Ok(())
68/// # }
69/// ```
70#[macro_export]
71macro_rules! kind {
72    ($x:ident, $y:ident) => {
73        #[derive(
74            Clone,
75            Debug,
76            Eq,
77            Hash,
78            Ord,
79            PartialEq,
80            PartialOrd,
81            serde::Deserialize,
82            serde::Serialize,
83        )]
84        /// A type stand-in for the constant $y, deriving serde traits
85        pub enum $x {
86            $y,
87        }
88
89        impl std::fmt::Display for $x {
90            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
91                write!(f, stringify!($y))
92            }
93        }
94
95        impl Default for $x {
96            fn default() -> Self {
97                $x::$y
98            }
99        }
100    };
101}
102
103pub mod activity {
104    //! Kinds of activities defined by the spec
105    //!
106    //! These types exist only to be statically-typed versions of the associated string. e.g.
107    //! `CreateType` -> `"Create"`
108
109    use crate::kind;
110
111    kind!(ActivityType, Activity);
112    kind!(AcceptType, Accept);
113    kind!(AddType, Add);
114    kind!(AnnounceType, Announce);
115    kind!(ArriveType, Arrive);
116    kind!(BlockType, Block);
117    kind!(CreateType, Create);
118    kind!(DeleteType, Delete);
119    kind!(DislikeType, Dislike);
120    kind!(FlagType, Flag);
121    kind!(FollowType, Follow);
122    kind!(IgnoreType, Ignore);
123    kind!(InviteType, Invite);
124    kind!(JoinType, Join);
125    kind!(LeaveType, Leave);
126    kind!(LikeType, Like);
127    kind!(ListenType, Listen);
128    kind!(MoveType, Move);
129    kind!(OfferType, Offer);
130    kind!(QuestionType, Question);
131    kind!(ReadType, Read);
132    kind!(RejectType, Reject);
133    kind!(RemoveType, Remove);
134    kind!(TentativeAcceptType, TentativeAccept);
135    kind!(TentativeRejectType, TentativeReject);
136    kind!(TravelType, Travel);
137    kind!(UndoType, Undo);
138    kind!(UpdateType, Update);
139    kind!(ViewType, View);
140}
141
142pub mod actor {
143    //! Kinds of actors defined by the spec
144    //!
145    //! These types exist only to be statically-typed versions of the associated string. e.g.
146    //! `PersonType` -> `"Person"`
147
148    use crate::kind;
149
150    kind!(ApplicationType, Application);
151    kind!(GroupType, Group);
152    kind!(OrganizationType, Organization);
153    kind!(PersonType, Person);
154    kind!(ServiceType, Service);
155}
156
157pub mod collection {
158    //! Kinds of collections defined by the spec
159    //!
160    //! These types exist only to be statically-typed versions of the associated string. e.g.
161    //! `CollectionType` -> `"Collection"`
162
163    use crate::kind;
164
165    kind!(CollectionType, Collection);
166    kind!(OrderedCollectionType, OrderedCollection);
167    kind!(CollectionPageType, CollectionPage);
168    kind!(OrderedCollectionPageType, OrderedCollectionPage);
169}
170
171pub mod link {
172    //! Kinds of links defined by the spec
173    //!
174    //! These types exist only to be statically-typed versions of the associated string. e.g.
175    //! `MentionType` -> `"Mention"`
176
177    use crate::kind;
178
179    kind!(LinkType, Link);
180    kind!(MentionType, Mention);
181}
182
183pub mod object {
184    //! Kinds of objects defined by the spec
185    //!
186    //! These types exist only to be statically-typed versions of the associated string. e.g.
187    //! `PlaceType` -> `"Place"`
188
189    use crate::kind;
190
191    kind!(ObjectType, Object);
192    kind!(ArticleType, Article);
193    kind!(AudioType, Audio);
194    kind!(DocumentType, Document);
195    kind!(EventType, Event);
196    kind!(ImageType, Image);
197    kind!(NoteType, Note);
198    kind!(PageType, Page);
199    kind!(PlaceType, Place);
200    kind!(ProfileType, Profile);
201    kind!(RelationshipType, Relationship);
202    kind!(TombstoneType, Tombstone);
203    kind!(VideoType, Video);
204}
205
206#[cfg(test)]
207mod tests {
208    use super::kind;
209
210    #[test]
211    fn to_string_works() {
212        kind!(MyType, My);
213
214        assert_eq!(MyType::My.to_string(), "My")
215    }
216}