From feb22d43706e712ec91e4664c1867b6c2a160fc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Wed, 6 Aug 2025 15:19:30 +0200 Subject: [PATCH] Move serde functions to module and use `#[serde(with = "")]` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kévin Commaille --- .../src/types/events/room/encrypted.rs | 11 ++---- .../src/types/events/room_key_request.rs | 11 +----- crates/matrix-sdk-crypto/src/types/mod.rs | 37 ++++++++++--------- 3 files changed, 24 insertions(+), 35 deletions(-) diff --git a/crates/matrix-sdk-crypto/src/types/events/room/encrypted.rs b/crates/matrix-sdk-crypto/src/types/events/room/encrypted.rs index 15e6ee367..79c0715c4 100644 --- a/crates/matrix-sdk-crypto/src/types/events/room/encrypted.rs +++ b/crates/matrix-sdk-crypto/src/types/events/room/encrypted.rs @@ -23,12 +23,12 @@ use vodozemac::{megolm::MegolmMessage, olm::OlmMessage, Curve25519PublicKey}; use super::Event; use crate::types::{ - deserialize_curve_key, deserialize_curve_key_option, + deserialize_curve_key, events::{ room_key_request::{self, SupportedKeyInfo}, EventType, ToDeviceEvent, }, - serialize_curve_key, serialize_curve_key_option, EventEncryptionAlgorithm, + serde_curve_key_option, serialize_curve_key, EventEncryptionAlgorithm, }; /// An m.room.encrypted room event. @@ -309,12 +309,7 @@ pub struct MegolmV1AesSha2Content { pub ciphertext: MegolmMessage, /// The Curve25519 key of the sender. - #[serde( - default, - deserialize_with = "deserialize_curve_key_option", - serialize_with = "serialize_curve_key_option", - skip_serializing_if = "Option::is_none" - )] + #[serde(default, with = "serde_curve_key_option", skip_serializing_if = "Option::is_none")] pub sender_key: Option, /// The ID of the sending device. diff --git a/crates/matrix-sdk-crypto/src/types/events/room_key_request.rs b/crates/matrix-sdk-crypto/src/types/events/room_key_request.rs index c4ea191b8..a9ccdb07d 100644 --- a/crates/matrix-sdk-crypto/src/types/events/room_key_request.rs +++ b/crates/matrix-sdk-crypto/src/types/events/room_key_request.rs @@ -25,9 +25,7 @@ use serde_json::Value; use vodozemac::Curve25519PublicKey; use super::{EventType, ToDeviceEvent}; -use crate::types::{ - deserialize_curve_key_option, serialize_curve_key_option, EventEncryptionAlgorithm, -}; +use crate::types::{serde_curve_key_option, EventEncryptionAlgorithm}; /// The `m.room_key_request` to-device event. pub type RoomKeyRequestEvent = ToDeviceEvent; @@ -211,12 +209,7 @@ pub struct MegolmV1AesSha2Content { pub room_id: OwnedRoomId, /// The Curve25519 key of the device which initiated the session originally. - #[serde( - default, - deserialize_with = "deserialize_curve_key_option", - serialize_with = "serialize_curve_key_option", - skip_serializing_if = "Option::is_none" - )] + #[serde(default, with = "serde_curve_key_option", skip_serializing_if = "Option::is_none")] pub sender_key: Option, /// The ID of the session that the key is for. diff --git a/crates/matrix-sdk-crypto/src/types/mod.rs b/crates/matrix-sdk-crypto/src/types/mod.rs index 2f78b709e..4c6032658 100644 --- a/crates/matrix-sdk-crypto/src/types/mod.rs +++ b/crates/matrix-sdk-crypto/src/types/mod.rs @@ -519,25 +519,26 @@ where keys.serialize(s) } -pub(crate) fn deserialize_curve_key_option<'de, D>( - de: D, -) -> Result, D::Error> -where - D: Deserializer<'de>, -{ - let key: Option = Deserialize::deserialize(de)?; - key.map(|k| Curve25519PublicKey::from_base64(&k)).transpose().map_err(serde::de::Error::custom) -} +mod serde_curve_key_option { + use super::{Curve25519PublicKey, Deserialize, Deserializer, Serialize, Serializer}; -pub(crate) fn serialize_curve_key_option( - key: &Option, - s: S, -) -> Result -where - S: Serializer, -{ - let key = key.as_ref().map(|k| k.to_base64()); - key.serialize(s) + pub(crate) fn deserialize<'de, D>(de: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + let key: Option = Deserialize::deserialize(de)?; + key.map(|k| Curve25519PublicKey::from_base64(&k)) + .transpose() + .map_err(serde::de::Error::custom) + } + + pub(crate) fn serialize(key: &Option, s: S) -> Result + where + S: Serializer, + { + let key = key.as_ref().map(|k| k.to_base64()); + key.serialize(s) + } } /// Trait to express the various room key export formats we have in a unified