activitystreams_types/
custom_props.rs

1/*
2 * This file is part of ActivityStreams Types.
3 *
4 * Copyright © 2018 Riley Trautman
5 *
6 * ActivityStreams Types is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ActivityStreams Types is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with ActivityStreams Types.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20//! A collection of simple types for extending the ActivityStreams Types base types
21
22use serde::{de::DeserializeOwned, ser};
23
24use activitystreams_traits::{
25    Activity, Actor, Collection, CollectionPage, IntransitiveActivity, Link, Object,
26};
27use serde_derive::{Deserialize, Serialize};
28
29/// A custom type extending Link
30///
31/// CustomLink allows for providing a pre-defined Link type, and a set of extending properties, and
32/// treating those two items as a single Link type.
33///
34/// ## Example
35/// ```rust
36/// use activitystreams_types::{
37///     CustomLink,
38///     link::Mention,
39/// };
40///
41/// struct MyProps {
42///     some_prop: String,
43/// }
44///
45/// fn main() {
46///     let mention = Mention::default();
47///     let extended_mention = CustomLink::new(mention, MyProps { some_prop: "hey".to_owned() });
48/// }
49/// ```
50#[derive(Clone, Debug, Deserialize, Serialize)]
51#[serde(rename_all = "camelCase")]
52pub struct CustomLink<C, L> {
53    #[serde(flatten)]
54    pub link: L,
55
56    #[serde(flatten)]
57    pub custom_props: C,
58}
59
60impl<C, L: Link> CustomLink<C, L> {
61    pub fn new(link: L, custom_props: C) -> Self {
62        CustomLink { link, custom_props }
63    }
64}
65
66impl<C, L> Link for CustomLink<C, L>
67where
68    C: DeserializeOwned + ser::Serialize,
69    L: Link,
70{
71}
72
73/// A custom type extending Object
74///
75/// CustomObject allows for providing a pre-defined Link type, and a set of extending properties,
76/// and treating those two items as a single Object type.
77///
78/// This type can also be used to extend any type deriving from Object, such as Actor, Activity, or
79/// Collection.
80///
81/// ## Example
82/// ```rust
83/// use activitystreams_types::{
84///     CustomObject,
85///     object::Video,
86/// };
87///
88/// struct MyProps {
89///     some_prop: String,
90/// }
91///
92/// fn main() {
93///     let video = Video::default();
94///     let extended_video = CustomObject::new(video, MyProps { some_prop: "hey".to_owned() });
95/// }
96/// ```
97#[derive(Clone, Debug, Deserialize, Serialize)]
98#[serde(rename_all = "camelCase")]
99pub struct CustomObject<C, O> {
100    #[serde(flatten)]
101    pub object: O,
102
103    #[serde(flatten)]
104    pub custom_props: C,
105}
106
107impl<C, O: Object> CustomObject<C, O> {
108    pub fn new(object: O, custom_props: C) -> Self {
109        CustomObject {
110            object,
111            custom_props,
112        }
113    }
114}
115
116impl<C, O> Object for CustomObject<C, O>
117where
118    C: DeserializeOwned + ser::Serialize,
119    O: Object,
120{
121}
122impl<C, O> Actor for CustomObject<C, O>
123where
124    C: DeserializeOwned + ser::Serialize,
125    O: Actor,
126{
127}
128impl<C, O> Collection for CustomObject<C, O>
129where
130    C: DeserializeOwned + ser::Serialize,
131    O: Collection,
132{
133}
134impl<C, O> CollectionPage for CustomObject<C, O>
135where
136    C: DeserializeOwned + ser::Serialize,
137    O: CollectionPage,
138{
139}
140impl<C, O> Activity for CustomObject<C, O>
141where
142    C: DeserializeOwned + ser::Serialize,
143    O: Activity,
144{
145}
146impl<C, O> IntransitiveActivity for CustomObject<C, O>
147where
148    C: DeserializeOwned + ser::Serialize,
149    O: IntransitiveActivity,
150{
151}