From 17d581663e66eedd10b888a8ae551ef05221e53a Mon Sep 17 00:00:00 2001 From: Herr Seppia Date: Fri, 10 Jan 2025 10:54:07 +0100 Subject: [PATCH] piecrust: add Session::set_meta See also dusk-network/rusk#3341 --- piecrust/CHANGELOG.md | 5 +++++ piecrust/src/session.rs | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/piecrust/CHANGELOG.md b/piecrust/CHANGELOG.md index 72bfe7fc..bc6eb57d 100644 --- a/piecrust/CHANGELOG.md +++ b/piecrust/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add `Session::set_meta` [#rusk_3341] + ## [0.27.0] - 2024-12-18 ### Added @@ -485,6 +489,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 +[#rusk_3341]: https://github.com/dusk-network/rusk/issues/3341 [#410]: https://github.com/dusk-network/piecrust/issues/410 [#405]: https://github.com/dusk-network/piecrust/issues/405 [#396]: https://github.com/dusk-network/piecrust/issues/396 diff --git a/piecrust/src/session.rs b/piecrust/src/session.rs index 6e90399e..f4441704 100644 --- a/piecrust/src/session.rs +++ b/piecrust/src/session.rs @@ -748,6 +748,22 @@ impl Session { self.inner.data.get(name) } + /// Set the value of a metadata item. + /// + /// Returns the previous value of the metadata item. + pub fn set_meta( + &mut self, + name: S, + value: V, + ) -> Result>, Error> + where + S: Into>, + V: for<'a> Serialize>, + { + let data = Self::serialize_data(&value)?; + Ok(self.inner.data.set(name, data)) + } + pub fn serialize_data(value: &V) -> Result, Error> where V: for<'a> Serialize>, @@ -893,6 +909,13 @@ impl SessionData { fn get(&self, name: &str) -> Option> { self.data.get(name).cloned() } + + fn set(&mut self, name: S, data: Vec) -> Option> + where + S: Into>, + { + self.data.insert(name.into(), data) + } } impl From for SessionData {