From 5ab115e0a75b26b5dee3d8ce57913b70ed1ef2c4 Mon Sep 17 00:00:00 2001 From: Lukas Date: Tue, 29 Oct 2024 17:05:11 +0000 Subject: [PATCH] add generic remove to client and server --- libs/pavex_session/src/session_.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/libs/pavex_session/src/session_.rs b/libs/pavex_session/src/session_.rs index 511c9d2fa..fefbbfe74 100644 --- a/libs/pavex_session/src/session_.rs +++ b/libs/pavex_session/src/session_.rs @@ -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(&mut self, key: &str) -> Result, 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. @@ -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(&mut self, key: &str) -> Result, 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.