From ea00fda23824306ff6a4c54cf0efc4806684861b Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Fri, 24 Nov 2023 17:55:34 +0000 Subject: [PATCH] Simplify de/serialization of GossipRequests Turns out we don't need to implement `TryInto/TryFrom`, whihc saves a bunch of boilerplate. --- .../src/crypto_store/mod.rs | 41 ++++++------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs b/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs index d290c5847a7..d0790c1eb0f 100644 --- a/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs +++ b/crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs @@ -138,6 +138,12 @@ impl From for IndexeddbCryptoStoreErr } } +impl From for IndexeddbCryptoStoreError { + fn from(e: serde_wasm_bindgen::Error) -> Self { + IndexeddbCryptoStoreError::Json(serde::de::Error::custom(e.to_string())) + } +} + impl From for CryptoStoreError { fn from(frm: IndexeddbCryptoStoreError) -> CryptoStoreError { match frm { @@ -363,10 +369,7 @@ impl IndexeddbCryptoStore { /// Transform a [`GossipRequest`] into a `JsValue` holding a /// [`GossipRequestIndexedDbObject`], ready for storing - fn serialize_gossip_request( - &self, - gossip_request: &GossipRequest, - ) -> Result { + fn serialize_gossip_request(&self, gossip_request: &GossipRequest) -> Result { let obj = GossipRequestIndexedDbObject { // hash the info as a key so that it can be used in index lookups. info: self.encode_key_as_string(keys::GOSSIP_REQUESTS, gossip_request.info.as_key()), @@ -377,17 +380,15 @@ impl IndexeddbCryptoStore { unsent: !gossip_request.sent_out, }; - Ok(obj.try_into()?) + Ok(serde_wasm_bindgen::to_value(&obj)?) } /// Transform a JsValue holding a [`GossipRequestIndexedDbObject`] back into /// a [`GossipRequest`] - fn deserialize_gossip_request( - &self, - stored_request: JsValue, - ) -> Result { - let idb_object: GossipRequestIndexedDbObject = stored_request.try_into()?; - self.deserialize_value_from_bytes(&idb_object.request) + fn deserialize_gossip_request(&self, stored_request: JsValue) -> Result { + let idb_object: GossipRequestIndexedDbObject = + serde_wasm_bindgen::from_value(stored_request)?; + Ok(self.deserialize_value_from_bytes(&idb_object.request)?) } } @@ -1223,24 +1224,6 @@ struct GossipRequestIndexedDbObject { unsent: bool, } -impl TryInto for GossipRequestIndexedDbObject { - type Error = IndexeddbCryptoStoreError; - - fn try_into(self) -> std::result::Result { - serde_wasm_bindgen::to_value(&self) - .map_err(|e| IndexeddbCryptoStoreError::Json(serde::ser::Error::custom(e.to_string()))) - } -} - -impl TryFrom for GossipRequestIndexedDbObject { - type Error = IndexeddbCryptoStoreError; - - fn try_from(value: JsValue) -> std::result::Result { - serde_wasm_bindgen::from_value(value) - .map_err(|e| IndexeddbCryptoStoreError::Json(serde::de::Error::custom(e.to_string()))) - } -} - #[cfg(all(test, target_arch = "wasm32"))] mod tests { use matrix_sdk_crypto::cryptostore_integration_tests;