[][src]Crate activitystreams_ext

An extension API for activitystreams

This crate provides Ext1, Ext2, Ext3, and Ext4 for adding extensions to ActivityStreams types

Usage

First, add ActivityStreams to your dependencies

[dependencies]
activitystreams = "0.7.0-alpha.3"
activitystreams-ext = "0.1.0-alpha.2"

For an example, we'll implement a PublicKey extension and demonstrate usage with Ext1

use activitystreams::{
    actor::{ApActor, Person},
    context,
    prelude::*,
    security,
    unparsed::UnparsedMutExt,
    url::Url,
};
use activitystreams_ext::{Ext1, UnparsedExtension};

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicKey {
    public_key: PublicKeyInner,
}

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicKeyInner {
    id: Url,
    owner: Url,
    public_key_pem: String,
}

impl<U> UnparsedExtension<U> for PublicKey
where
    U: UnparsedMutExt,
{
    type Error = serde_json::Error;

    fn try_from_unparsed(unparsed_mut: &mut U) -> Result<Self, Self::Error> {
        Ok(PublicKey {
            public_key: unparsed_mut.remove("publicKey")?,
        })
    }

    fn try_into_unparsed(self, unparsed_mut: &mut U) -> Result<(), Self::Error> {
        unparsed_mut.insert("publicKey", self.public_key)?;
        Ok(())
    }
}

pub type ExtendedPerson = Ext1<ApActor<Person>, PublicKey>;

fn main() -> Result<(), anyhow::Error> {
    let actor = ApActor::new("http://in.box".parse()?, Person::new());

    let mut person = Ext1::new(
        actor,
        PublicKey {
            public_key: PublicKeyInner {
                id: "http://key.id".parse()?,
                owner: "http://owner.id".parse()?,
                public_key_pem: "asdfasdfasdf".to_owned(),
            },
        },
    );

    person.set_context(context()).add_context(security());

    let any_base = person.into_any_base()?;
    println!("any_base: {:#?}", any_base);
    let person = ExtendedPerson::from_any_base(any_base)?;

    println!("person: {:#?}", person);
    Ok(())
}

Structs

Ext1

Extend a type with a single value

Ext2

Extend a type with two values

Ext3

Extend a type with three values

Ext4

Extend a type with four values

Traits

UnparsedExtension

Transform types from and into the Unparsed structure