Skip to content

Commit

Permalink
add generic remove to client and server
Browse files Browse the repository at this point in the history
  • Loading branch information
bosukas committed Oct 29, 2024
1 parent 0f69fae commit 5ab115e
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions libs/pavex_session/src/session_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,16 @@ impl<'session> ClientSessionStateMut<'session> {
}
}

/// Remove the value associated with `key` from the client-side state.
///
/// If the key exists, the removed value is returned.
/// If the removed value cannot be serialized, an error is returned.
pub fn remove<T: DeserializeOwned>(&mut self, key: &str) -> Result<Option<T>, serde_json::Error> {
self.remove_value(key)
.map(|value| serde_json::from_value(value))
.transpose()
}

/// Remove the value associated with `key` from the client-side state.
///
/// If the key exists, the removed value is returned.
Expand Down Expand Up @@ -432,6 +442,17 @@ impl<'session, 'store> ServerSessionState<'session, 'store> {
Ok(old_value)
}

/// Remove the value associated with `key` from the server-side state.
///
/// If the key exists, the removed value is returned.
/// If the removed value cannot be serialized, an error is returned.
pub async fn remove<T: DeserializeOwned>(&mut self, key: &str) -> Result<Option<T>, LoadError> {
self.remove_value(key).await?
.map(serde_json::from_value)
.transpose()
.map_err(LoadError::DeserializationError)
}

/// Remove the value associated with `key` from the server-side state.
///
/// If the key exists, the removed value is returned.
Expand Down

0 comments on commit 5ab115e

Please sign in to comment.