activitystreams_ext/
lib.rs

1//! # An extension API for activitystreams
2//! _This crate provides Ext1, Ext2, Ext3, and Ext4 for adding extensions to ActivityStreams types_
3//!
4//! - Find the code on [git.asonix.dog](https://git.asonix.dog/asonix/activitystreams)
5//! - Read the docs on [docs.rs](https://docs.rs/activitystreams-ext)
6//! - Join the matrix channel at [#activitypub:asonix.dog](https://matrix.to/#/!fAEcHyTUdAaKCzIKCt:asonix.dog?via=asonix.dog&via=matrix.org&via=t2bot.io)
7//! - Hit me up on [mastodon](https://masto.asonix.dog/@asonix)
8//!
9//! ## Usage
10//!
11//! First, add ActivityStreams to your dependencies
12//! ```toml
13//! [dependencies]
14//! activitystreams = "0.7.0-alpha.3"
15//! activitystreams-ext = "0.1.0-alpha.2"
16//! ```
17//!
18//! For an example, we'll implement a PublicKey extension and demonstrate usage with Ext1
19//! ```rust
20//! use activitystreams::{
21//!     actor::{ApActor, Person},
22//!     context,
23//!     prelude::*,
24//!     security,
25//!     unparsed::UnparsedMutExt,
26//!     iri_string::types::IriString,
27//! };
28//! use activitystreams_ext::{Ext1, UnparsedExtension};
29//!
30//! #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
31//! #[serde(rename_all = "camelCase")]
32//! pub struct PublicKey {
33//!     public_key: PublicKeyInner,
34//! }
35//!
36//! #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
37//! #[serde(rename_all = "camelCase")]
38//! pub struct PublicKeyInner {
39//!     id: IriString,
40//!     owner: IriString,
41//!     public_key_pem: String,
42//! }
43//!
44//! impl<U> UnparsedExtension<U> for PublicKey
45//! where
46//!     U: UnparsedMutExt,
47//! {
48//!     type Error = serde_json::Error;
49//!
50//!     fn try_from_unparsed(unparsed_mut: &mut U) -> Result<Self, Self::Error> {
51//!         Ok(PublicKey {
52//!             public_key: unparsed_mut.remove("publicKey")?,
53//!         })
54//!     }
55//!
56//!     fn try_into_unparsed(self, unparsed_mut: &mut U) -> Result<(), Self::Error> {
57//!         unparsed_mut.insert("publicKey", self.public_key)?;
58//!         Ok(())
59//!     }
60//! }
61//!
62//! pub type ExtendedPerson = Ext1<ApActor<Person>, PublicKey>;
63//!
64//! fn main() -> Result<(), anyhow::Error> {
65//!     let actor = ApActor::new("http://in.box".parse()?, Person::new());
66//!
67//!     let mut person = Ext1::new(
68//!         actor,
69//!         PublicKey {
70//!             public_key: PublicKeyInner {
71//!                 id: "http://key.id".parse()?,
72//!                 owner: "http://owner.id".parse()?,
73//!                 public_key_pem: "asdfasdfasdf".to_owned(),
74//!             },
75//!         },
76//!     );
77//!
78//!     person.set_context(context()).add_context(security());
79//!
80//!     let any_base = person.into_any_base()?;
81//!     println!("any_base: {:#?}", any_base);
82//!     let person = ExtendedPerson::from_any_base(any_base)?;
83//!
84//!     println!("person: {:#?}", person);
85//!     Ok(())
86//! }
87//! ```
88
89#![doc(html_root_url = "https://docs.rs/activitystreams-ext/0.1.0-alpha.0/activitystreams_ext")]
90
91use activitystreams::{
92    base::{Base, Extends},
93    unparsed::{UnparsedMut, UnparsedMutExt},
94};
95
96mod ext1;
97mod ext2;
98mod ext3;
99mod ext4;
100
101/// Transform types from and into the Unparsed structure
102pub trait UnparsedExtension<U>
103where
104    U: UnparsedMutExt,
105{
106    type Error: std::error::Error;
107
108    /// Generate Self from Unparsed
109    fn try_from_unparsed(unparsed_mut: &mut U) -> Result<Self, Self::Error>
110    where
111        Self: Sized;
112
113    /// Insert Self into Unparsed
114    fn try_into_unparsed(self, unparsed_mut: &mut U) -> Result<(), Self::Error>;
115}
116
117/// Extend a type with a single value
118#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
119pub struct Ext1<Inner, A> {
120    #[serde(flatten)]
121    pub ext_one: A,
122
123    /// The type being extended
124    #[serde(flatten)]
125    pub inner: Inner,
126}
127
128/// Extend a type with two values
129#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
130pub struct Ext2<Inner, A, B> {
131    #[serde(flatten)]
132    pub ext_one: A,
133
134    #[serde(flatten)]
135    pub ext_two: B,
136
137    /// The type being extended
138    #[serde(flatten)]
139    pub inner: Inner,
140}
141
142/// Extend a type with three values
143#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
144pub struct Ext3<Inner, A, B, C> {
145    #[serde(flatten)]
146    pub ext_one: A,
147
148    #[serde(flatten)]
149    pub ext_two: B,
150
151    #[serde(flatten)]
152    pub ext_three: C,
153
154    /// The type being extended
155    #[serde(flatten)]
156    pub inner: Inner,
157}
158
159/// Extend a type with four values
160#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
161pub struct Ext4<Inner, A, B, C, D> {
162    #[serde(flatten)]
163    pub ext_one: A,
164
165    #[serde(flatten)]
166    pub ext_two: B,
167
168    #[serde(flatten)]
169    pub ext_three: C,
170
171    #[serde(flatten)]
172    pub ext_four: D,
173
174    /// The type being extended
175    #[serde(flatten)]
176    pub inner: Inner,
177}
178
179impl<Inner, A> Ext1<Inner, A> {
180    pub fn new(inner: Inner, ext_one: A) -> Self {
181        Ext1 { inner, ext_one }
182    }
183
184    pub fn into_parts(self) -> (Inner, A) {
185        (self.inner, self.ext_one)
186    }
187
188    pub fn extend<B>(self, ext_two: B) -> Ext2<Inner, A, B> {
189        Ext2 {
190            inner: self.inner,
191            ext_one: self.ext_one,
192            ext_two,
193        }
194    }
195}
196
197impl<Inner, A, B> Ext2<Inner, A, B> {
198    pub fn new(inner: Inner, ext_one: A, ext_two: B) -> Self {
199        Ext2 {
200            inner,
201            ext_one,
202            ext_two,
203        }
204    }
205
206    pub fn into_parts(self) -> (Inner, A, B) {
207        (self.inner, self.ext_one, self.ext_two)
208    }
209
210    pub fn extend<C>(self, ext_three: C) -> Ext3<Inner, A, B, C> {
211        Ext3 {
212            inner: self.inner,
213            ext_one: self.ext_one,
214            ext_two: self.ext_two,
215            ext_three,
216        }
217    }
218}
219
220impl<Inner, A, B, C> Ext3<Inner, A, B, C> {
221    pub fn new(inner: Inner, ext_one: A, ext_two: B, ext_three: C) -> Self {
222        Ext3 {
223            inner,
224            ext_one,
225            ext_two,
226            ext_three,
227        }
228    }
229
230    pub fn into_parts(self) -> (Inner, A, B, C) {
231        (self.inner, self.ext_one, self.ext_two, self.ext_three)
232    }
233
234    pub fn extend<D>(self, ext_four: D) -> Ext4<Inner, A, B, C, D> {
235        Ext4 {
236            inner: self.inner,
237            ext_one: self.ext_one,
238            ext_two: self.ext_two,
239            ext_three: self.ext_three,
240            ext_four,
241        }
242    }
243}
244
245impl<Inner, A, B, C, D> Ext4<Inner, A, B, C, D> {
246    pub fn new(inner: Inner, ext_one: A, ext_two: B, ext_three: C, ext_four: D) -> Self {
247        Ext4 {
248            inner,
249            ext_one,
250            ext_two,
251            ext_three,
252            ext_four,
253        }
254    }
255
256    pub fn into_parts(self) -> (Inner, A, B, C, D) {
257        (
258            self.inner,
259            self.ext_one,
260            self.ext_two,
261            self.ext_three,
262            self.ext_four,
263        )
264    }
265}
266
267impl<Inner, A, Error> Extends for Ext1<Inner, A>
268where
269    Inner: Extends<Error = Error> + UnparsedMut,
270    A: UnparsedExtension<Inner, Error = Error>,
271    Error: From<serde_json::Error> + std::error::Error,
272{
273    type Kind = Inner::Kind;
274
275    type Error = Error;
276
277    fn extends(base: Base<Self::Kind>) -> Result<Self, Self::Error> {
278        let mut inner = Inner::extends(base)?;
279        let ext_one = A::try_from_unparsed(&mut inner)?;
280
281        Ok(Ext1 { inner, ext_one })
282    }
283
284    fn retracts(self) -> Result<Base<Self::Kind>, Self::Error> {
285        let Ext1 { mut inner, ext_one } = self;
286
287        ext_one.try_into_unparsed(&mut inner)?;
288        inner.retracts()
289    }
290}
291
292impl<Inner, A, B, Error> Extends for Ext2<Inner, A, B>
293where
294    Inner: Extends<Error = Error> + UnparsedMut,
295    A: UnparsedExtension<Inner, Error = Error>,
296    B: UnparsedExtension<Inner, Error = Error>,
297    Error: From<serde_json::Error> + std::error::Error,
298{
299    type Kind = Inner::Kind;
300
301    type Error = Error;
302
303    fn extends(base: Base<Self::Kind>) -> Result<Self, Self::Error> {
304        let mut inner = Inner::extends(base)?;
305        let ext_one = A::try_from_unparsed(&mut inner)?;
306        let ext_two = B::try_from_unparsed(&mut inner)?;
307
308        Ok(Ext2 {
309            inner,
310            ext_one,
311            ext_two,
312        })
313    }
314
315    fn retracts(self) -> Result<Base<Self::Kind>, Self::Error> {
316        let Ext2 {
317            mut inner,
318            ext_one,
319            ext_two,
320        } = self;
321
322        ext_one.try_into_unparsed(&mut inner)?;
323        ext_two.try_into_unparsed(&mut inner)?;
324        inner.retracts()
325    }
326}
327
328impl<Inner, A, B, C, Error> Extends for Ext3<Inner, A, B, C>
329where
330    Inner: Extends<Error = Error> + UnparsedMut,
331    A: UnparsedExtension<Inner, Error = Error>,
332    B: UnparsedExtension<Inner, Error = Error>,
333    C: UnparsedExtension<Inner, Error = Error>,
334    Error: From<serde_json::Error> + std::error::Error,
335{
336    type Kind = Inner::Kind;
337
338    type Error = Error;
339
340    fn extends(base: Base<Self::Kind>) -> Result<Self, Self::Error> {
341        let mut inner = Inner::extends(base)?;
342        let ext_one = A::try_from_unparsed(&mut inner)?;
343        let ext_two = B::try_from_unparsed(&mut inner)?;
344        let ext_three = C::try_from_unparsed(&mut inner)?;
345
346        Ok(Ext3 {
347            inner,
348            ext_one,
349            ext_two,
350            ext_three,
351        })
352    }
353
354    fn retracts(self) -> Result<Base<Self::Kind>, Self::Error> {
355        let Ext3 {
356            mut inner,
357            ext_one,
358            ext_two,
359            ext_three,
360        } = self;
361
362        ext_one.try_into_unparsed(&mut inner)?;
363        ext_two.try_into_unparsed(&mut inner)?;
364        ext_three.try_into_unparsed(&mut inner)?;
365        inner.retracts()
366    }
367}
368
369impl<Inner, A, B, C, D, Error> Extends for Ext4<Inner, A, B, C, D>
370where
371    Inner: Extends<Error = Error> + UnparsedMut,
372    A: UnparsedExtension<Inner, Error = Error>,
373    B: UnparsedExtension<Inner, Error = Error>,
374    C: UnparsedExtension<Inner, Error = Error>,
375    D: UnparsedExtension<Inner, Error = Error>,
376    Error: From<serde_json::Error> + std::error::Error,
377{
378    type Kind = Inner::Kind;
379
380    type Error = Error;
381
382    fn extends(base: Base<Self::Kind>) -> Result<Self, Self::Error> {
383        let mut inner = Inner::extends(base)?;
384        let ext_one = A::try_from_unparsed(&mut inner)?;
385        let ext_two = B::try_from_unparsed(&mut inner)?;
386        let ext_three = C::try_from_unparsed(&mut inner)?;
387        let ext_four = D::try_from_unparsed(&mut inner)?;
388
389        Ok(Ext4 {
390            inner,
391            ext_one,
392            ext_two,
393            ext_three,
394            ext_four,
395        })
396    }
397
398    fn retracts(self) -> Result<Base<Self::Kind>, Self::Error> {
399        let Ext4 {
400            mut inner,
401            ext_one,
402            ext_two,
403            ext_three,
404            ext_four,
405        } = self;
406
407        ext_one.try_into_unparsed(&mut inner)?;
408        ext_two.try_into_unparsed(&mut inner)?;
409        ext_three.try_into_unparsed(&mut inner)?;
410        ext_four.try_into_unparsed(&mut inner)?;
411        inner.retracts()
412    }
413}