From feb42ee3ed2e951945a237a88c66fd64741259c2 Mon Sep 17 00:00:00 2001 From: asonix Date: Wed, 25 Nov 2020 10:11:10 -0600 Subject: [PATCH] Add from_arbitrary_json to AnyBase --- CHANGELOG.md | 3 +++ Cargo.toml | 2 +- README.md | 2 +- src/base.rs | 23 +++++++++++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0f73fd..ada6329 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Unreleased +# 0.7.0-alpha.8 +- Add `from_arbitrary_json` to AnyBase + # 0.7.0-alpha.7 - implement Extends for Base diff --git a/Cargo.toml b/Cargo.toml index 53393cd..a4df4ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "activitystreams" description = "A set of core types and traits for activitystreams data" -version = "0.7.0-alpha.7" +version = "0.7.0-alpha.8" license = "GPL-3.0" authors = ["asonix "] repository = "https://git.asonix.dog/Aardwolf/activitystreams" diff --git a/README.md b/README.md index af46865..7bbb69f 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ _A set of Traits and Types that make up the ActivityStreams and ActivityPub spec First, add ActivityStreams to your dependencies ```toml [dependencies] -activitystreams = "0.7.0-alpha.6" +activitystreams = "0.7.0-alpha.8" ``` ### Types diff --git a/src/base.rs b/src/base.rs index 60cae4b..7617fb6 100644 --- a/src/base.rs +++ b/src/base.rs @@ -1097,6 +1097,29 @@ impl Base { } impl AnyBase { + /// Convert arbitrary json into an AnyBase + /// + /// This is provided so that strangely-shaped objects, specifically in context fields, can be + /// easily added into a structure, but otherwise probably shouldn't be used. + /// + /// ```rust + /// # fn main() -> Result<(), anyhow::Error> { + /// # use activitystreams::base::AnyBase; + /// let any_base = AnyBase::from_arbitrary_json(serde_json::json!({ + /// "key": "value" + /// }))?; + /// # Ok(()) + /// # } + /// ``` + pub fn from_arbitrary_json(serializable: T) -> Result + where + T: serde::Serialize, + { + let value = serde_json::to_value(serializable)?; + let base: Base = serde_json::from_value(value)?; + Ok(base.into()) + } + /// Convert any type that is extended from `Base` into an AnyBase for storing /// /// ```rust