#activity-stream #activity-pub #macro-derive

进程宏 activitystreams-derive

为activitystreams提供派生宏

20个版本 (4个重大更改)

0.6.1 2020年5月5日
0.6.0 2020年4月29日
0.5.0 2020年4月20日
0.5.0-alpha.42020年3月18日
0.1.0 2018年5月14日

#5 in #activitystreams


用于 2 crates

GPL-3.0 许可证

61KB
1K SLoC

ActivityStreams Derive

为ActivityStreams提供派生宏

用法

将所需的crate添加到您的 Cargo.toml

# Cargo.toml

activitystreams = "0.6.0"
serde = { version = "1.0", features = ["derive"] }

然后在您的项目中

// derive macros
use activitystreams::{
    properties,
    PropRefs,
    UnitString
};
// traits
use activitystreams::Object;
// properties
use activitystreams::object::properties::ObjectProperties;

/// 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, UnitString)]
#[unit_string(SomeKind)]
pub struct MyKind;

properties! {
    My {
        docs [
            "Using the properties macro",
            "",
            "This macro generates getters and setters for the associated fields.",
        ],

        kind {
            docs [
                "Use the UnitString MyKind to enforce the type of the object by \"SomeKind\"",
                "",
                "Rename to/from 'type' when serializing/deserializing",
            ],

            types [
                MyKind,
            ],
            functional,
            required,
            rename("type"),
        },

        required_key {
            docs [
                "Derive getters and setters for required_key with String type.",
                "",
                "In the Activity Streams spec, 'functional' means there can only be one item for",
                "this key. This means all fields not labeled 'functional' can also be",
                "serialized/deserialized as Vec<T>.",
                "",
                "'required' here means that the field must be present, otherwise, it's"
                "represented as an Option<T>",
            ],
            types [
                String,
            ],
            functional,
            required,
        },
    }
}

#[derive(Clone, Default, PropRefs, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
#[prop_refs(Object)]
pub struct My {
    /// Derive AsRef<MyProperties> and AsMut<MyProperties>
    #[serde(flatten)]
    #[prop_refs]
    my_properties: MyProperties,

    /// Derive AsRef<ObjectProperties> and AsMut<ObjectProperties>
    ///
    /// as well as the Object trait
    #[serde(flatten)]
    #[prop_refs]
    properties: ObjectProperties,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut my = My::default();

    let mprops: &mut MyProperties = my.as_mut();
    mprops.set_required_key("Hello")?;

    let mprops: &MyProperties = my.as_ref();
    assert_eq!(mprops.get_required_key(), "Hello");
    Ok(())
}

贡献

如果您发现任何问题,请自由地提出问题。请注意,任何贡献的代码都将根据GPLv3许可。

许可证

版权 © 2020 Riley Trautman

ActivityStreams Derive是免费软件:您可以在自由软件基金会发布的GNU通用公共许可证的条款和条件下重新分发和/或修改它,许可证版本为3,或者(根据您的选择)任何较新版本。

ActivityStreams Derive的目的是希望它是有用的,但没有任何保证;甚至没有关于适销性或适用于特定目的的暗示保证。有关详细信息,请参阅GNU通用公共许可证。此文件是ActivityStreams Derive的一部分。

您应该已随ActivityStreams Derive收到GNU通用公共许可证的副本。如果没有,请参阅https://gnu.ac.cn/licenses/

依赖项

~1.5MB
~36K SLoC