From ae02f234a095d048a02ea16aab091e7cc4662330 Mon Sep 17 00:00:00 2001 From: asonix Date: Tue, 10 Mar 2020 11:48:54 -0500 Subject: [PATCH] Start updating Readmes, dates --- Cargo.toml | 6 +- activitystreams-derive/README.md | 103 +++++++++++++----- activitystreams-derive/src/lib.rs | 2 +- activitystreams-traits/README.md | 31 +++++- activitystreams-traits/src/activity.rs | 2 +- activitystreams-traits/src/actor.rs | 2 +- activitystreams-traits/src/collection.rs | 2 +- activitystreams-traits/src/lib.rs | 17 ++- activitystreams-traits/src/link.rs | 2 +- activitystreams-traits/src/object.rs | 11 +- activitystreams-types/Cargo.toml | 2 +- activitystreams-types/README.md | 13 +-- activitystreams-types/src/activity/accept.rs | 2 +- activitystreams-types/src/activity/add.rs | 2 +- activitystreams-types/src/activity/amove.rs | 2 +- .../src/activity/announce.rs | 2 +- activitystreams-types/src/activity/arrive.rs | 2 +- activitystreams-types/src/activity/block.rs | 2 +- activitystreams-types/src/activity/create.rs | 2 +- 19 files changed, 147 insertions(+), 60 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b3a30ee..699d3ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,14 +10,14 @@ keywords = ["activitystreams", "activitypub"] edition = "2018" [dependencies] -activitystreams-traits = { version = "0.3", path = "activitystreams-traits" } -activitystreams-types = { version = "0.3.0", path = "activitystreams-types" } +activitystreams-traits = { version = "0.3.0", path = "activitystreams-traits" } +activitystreams-types = { version = "0.4.0", path = "activitystreams-types" } [dev-dependencies] anyhow = "1.0" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -activitystreams-derive = { version = "0.3", path = "activitystreams-derive" } +activitystreams-derive = { version = "0.3.0", path = "activitystreams-derive" } [profile.dev.package.activitystreams-derive] opt-level = 3 diff --git a/activitystreams-derive/README.md b/activitystreams-derive/README.md index 8b142da..fdd13dd 100644 --- a/activitystreams-derive/README.md +++ b/activitystreams-derive/README.md @@ -3,25 +3,27 @@ __derive macros for ActivityStreams__ - [Read the documentation on docs.rs](https://docs.rs/activitystreams-derive) - [Find the crate on crates.io](https://crates.io/crates/activitystreams-derive) -- [Join the discussion on Matrix](https://matrix.to/#/!fAEcHyTUdAaKCzIKCt:asonix.dog?via=asonix.dog) +- [Hit me up on Mastodon](https://asonix.dog/@asonix) ## Usage Add the required crates to your `Cargo.toml` ```toml # Cargo.toml -activitystreams-derive = "0.2" -activitystreams-traits = "0.2" -serde = "1.0" -serde_derive = "1.0" -serde_json = "1.0" +activitystreams-derive = "0.3" +activitystreams-traits = "0.3" +activitystreams-types = "0.4" +serde = { version = "1.0", features = ["derive"] } ``` And then in your project ```rust -use activitystreams_derive::{Properties, UnitString}; -use activitystreams_traits::{Link, Object}; -use serde_derive::{Deserialize, Serialize}; +use activitystreams_derive::{properties, PropRefs, UnitString}; +use activitystreams_traits::Object; +use activitystreams_types::object::{ + properties::ObjectProperties, + ObjectExt, +}; /// Using the UnitString derive macro /// @@ -31,27 +33,72 @@ use serde_derive::{Deserialize, Serialize}; #[activitystreams(SomeKind)] pub struct MyKind; -/// Using the Properties derive macro -/// -/// This macro generates getters and setters for the associated fields. -#[derive(Clone, Debug, Default, Deserialize, Serialize, Properties)] +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.", + "", + "'required' here means that the field must be present, otherwise, it's" + "represented as an Option", + ], + types [ + String, + ], + functional, + required, + }, + } +} + +#[derive(Clone, Default, PropRefs, serde::Deserialize, serde::Serialize)] #[serde(rename_all = "camelCase")] -pub struct MyProperties { - /// Derive getters and setters for @context with Link and Object traits. - #[serde(rename = "@context")] - #[activitystreams(ab(Object, Link))] - pub context: Option, +pub struct My { + /// Derive AsRef and AsMut + #[serde(flatten)] + #[activitystreams(None)] + my_properties: MyProperties, - /// Use the UnitString MyKind to enforce the type of the object by "SomeKind" - pub kind: MyKind, - - /// Derive getters and setters for required_key with String type. + /// Derive AsRef and AsMut /// - /// 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. - #[activitystreams(concrete(String), functional)] - pub required_key: serde_json::Value, + /// as well as the Object and ObjectExt traits + #[serde(flatten)] + #[activitystreams(Object)] + properties: ObjectProperties, +} + +fn main() -> Result<(), Box> { + let mut my = My::default(); + + my.as_mut().set_required_key("Hello")?; + + assert_eq!(my.as_ref().get_required_key(), "Hello"); + Ok(()) } ``` @@ -60,7 +107,7 @@ Feel free to open issues for anything you find an issue with. Please note that a ## License -Copyright © 2018 Riley Trautman +Copyright © 2020 Riley Trautman ActivityStreams Derive is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. diff --git a/activitystreams-derive/src/lib.rs b/activitystreams-derive/src/lib.rs index 6c0ab42..9904c5c 100644 --- a/activitystreams-derive/src/lib.rs +++ b/activitystreams-derive/src/lib.rs @@ -1,7 +1,7 @@ /* * This file is part of ActivityStreams Derive. * - * Copyright © 2018 Riley Trautman + * Copyright © 2020 Riley Trautman * * ActivityStreams Derive is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/activitystreams-traits/README.md b/activitystreams-traits/README.md index 25b417c..df90e67 100644 --- a/activitystreams-traits/README.md +++ b/activitystreams-traits/README.md @@ -3,25 +3,46 @@ __Traits for Activity Streams__ - [Read the documentation on docs.rs](https://docs.rs/activitystreams-traits) - [Find the crate on crates.io](https://crates.io/crates/activitystreams-traits) -- [Join the discussion on Matrix](https://matrix.to/#/!fAEcHyTUdAaKCzIKCt:asonix.dog?via=asonix.dog) +- [Hit me up on Mastodon](https://asonix.dog/@asonix) These traits don't provide any functionality other than anotations for types created in other projects. See the `activitystreams-types` crate for examples of how these traits could be used. ## Examples +Add it to your `Cargo.toml` +```toml +activitystreams-traits = "0.3" +serde = { version = "1.0", features = ["derive"] } +typetag = "0.1.4" +``` + +Use it in your project ```rust use activitystreams_traits::{Object, Actor}; -use serde_derive::{Deserialize, Serialize}; -#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)] #[serde(rename_all = "camelCase")] pub struct Persona { #[serde(rename = "type")] kind: String, } -impl Object for Persona {} +// TypeTag exists so user-defined types can be deserialized generically in activitystreams-types +#[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 { + Box::new(self.clone()) + } +} impl Actor for Persona {} ``` @@ -30,7 +51,7 @@ Feel free to open issues for anything you find an issue with. Please note that a ## License -Copyright © 2018 Riley Trautman +Copyright © 2020 Riley Trautman ActivityStreams Traits is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. diff --git a/activitystreams-traits/src/activity.rs b/activitystreams-traits/src/activity.rs index f0c7281..36a7591 100644 --- a/activitystreams-traits/src/activity.rs +++ b/activitystreams-traits/src/activity.rs @@ -1,7 +1,7 @@ /* * This file is part of ActivityStreams Traits. * - * Copyright © 2018 Riley Trautman + * Copyright © 2020 Riley Trautman * * ActivityStreams Traits is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/activitystreams-traits/src/actor.rs b/activitystreams-traits/src/actor.rs index d6b5757..771fcf9 100644 --- a/activitystreams-traits/src/actor.rs +++ b/activitystreams-traits/src/actor.rs @@ -1,7 +1,7 @@ /* * This file is part of ActivityStreams Traits. * - * Copyright © 2018 Riley Trautman + * Copyright © 2020 Riley Trautman * * ActivityStreams Traits is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/activitystreams-traits/src/collection.rs b/activitystreams-traits/src/collection.rs index 38c504a..1c4aa86 100644 --- a/activitystreams-traits/src/collection.rs +++ b/activitystreams-traits/src/collection.rs @@ -1,7 +1,7 @@ /* * This file is part of ActivityStreams Traits. * - * Copyright © 2018 Riley Trautman + * Copyright © 2020 Riley Trautman * * ActivityStreams Traits is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/activitystreams-traits/src/lib.rs b/activitystreams-traits/src/lib.rs index 275122b..289350f 100644 --- a/activitystreams-traits/src/lib.rs +++ b/activitystreams-traits/src/lib.rs @@ -1,7 +1,7 @@ /* * This file is part of ActivityStreams Traits. * - * Copyright © 2018 Riley Trautman + * Copyright © 2020 Riley Trautman * * ActivityStreams Traits is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,8 +39,19 @@ //! } //! //! #[typetag::serde] -//! impl Object for Persona {} -//! #[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 { +//! Box::new(self.clone()) +//! } +//! } //! impl Actor for Persona {} //! //! # fn main() {} diff --git a/activitystreams-traits/src/link.rs b/activitystreams-traits/src/link.rs index cf5f17c..79fbaf4 100644 --- a/activitystreams-traits/src/link.rs +++ b/activitystreams-traits/src/link.rs @@ -1,7 +1,7 @@ /* * This file is part of ActivityStreams Traits. * - * Copyright © 2018 Riley Trautman + * Copyright © 2020 Riley Trautman * * ActivityStreams Traits is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/activitystreams-traits/src/object.rs b/activitystreams-traits/src/object.rs index 352bee1..40c7107 100644 --- a/activitystreams-traits/src/object.rs +++ b/activitystreams-traits/src/object.rs @@ -1,7 +1,7 @@ /* * This file is part of ActivityStreams Traits. * - * Copyright © 2018 Riley Trautman + * Copyright © 2020 Riley Trautman * * ActivityStreams Traits is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,9 +26,18 @@ use std::any::Any; /// `Collection` and `OrderedCollection`. #[typetag::serde(tag = "type")] pub trait Object: std::fmt::Debug { + /// Provide an as_any method to allow for borrowed downcasting. + /// + /// This is useful since Objects can be deserialized generically via typetag fn as_any(&self) -> &dyn Any; + /// Provide an as_any method to allow for mutably borrowed downcasting. + /// + /// This is useful since Objects can be deserialized generically via typetag fn as_any_mut(&mut self) -> &mut dyn Any; + /// Provide a duplicate method to allow for cloning type objects. + /// + /// This is useful since Objects can be deserialized generically via typetag fn duplicate(&self) -> Box; } diff --git a/activitystreams-types/Cargo.toml b/activitystreams-types/Cargo.toml index 3c7303e..e7a5ff4 100644 --- a/activitystreams-types/Cargo.toml +++ b/activitystreams-types/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "activitystreams-types" description = "Base types from the Activity Streams spec" -version = "0.3.0" +version = "0.4.0" license = "GPL-3.0" authors = ["asonix "] repository = "https://git.asonix.dog/Aardwolf/activitystreams" diff --git a/activitystreams-types/README.md b/activitystreams-types/README.md index 5db7494..d15fe04 100644 --- a/activitystreams-types/README.md +++ b/activitystreams-types/README.md @@ -3,14 +3,14 @@ __A base set of types from the Activity Streams specification.__ - [Read the documentation on docs.rs](https://docs.rs/activitystreams-types) - [Find the crate on crates.io](https://crates.io/crates/activitystreams-types) -- [Join the discussion on Matrix](https://matrix.to/#/!fAEcHyTUdAaKCzIKCt:asonix.dog?via=asonix.dog) +- [Hit me up on Mastodon](https://asonix.dog/@asonix) ## Usage -First, add the crate to your cargo.toml +First, add the crate to your `Cargo.toml` ```toml # Cargo.toml -activitystreams-types = "0.3" +activitystreams-types = "0.4" ``` Then use it in your project! @@ -18,12 +18,11 @@ Then use it in your project! // in your project use activitystreams_types::{context, link::Mention}; -use failure::Error; -fn run() -> Result<(), Error> { +fn run() -> Result<(), Box> { /// A Mention is the only predefined Link type in the Activity Streams spec let mut mention = Mention::default(); - mention.link_props.set_context_object(context())?; + mention.as_ref().set_context_xsd_any_uri(context())?; let mention_string = serde_json::to_string(&mention)?; @@ -38,7 +37,7 @@ Feel free to open issues for anything you find an issue with. Please note that a ## License -Copyright © 2018 Riley Trautman +Copyright © 2020 Riley Trautman ActivityStreams Types is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. diff --git a/activitystreams-types/src/activity/accept.rs b/activitystreams-types/src/activity/accept.rs index a89be5d..6eef89d 100644 --- a/activitystreams-types/src/activity/accept.rs +++ b/activitystreams-types/src/activity/accept.rs @@ -1,7 +1,7 @@ /* * This file is part of ActivityStreams Types. * - * Copyright © 2018 Riley Trautman + * Copyright © 2020 Riley Trautman * * ActivityStreams Types is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/activitystreams-types/src/activity/add.rs b/activitystreams-types/src/activity/add.rs index 9e2ba2b..5e13bb6 100644 --- a/activitystreams-types/src/activity/add.rs +++ b/activitystreams-types/src/activity/add.rs @@ -1,7 +1,7 @@ /* * This file is part of ActivityStreams Types. * - * Copyright © 2018 Riley Trautman + * Copyright © 2020 Riley Trautman * * ActivityStreams Types is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/activitystreams-types/src/activity/amove.rs b/activitystreams-types/src/activity/amove.rs index e7f942f..0eac1da 100644 --- a/activitystreams-types/src/activity/amove.rs +++ b/activitystreams-types/src/activity/amove.rs @@ -1,7 +1,7 @@ /* * This file is part of ActivityStreams Types. * - * Copyright © 2018 Riley Trautman + * Copyright © 2020 Riley Trautman * * ActivityStreams Types is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/activitystreams-types/src/activity/announce.rs b/activitystreams-types/src/activity/announce.rs index 2666b52..f724a12 100644 --- a/activitystreams-types/src/activity/announce.rs +++ b/activitystreams-types/src/activity/announce.rs @@ -1,7 +1,7 @@ /* * This file is part of ActivityStreams Types. * - * Copyright © 2018 Riley Trautman + * Copyright © 2020 Riley Trautman * * ActivityStreams Types is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/activitystreams-types/src/activity/arrive.rs b/activitystreams-types/src/activity/arrive.rs index 4e10197..15f5ec5 100644 --- a/activitystreams-types/src/activity/arrive.rs +++ b/activitystreams-types/src/activity/arrive.rs @@ -1,7 +1,7 @@ /* * This file is part of ActivityStreams Types. * - * Copyright © 2018 Riley Trautman + * Copyright © 2020 Riley Trautman * * ActivityStreams Types is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/activitystreams-types/src/activity/block.rs b/activitystreams-types/src/activity/block.rs index aec816e..a849477 100644 --- a/activitystreams-types/src/activity/block.rs +++ b/activitystreams-types/src/activity/block.rs @@ -1,7 +1,7 @@ /* * This file is part of ActivityStreams Types. * - * Copyright © 2018 Riley Trautman + * Copyright © 2020 Riley Trautman * * ActivityStreams Types is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/activitystreams-types/src/activity/create.rs b/activitystreams-types/src/activity/create.rs index 343707a..1284060 100644 --- a/activitystreams-types/src/activity/create.rs +++ b/activitystreams-types/src/activity/create.rs @@ -1,7 +1,7 @@ /* * This file is part of ActivityStreams Types. * - * Copyright © 2018 Riley Trautman + * Copyright © 2020 Riley Trautman * * ActivityStreams Types is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by