Skip to main content

activitystreams/object/
mod.rs

1/*
2 * This file is part of ActivityStreams.
3 *
4 * Copyright © 2020 Riley Trautman
5 *
6 * ActivityStreams 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 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.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20//! Namespace for Object types
21
22#[cfg(feature = "kinds")]
23pub mod kind;
24#[cfg(feature = "types")]
25pub mod properties;
26#[cfg(feature = "types")]
27mod types;
28
29#[cfg(feature = "types")]
30pub use self::types::{
31    Article, Audio, Document, Event, Image, Note, Page, Place, Profile, Relationship, Tombstone,
32    Video,
33};
34
35/// Describes an object of any kind.
36///
37/// The Object type serves as the base type for most of the other kinds of objects defined in the
38/// Activity Vocabulary, including other Core types such as `Activity`, `IntransitiveActivity`,
39/// `Collection` and `OrderedCollection`.
40#[cfg_attr(feature = "derive", crate::wrapper_type)]
41pub trait Object: crate::Base {}
42
43#[cfg(feature = "types")]
44/// Describes any kind of Image
45///
46/// Since Image is "concrete" in the ActivityStreams spec, but multiple fields in ObjectProperties
47/// require an "Image", this type acts as a filter to ensure only Images can be serialized or
48/// deserialized, but allows any adjacent fields through
49#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
50pub struct AnyImage {
51    #[serde(rename = "type")]
52    kind: self::kind::ImageType,
53
54    #[serde(flatten)]
55    rest: std::collections::HashMap<String, serde_json::Value>,
56}
57
58#[cfg(feature = "types")]
59impl AnyImage {
60    pub fn from_concrete<T>(t: T) -> Result<Self, serde_json::Error>
61    where
62        T: Object + serde::ser::Serialize,
63    {
64        serde_json::from_value(serde_json::to_value(t)?)
65    }
66
67    pub fn into_concrete<T>(self) -> Result<T, serde_json::Error>
68    where
69        T: Object + serde::de::DeserializeOwned,
70    {
71        serde_json::from_value(serde_json::to_value(self)?)
72    }
73}