1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//! # 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")
    }
}