Skip to content

Commit feb22d4

Browse files
zecakehpoljar
authored andcommitted
Move serde functions to module and use #[serde(with = "")]
Signed-off-by: Kévin Commaille <[email protected]>
1 parent 6520c9b commit feb22d4

File tree

3 files changed

+24
-35
lines changed

3 files changed

+24
-35
lines changed

crates/matrix-sdk-crypto/src/types/events/room/encrypted.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ use vodozemac::{megolm::MegolmMessage, olm::OlmMessage, Curve25519PublicKey};
2323

2424
use super::Event;
2525
use crate::types::{
26-
deserialize_curve_key, deserialize_curve_key_option,
26+
deserialize_curve_key,
2727
events::{
2828
room_key_request::{self, SupportedKeyInfo},
2929
EventType, ToDeviceEvent,
3030
},
31-
serialize_curve_key, serialize_curve_key_option, EventEncryptionAlgorithm,
31+
serde_curve_key_option, serialize_curve_key, EventEncryptionAlgorithm,
3232
};
3333

3434
/// An m.room.encrypted room event.
@@ -309,12 +309,7 @@ pub struct MegolmV1AesSha2Content {
309309
pub ciphertext: MegolmMessage,
310310

311311
/// The Curve25519 key of the sender.
312-
#[serde(
313-
default,
314-
deserialize_with = "deserialize_curve_key_option",
315-
serialize_with = "serialize_curve_key_option",
316-
skip_serializing_if = "Option::is_none"
317-
)]
312+
#[serde(default, with = "serde_curve_key_option", skip_serializing_if = "Option::is_none")]
318313
pub sender_key: Option<Curve25519PublicKey>,
319314

320315
/// The ID of the sending device.

crates/matrix-sdk-crypto/src/types/events/room_key_request.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ use serde_json::Value;
2525
use vodozemac::Curve25519PublicKey;
2626

2727
use super::{EventType, ToDeviceEvent};
28-
use crate::types::{
29-
deserialize_curve_key_option, serialize_curve_key_option, EventEncryptionAlgorithm,
30-
};
28+
use crate::types::{serde_curve_key_option, EventEncryptionAlgorithm};
3129

3230
/// The `m.room_key_request` to-device event.
3331
pub type RoomKeyRequestEvent = ToDeviceEvent<RoomKeyRequestContent>;
@@ -211,12 +209,7 @@ pub struct MegolmV1AesSha2Content {
211209
pub room_id: OwnedRoomId,
212210

213211
/// The Curve25519 key of the device which initiated the session originally.
214-
#[serde(
215-
default,
216-
deserialize_with = "deserialize_curve_key_option",
217-
serialize_with = "serialize_curve_key_option",
218-
skip_serializing_if = "Option::is_none"
219-
)]
212+
#[serde(default, with = "serde_curve_key_option", skip_serializing_if = "Option::is_none")]
220213
pub sender_key: Option<Curve25519PublicKey>,
221214

222215
/// The ID of the session that the key is for.

crates/matrix-sdk-crypto/src/types/mod.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -519,25 +519,26 @@ where
519519
keys.serialize(s)
520520
}
521521

522-
pub(crate) fn deserialize_curve_key_option<'de, D>(
523-
de: D,
524-
) -> Result<Option<Curve25519PublicKey>, D::Error>
525-
where
526-
D: Deserializer<'de>,
527-
{
528-
let key: Option<String> = Deserialize::deserialize(de)?;
529-
key.map(|k| Curve25519PublicKey::from_base64(&k)).transpose().map_err(serde::de::Error::custom)
530-
}
522+
mod serde_curve_key_option {
523+
use super::{Curve25519PublicKey, Deserialize, Deserializer, Serialize, Serializer};
531524

532-
pub(crate) fn serialize_curve_key_option<S>(
533-
key: &Option<Curve25519PublicKey>,
534-
s: S,
535-
) -> Result<S::Ok, S::Error>
536-
where
537-
S: Serializer,
538-
{
539-
let key = key.as_ref().map(|k| k.to_base64());
540-
key.serialize(s)
525+
pub(crate) fn deserialize<'de, D>(de: D) -> Result<Option<Curve25519PublicKey>, D::Error>
526+
where
527+
D: Deserializer<'de>,
528+
{
529+
let key: Option<String> = Deserialize::deserialize(de)?;
530+
key.map(|k| Curve25519PublicKey::from_base64(&k))
531+
.transpose()
532+
.map_err(serde::de::Error::custom)
533+
}
534+
535+
pub(crate) fn serialize<S>(key: &Option<Curve25519PublicKey>, s: S) -> Result<S::Ok, S::Error>
536+
where
537+
S: Serializer,
538+
{
539+
let key = key.as_ref().map(|k| k.to_base64());
540+
key.serialize(s)
541+
}
541542
}
542543

543544
/// Trait to express the various room key export formats we have in a unified

0 commit comments

Comments
 (0)