Skip to content

Commit

Permalink
piecrust: add Session::set_meta
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-seppia committed Jan 10, 2025
1 parent 27ec84b commit 17d5816
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions piecrust/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -485,6 +489,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- ISSUES -->

[#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
Expand Down
23 changes: 23 additions & 0 deletions piecrust/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S, V>(
&mut self,
name: S,
value: V,
) -> Result<Option<Vec<u8>>, Error>
where
S: Into<Cow<'static, str>>,
V: for<'a> Serialize<StandardBufSerializer<'a>>,
{
let data = Self::serialize_data(&value)?;
Ok(self.inner.data.set(name, data))
}

pub fn serialize_data<V>(value: &V) -> Result<Vec<u8>, Error>
where
V: for<'a> Serialize<StandardBufSerializer<'a>>,
Expand Down Expand Up @@ -893,6 +909,13 @@ impl SessionData {
fn get(&self, name: &str) -> Option<Vec<u8>> {
self.data.get(name).cloned()
}

fn set<S>(&mut self, name: S, data: Vec<u8>) -> Option<Vec<u8>>
where
S: Into<Cow<'static, str>>,
{
self.data.insert(name.into(), data)
}
}

impl From<SessionDataBuilder> for SessionData {
Expand Down

0 comments on commit 17d5816

Please sign in to comment.