Move serde functions to module and use #[serde(with = "")]

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
This commit is contained in:
Kévin Commaille
2025-08-06 15:19:30 +02:00
committed by Damir Jelić
parent 6520c9b16e
commit feb22d4370
3 changed files with 24 additions and 35 deletions

View File

@@ -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<Curve25519PublicKey>,
/// The ID of the sending device.

View File

@@ -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<RoomKeyRequestContent>;
@@ -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<Curve25519PublicKey>,
/// The ID of the session that the key is for.

View File

@@ -519,25 +519,26 @@ where
keys.serialize(s)
}
pub(crate) fn deserialize_curve_key_option<'de, D>(
de: D,
) -> Result<Option<Curve25519PublicKey>, D::Error>
where
D: Deserializer<'de>,
{
let key: Option<String> = 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<S>(
key: &Option<Curve25519PublicKey>,
s: S,
) -> Result<S::Ok, S::Error>
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<Option<Curve25519PublicKey>, D::Error>
where
D: Deserializer<'de>,
{
let key: Option<String> = Deserialize::deserialize(de)?;
key.map(|k| Curve25519PublicKey::from_base64(&k))
.transpose()
.map_err(serde::de::Error::custom)
}
pub(crate) fn serialize<S>(key: &Option<Curve25519PublicKey>, s: S) -> Result<S::Ok, S::Error>
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