From ea76d6d615800db659454697552d8123f5c8cb08 Mon Sep 17 00:00:00 2001 From: Tomasz Kurcz Date: Wed, 13 Nov 2024 16:52:42 +0100 Subject: [PATCH] fix!: rename `Column::update` to `Column::set` --- packages/storey/src/containers/column.rs | 10 ++-- packages/storey/src/containers/item.rs | 58 +++++++++++++++++++----- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/packages/storey/src/containers/column.rs b/packages/storey/src/containers/column.rs index 449819e..df39eed 100644 --- a/packages/storey/src/containers/column.rs +++ b/packages/storey/src/containers/column.rs @@ -365,7 +365,7 @@ where Ok(id) } - /// Update the value associated with the given ID. + /// Set the value associated with the given ID. /// /// # Example /// ``` @@ -380,10 +380,10 @@ where /// access.push(&1337).unwrap(); /// assert_eq!(access.get(1).unwrap(), Some(1337)); /// - /// access.update(1, &9001).unwrap(); + /// access.set(1, &9001).unwrap(); /// assert_eq!(access.get(1).unwrap(), Some(9001)); /// ``` - pub fn update(&mut self, id: u32, value: &T) -> Result<(), UpdateError> { + pub fn set(&mut self, id: u32, value: &T) -> Result<(), UpdateError> { self.storage .get(&encode_id(id)) .ok_or(UpdateError::NotFound)?; @@ -497,8 +497,8 @@ mod tests { assert_eq!(access.len().unwrap(), 2); access.remove(1).unwrap(); - assert_eq!(access.update(1, &9001), Err(UpdateError::NotFound)); - access.update(2, &9001).unwrap(); + assert_eq!(access.set(1, &9001), Err(UpdateError::NotFound)); + access.set(2, &9001).unwrap(); assert_eq!(access.get(1).unwrap(), None); assert_eq!(access.get(2).unwrap(), Some(9001)); diff --git a/packages/storey/src/containers/item.rs b/packages/storey/src/containers/item.rs index 465a5eb..89f1ba8 100644 --- a/packages/storey/src/containers/item.rs +++ b/packages/storey/src/containers/item.rs @@ -234,12 +234,37 @@ where Ok(()) } - pub fn update(&mut self, f: F) -> Result<(), UpdateError> + /// Update the value of the item. + /// + /// The function `f` is called with the current value of the item, if it exists. + /// If the function returns `Some`, the item is set to the new value. + /// If the function returns `None`, the item is removed. + /// + /// # Example + /// ``` + /// # use mocks::encoding::TestEncoding; + /// # use mocks::backend::TestStorage; + /// use storey::containers::Item; + /// + /// let mut storage = TestStorage::new(); + /// let item = Item::::new(0); + /// + /// item.access(&mut storage).set(&42).unwrap(); + /// item.access(&mut storage).update(|value| value.map(|v| v + 1)).unwrap(); + /// assert_eq!(item.access(&storage).get().unwrap(), Some(43)); + /// ``` + pub fn update(&mut self, f: F) -> Result<(), UpdateError> where - F: FnOnce(Option) -> T, + F: FnOnce(Option) -> Option, { let new_value = f(self.get().map_err(UpdateError::Decode)?); - self.set(&new_value).map_err(UpdateError::Encode) + match new_value { + Some(value) => self.set(&value).map_err(UpdateError::Encode), + None => { + self.remove(); + Ok(()) + } + } } /// Remove the value of the item. @@ -263,16 +288,11 @@ where } #[derive(Debug, PartialEq, Eq, Clone, Copy, thiserror::Error)] -pub enum UpdateError -where - E: Encoding, - E::DecodeError: std::fmt::Display, - E::EncodeError: std::fmt::Display, -{ +pub enum UpdateError { #[error("decode error: {0}")] - Decode(E::DecodeError), + Decode(D), #[error("encode error: {0}")] - Encode(E::EncodeError), + Encode(E), } #[cfg(test)] @@ -297,4 +317,20 @@ mod tests { assert_eq!(access1.get().unwrap(), None); assert_eq!(storage.get(&[1]), None); } + + #[test] + fn update() { + let mut storage = TestStorage::new(); + + let item = Item::::new(0); + item.access(&mut storage).set(&42).unwrap(); + + item.access(&mut storage) + .update(|value| value.map(|v| v + 1)) + .unwrap(); + assert_eq!(item.access(&storage).get().unwrap(), Some(43)); + + item.access(&mut storage).update(|_| None).unwrap(); + assert_eq!(item.access(&storage).get().unwrap(), None); + } }