2018-05-12 20:14:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of ActivityStreams.
|
|
|
|
*
|
|
|
|
* Copyright © 2018 Riley Trautman
|
|
|
|
*
|
|
|
|
* ActivityStreams 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 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. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-05-14 06:18:12 +00:00
|
|
|
//! ActivityStreams
|
|
|
|
//!
|
|
|
|
//! A set of Traits and Types that make up the Activity Streams specification
|
|
|
|
//!
|
|
|
|
//! ## Examples
|
|
|
|
//!
|
|
|
|
//! ### Basic
|
|
|
|
//!
|
|
|
|
//! ```rust
|
2020-03-10 19:55:15 +00:00
|
|
|
//! use activitystreams::{
|
|
|
|
//! context,
|
|
|
|
//! object::{
|
|
|
|
//! properties::{
|
|
|
|
//! ObjectProperties,
|
|
|
|
//! ProfileProperties
|
|
|
|
//! },
|
|
|
|
//! Profile,
|
|
|
|
//! },
|
|
|
|
//! primitives::XsdAnyUri,
|
|
|
|
//! Actor,
|
|
|
|
//! Object,
|
|
|
|
//! };
|
|
|
|
//! use serde::{Deserialize, Serialize};
|
|
|
|
//! use std::any::Any;
|
2018-05-14 06:18:12 +00:00
|
|
|
//!
|
|
|
|
//! #[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
|
|
|
//! #[serde(rename_all = "camelCase")]
|
|
|
|
//! pub struct Persona {
|
|
|
|
//! #[serde(rename = "@context")]
|
2020-03-10 19:55:15 +00:00
|
|
|
//! context: XsdAnyUri,
|
2018-05-14 06:18:12 +00:00
|
|
|
//!
|
|
|
|
//! #[serde(rename = "type")]
|
|
|
|
//! kind: String,
|
|
|
|
//! }
|
|
|
|
//!
|
2020-03-10 19:55:15 +00:00
|
|
|
//! #[typetag::serde]
|
|
|
|
//! impl Object for Persona {
|
|
|
|
//! fn as_any(&self) -> &(dyn Any + 'static) {
|
|
|
|
//! self
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! fn as_any_mut(&mut self) -> &mut (dyn Any + 'static) {
|
|
|
|
//! self
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! fn duplicate(&self) -> Box<dyn Object + 'static> {
|
|
|
|
//! Box::new(self.clone())
|
|
|
|
//! }
|
|
|
|
//! }
|
2018-05-14 06:18:12 +00:00
|
|
|
//! impl Actor for Persona {}
|
|
|
|
//!
|
2020-03-10 19:55:15 +00:00
|
|
|
//! fn main() -> Result<(), anyhow::Error> {
|
2018-05-14 06:18:12 +00:00
|
|
|
//! let mut profile = Profile::default();
|
|
|
|
//!
|
2020-03-10 19:55:15 +00:00
|
|
|
//! let pprops: &mut ProfileProperties = profile.as_mut();
|
2018-05-14 06:18:12 +00:00
|
|
|
//!
|
2020-03-10 19:55:15 +00:00
|
|
|
//! pprops.set_describes_object_box(Persona {
|
|
|
|
//! context: context(),
|
2018-05-14 06:18:12 +00:00
|
|
|
//! kind: "Persona".to_owned(),
|
|
|
|
//! })?;
|
|
|
|
//!
|
2020-03-10 19:55:15 +00:00
|
|
|
//! let oprops: &mut ObjectProperties = profile.as_mut();
|
|
|
|
//! oprops.set_context_xsd_any_uri(context())?;
|
2018-05-14 06:18:12 +00:00
|
|
|
//!
|
|
|
|
//! let profile_string = serde_json::to_string(&profile)?;
|
|
|
|
//!
|
|
|
|
//! let profile: Profile = serde_json::from_str(&profile_string)?;
|
|
|
|
//!
|
|
|
|
//! Ok(())
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! ### Advanced
|
|
|
|
//!
|
|
|
|
//! ```rust
|
2020-03-10 19:55:15 +00:00
|
|
|
//! use activitystreams::{
|
|
|
|
//! properties,
|
|
|
|
//! link::{
|
|
|
|
//! properties::LinkProperties,
|
|
|
|
//! Mention,
|
|
|
|
//! },
|
|
|
|
//! Link,
|
|
|
|
//! PropRefs,
|
|
|
|
//! UnitString,
|
|
|
|
//! };
|
|
|
|
//! use serde::{Deserialize, Serialize};
|
2018-05-14 06:18:12 +00:00
|
|
|
//!
|
|
|
|
//! /// Using the UnitString derive macro
|
|
|
|
//! ///
|
|
|
|
//! /// This macro implements Serialize and Deserialize for the given type, making this type
|
2020-03-10 19:55:15 +00:00
|
|
|
//! /// represent the string "MyLink" in JSON.
|
2018-05-14 06:18:12 +00:00
|
|
|
//! #[derive(Clone, Debug, Default, UnitString)]
|
2020-03-10 19:55:15 +00:00
|
|
|
//! #[activitystreams(MyLink)]
|
2018-05-14 06:18:12 +00:00
|
|
|
//! pub struct MyKind;
|
|
|
|
//!
|
2020-03-10 19:55:15 +00:00
|
|
|
//! properties! {
|
|
|
|
//! My {
|
|
|
|
//! docs [ "Defining our own properties struct called MyProperties" ],
|
|
|
|
//!
|
|
|
|
//! required_key {
|
|
|
|
//! docs [
|
|
|
|
//! "Our own required key field",
|
|
|
|
//! "",
|
|
|
|
//! "'types' defines the range of values that can be stored in required_key",
|
|
|
|
//! "",
|
|
|
|
//! "'functional' means there is at most one value for required_key",
|
|
|
|
//! "'required' means there is at least one value for required_key",
|
|
|
|
//! ],
|
|
|
|
//! types [ String ],
|
|
|
|
//! functional,
|
|
|
|
//! required,
|
|
|
|
//! },
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
2018-05-14 06:18:12 +00:00
|
|
|
//! /// Using the Properties derive macro
|
|
|
|
//! ///
|
|
|
|
//! /// This macro generates getters and setters for the associated fields.
|
2020-03-10 19:55:15 +00:00
|
|
|
//! #[derive(Clone, Debug, Default, Deserialize, Serialize, PropRefs)]
|
2018-05-14 06:18:12 +00:00
|
|
|
//! #[serde(rename_all = "camelCase")]
|
2020-03-10 19:55:15 +00:00
|
|
|
//! pub struct My {
|
|
|
|
//! /// Use the UnitString MyKind to enforce the type of the object by "MyLink"
|
2018-05-14 06:18:12 +00:00
|
|
|
//! pub kind: MyKind,
|
|
|
|
//!
|
2020-03-10 19:55:15 +00:00
|
|
|
//! /// Derive AsRef/AsMut for My -> MyProperties
|
|
|
|
//! #[activitystreams(None)]
|
|
|
|
//! pub my_properties: MyProperties,
|
2018-05-14 06:18:12 +00:00
|
|
|
//!
|
2020-03-10 22:26:04 +00:00
|
|
|
//! /// Derive AsRef/AsMut/Link for My -> MyProperties
|
2020-03-10 19:55:15 +00:00
|
|
|
//! #[activitystreams(Link)]
|
|
|
|
//! pub link_properties: LinkProperties,
|
|
|
|
//! }
|
2018-05-14 06:18:12 +00:00
|
|
|
//!
|
2020-03-10 19:55:15 +00:00
|
|
|
//! fn main() -> Result<(), anyhow::Error> {
|
|
|
|
//! let mut my_link = My::default();
|
2018-05-14 06:18:12 +00:00
|
|
|
//!
|
2020-03-10 19:55:15 +00:00
|
|
|
//! let lprops: &mut MyProperties = my_link.as_mut();
|
|
|
|
//! lprops.set_required_key("Hey")?;
|
2018-05-14 06:18:12 +00:00
|
|
|
//!
|
|
|
|
//! let my_link_string = serde_json::to_string(&my_link)?;
|
|
|
|
//!
|
2020-03-10 19:55:15 +00:00
|
|
|
//! let my_link: My = serde_json::from_str(&my_link_string)?;
|
2018-05-14 06:18:12 +00:00
|
|
|
//!
|
|
|
|
//! Ok(())
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
|
2018-05-12 05:31:33 +00:00
|
|
|
pub mod activity;
|
|
|
|
pub mod actor;
|
|
|
|
pub mod collection;
|
|
|
|
pub mod link;
|
|
|
|
pub mod object;
|
2018-05-13 20:47:27 +00:00
|
|
|
|
2020-03-10 03:54:41 +00:00
|
|
|
pub use self::{
|
2020-03-10 22:26:04 +00:00
|
|
|
activity::{Activity, IntransitiveActivity},
|
2020-03-10 03:54:41 +00:00
|
|
|
actor::Actor,
|
2020-03-10 22:26:04 +00:00
|
|
|
collection::{Collection, CollectionPage},
|
|
|
|
link::Link,
|
|
|
|
object::Object,
|
2020-03-10 03:54:41 +00:00
|
|
|
};
|
|
|
|
pub use activitystreams_types::{context, primitives};
|
2020-03-10 19:55:15 +00:00
|
|
|
|
|
|
|
pub use activitystreams_derive::{properties, PropRefs, UnitString};
|