diff --git a/piecrust/CHANGELOG.md b/piecrust/CHANGELOG.md index 72bfe7fc..dd70cf19 100644 --- a/piecrust/CHANGELOG.md +++ b/piecrust/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add `Session::set_meta` [#rusk_3341] +- Add `Session::remove_meta` [#rusk_3341] + ## [0.27.0] - 2024-12-18 ### Added @@ -485,6 +490,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..c30b8f3f 100644 --- a/piecrust/src/session.rs +++ b/piecrust/src/session.rs @@ -748,6 +748,32 @@ 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)) + } + + /// Remove a metadata item. + /// + /// Returns the value of the removed item (if any). + pub fn remove_meta(&mut self, name: S) -> Option> + where + S: Into>, + { + self.inner.data.remove(name) + } + pub fn serialize_data(value: &V) -> Result, Error> where V: for<'a> Serialize>, @@ -893,6 +919,20 @@ 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) + } + + fn remove(&mut self, name: S) -> Option> + where + S: Into>, + { + self.data.remove(&name.into()) + } } impl From for SessionData {