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
/*
 * This file is part of ActivityStreams Types.
 *
 * Copyright © 2018 Riley Trautman
 *
 * ActivityStreams Types is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ActivityStreams Types is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ActivityStreams Types.  If not, see <http://www.gnu.org/licenses/>.
 */

//! A collection of simple types for extending the ActivityStreams Types base types

use serde::{de::DeserializeOwned, ser::Serialize};

use activitystreams_traits::{
    Activity, Actor, Collection, CollectionPage, IntransitiveActivity, Link, Object,
};

/// A custom type extending Link
///
/// CustomLink allows for providing a pre-defined Link type, and a set of extending properties, and
/// treating those two items as a single Link type.
///
/// ## Example
/// ```rust
/// use activitystreams_types::{
///     CustomLink,
///     link::Mention,
/// };
///
/// struct MyProps {
///     some_prop: String,
/// }
///
/// fn main() {
///     let mention = Mention::default();
///     let extended_mention = CustomLink::new(mention, MyProps { some_prop: "hey".to_owned() });
/// }
/// ```
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CustomLink<C, L> {
    #[serde(flatten)]
    pub link: L,

    #[serde(flatten)]
    pub custom_props: C,
}

impl<C, L: Link> CustomLink<C, L> {
    pub fn new(link: L, custom_props: C) -> Self {
        CustomLink { link, custom_props }
    }
}

impl<C, L> Link for CustomLink<C, L>
where
    C: DeserializeOwned + Serialize,
    L: Link,
{
}

/// A custom type extending Object
///
/// CustomObject allows for providing a pre-defined Link type, and a set of extending properties,
/// and treating those two items as a single Object type.
///
/// This type can also be used to extend any type deriving from Object, such as Actor, Activity, or
/// Collection.
///
/// ## Example
/// ```rust
/// use activitystreams_types::{
///     CustomObject,
///     object::Video,
/// };
///
/// struct MyProps {
///     some_prop: String,
/// }
///
/// fn main() {
///     let video = Video::default();
///     let extended_video = CustomObject::new(video, MyProps { some_prop: "hey".to_owned() });
/// }
/// ```
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CustomObject<C, O> {
    #[serde(flatten)]
    pub object: O,

    #[serde(flatten)]
    pub custom_props: C,
}

impl<C, O: Object> CustomObject<C, O> {
    pub fn new(object: O, custom_props: C) -> Self {
        CustomObject {
            object,
            custom_props,
        }
    }
}

impl<C, O> Object for CustomObject<C, O>
where
    C: DeserializeOwned + Serialize,
    O: Object,
{
}
impl<C, O> Actor for CustomObject<C, O>
where
    C: DeserializeOwned + Serialize,
    O: Actor,
{
}
impl<C, O> Collection for CustomObject<C, O>
where
    C: DeserializeOwned + Serialize,
    O: Collection,
{}
impl<C, O> CollectionPage for CustomObject<C, O>
where
    C: DeserializeOwned + Serialize,
    O: CollectionPage,
{}
impl<C, O> Activity for CustomObject<C, O>
where
    C: DeserializeOwned + Serialize,
    O: Activity,
{}
impl<C, O> IntransitiveActivity for CustomObject<C, O>
where
    C: DeserializeOwned + Serialize,
    O: IntransitiveActivity,
{}