Skip to content

Commit

Permalink
Merge pull request #417 from dusk-network/session_metadata_write
Browse files Browse the repository at this point in the history
piecrust: add Session::set_meta
  • Loading branch information
herr-seppia authored Jan 15, 2025
2 parents 27ec84b + cbc5c84 commit 38757e3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions piecrust/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -485,6 +490,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
40 changes: 40 additions & 0 deletions piecrust/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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))
}

/// Remove a metadata item.
///
/// Returns the value of the removed item (if any).
pub fn remove_meta<S>(&mut self, name: S) -> Option<Vec<u8>>
where
S: Into<Cow<'static, str>>,
{
self.inner.data.remove(name)
}

pub fn serialize_data<V>(value: &V) -> Result<Vec<u8>, Error>
where
V: for<'a> Serialize<StandardBufSerializer<'a>>,
Expand Down Expand Up @@ -893,6 +919,20 @@ 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)
}

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

impl From<SessionDataBuilder> for SessionData {
Expand Down

0 comments on commit 38757e3

Please sign in to comment.