[][src]Crate activitystreams

ActivityStreams

A set of Traits and Types that make up the Activity Streams specification

Examples

Basic

use activitystreams::{
    context,
    object::{
        properties::{
            ObjectProperties,
            ProfileProperties
        },
        Profile,
    },
    primitives::XsdAnyUri,
    Actor,
    Object,
};
use serde::{Deserialize, Serialize};
use std::any::Any;

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Persona {
    #[serde(rename = "@context")]
    context: XsdAnyUri,

    #[serde(rename = "type")]
    kind: String,
}

#[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())
    }
}
impl Actor for Persona {}

fn main() -> Result<(), anyhow::Error> {
    let mut profile = Profile::default();

    let pprops: &mut ProfileProperties = profile.as_mut();

    pprops.set_describes_object_box(Persona {
        context: context(),
        kind: "Persona".to_owned(),
    })?;

    let oprops: &mut ObjectProperties = profile.as_mut();
    oprops.set_context_xsd_any_uri(context())?;

    let profile_string = serde_json::to_string(&profile)?;

    let profile: Profile = serde_json::from_str(&profile_string)?;

    Ok(())
}

Advanced

use activitystreams::{
    properties,
    link::{
        properties::LinkProperties,
        Mention,
    },
    Link,
    PropRefs,
    UnitString,
};
use serde::{Deserialize, Serialize};

/// Using the UnitString derive macro
///
/// This macro implements Serialize and Deserialize for the given type, making this type
/// represent the string "MyLink" in JSON.
#[derive(Clone, Debug, Default, UnitString)]
#[activitystreams(MyLink)]
pub struct MyKind;

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,
        },
    }
}

/// Using the Properties derive macro
///
/// This macro generates getters and setters for the associated fields.
#[derive(Clone, Debug, Default, Deserialize, Serialize, PropRefs)]
#[serde(rename_all = "camelCase")]
pub struct My {
    /// Use the UnitString MyKind to enforce the type of the object by "MyLink"
    pub kind: MyKind,

    /// Derive AsRef/AsMut for My -> MyProperties
    #[activitystreams(None)]
    pub my_properties: MyProperties,

    /// Derive AsRef/AsMut/Link for My -> MyProperties
    #[activitystreams(Link)]
    pub link_properties: LinkProperties,
}

fn main() -> Result<(), anyhow::Error> {
    let mut my_link = My::default();

    let lprops: &mut MyProperties = my_link.as_mut();
    lprops.set_required_key("Hey")?;

    let my_link_string = serde_json::to_string(&my_link)?;

    let my_link: My = serde_json::from_str(&my_link_string)?;

    Ok(())
}

Modules

activity

Activity traits and types

actor

Actor traits and types

collection

Collection traits and types

link

Link traits and types

object

Object traits and types

primitives

Types defined as 'primitives' are used as building-blocks for ActivityStreams objects.

Macros

properties

Generate structs and enums for activitystreams objects

Traits

Activity

An Activity is a subtype of Object that describes some form of action that may happen, is currently happening, or has already happened.

Actor

Actor types are Object types that are capable of performing activities.

Collection

A Collection is a subtype of Object that represents ordered or unordered sets of Object or Link instances.

CollectionPage

Used to represent distinct subsets of items from a Collection.

IntransitiveActivity

Instances of IntransitiveActivity are a subtype of Activity representing intransitive actions.

Link

A Link is an indirect, qualified reference to a resource identified by a URL.

Object

Describes an object of any kind.

Functions

context

The context associated with all of the Activity Streams types defined in the crate.

Derive Macros

PropRefs

Derive implementations for activitystreams objects

UnitString

Derive implementations Serialize and Deserialize for a constant string Struct type