rustpub 0.1.0

An implementation of the Activitypub types
Documentation
use serde::{Deserialize, Serialize};

use crate::core::*;

macro_rules! object_property {
    ($name:ident) => {
        #[derive(Clone, Debug, Serialize, Deserialize)]
        #[serde(untagged)]
        pub enum $name {
            SingleObject(Box<Object>),
            MultipleObjects(Vec<Object>),
        }
    };
}

macro_rules! link_property {
    ($name:ident) => {
        #[derive(Clone, Debug, Serialize, Deserialize)]
        #[serde(untagged)]
        pub enum $name {
            SingleLink(Box<Link>),
            MultipleLinks(Vec<Link>),
        }
    };
}

macro_rules! object_link_property {
    ($name:ident) => {
        #[derive(Clone, Debug, Serialize, Deserialize)]
        #[serde(untagged)]
        pub enum $name {
            SingleObject(Box<Object>),
            MultipleObjects(Vec<Object>),
            SingleLink(Box<Link>),
            MultipleLinks(Vec<Link>),
        }
    };
}

object_link_property!(Attachment);
object_link_property!(AttributedTo);
object_link_property!(Audience);

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Context {
    Single(String),
    Many(Vec<String>),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Kind(String);

#[cfg(test)]
mod test {
    use super::*;
    use crate::parse_test;

    parse_test!(
        attachment,
        r#"{
            "@context": "https://www.w3.org/ns/activitystreams",
            "type": "Note",
            "name": "Have you seen my cat?",
            "attachment": [
                {
                    "type": "Image",
                    "content": "This is what he looks like.",
                    "url": "http://example.org/cat.jpeg"
                }
            ]
        }"#
    );

    parse_test!(
        attributed_to,
        r#"{
            "@context": "https://www.w3.org/ns/activitystreams",
            "type": "Image",
            "name": "My cat taking a nap",
            "url": "http://example.org/cat.jpeg",
            "attributedTo": [
                {
                    "type": "Person",
                    "name": "Sally"
                }
            ]
        }"#,
        r#"{
            "@context": "https://www.w3.org/ns/activitystreams",
            "type": "Image",
            "name": "My cat taking a nap",
            "url": "http://example.org/cat.jpeg",
            "attributedTo": [
                "http://joe.example.org",
                {
                    "type": "Person",
                    "name": "Sally"
                }
            ]
        }"#
    );

    parse_test!(
        audience,
        r#"{
            "@context": "https://www.w3.org/ns/activitystreams",
            "name": "Holiday announcement",
            "type": "Note",
            "content": "Thursday will be a company-wide holiday. Enjoy your day off!",
            "audience": {
                "type": "http://example.org/Organization",
                "name": "ExampleCo LLC"
            }
        }"#
    );
}