[][src]Crate activitystreams_derive

Derive macros for Activity Streams

Examples

First, add serde and activitystreams-derive to your Cargo.toml

activitystreams-derive = "0.5.0"
# or activitystreams = "0.5.0"
serde = { version = "1.0", features = ["derive"] }
use activitystreams_derive::{properties, UnitString};
// or activitystreams::{properties, UnitString};
use serde_json::Value;

/// Using the UnitString derive macro
///
/// This macro implements Serialize and Deserialize for the given type, making this type
/// represent the string "SomeKind" in JSON.
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, UnitString)]
#[unit_string(SomeKind)]
pub struct MyKind;

/// Using the properties macro
///
/// This macro generates getters and setters for the associated fields.
properties! {
    My {
        context {
            types [
                String,
            ],
            rename("@context"),
        },
        kind {
            types [
                MyKind,
            ],
            functional,
            required,
            rename("type"),
        },
        required_key {
            types [
                Value,
            ],
            functional,
            required,
            alias [
                "someKey",
                "existingKey",
                "woooKey",
            ],
        },
    }
}

fn main () -> Result<(), Box<dyn std::error::Error>> {
    let s = r#"{
        "@context": "http://www.w3c.org/ns#activitystreams",
        "type": "SomeKind",
        "woooKey": {
            "key": "value"
        }
    }"#;

    let m: MyProperties = serde_json::from_str(s)?;
    assert_eq!(&MyKind, m.get_kind());
    Ok(())
}

Macros

properties

Generate structs and enums for activitystreams objects

Attribute Macros

wrapper_type

Derive a wrapper type based on serde_json::Value to contain any possible trait type

Derive Macros

Extensible

Generate a type with default extensions

PropRefs

Derive implementations for activitystreams objects

UnitString

Derive implementations Serialize and Deserialize for a constant string Struct type