Derive Serialize for types in room::encrypted
This commit is contained in:
parent
a5603aa949
commit
8944b23a39
@ -4,8 +4,7 @@ use std::{collections::BTreeMap, time::SystemTime};
|
|||||||
|
|
||||||
use js_int::UInt;
|
use js_int::UInt;
|
||||||
use ruma_identifiers::{DeviceId, EventId, RoomId, UserId};
|
use ruma_identifiers::{DeviceId, EventId, RoomId, UserId};
|
||||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::{from_value, Value};
|
|
||||||
|
|
||||||
use crate::{Algorithm, EventType, FromRaw, UnsignedData};
|
use crate::{Algorithm, EventType, FromRaw, UnsignedData};
|
||||||
|
|
||||||
@ -14,7 +13,7 @@ use crate::{Algorithm, EventType, FromRaw, UnsignedData};
|
|||||||
/// This type is to be used within a room. For a to-device event, use `EncryptedEventContent`
|
/// This type is to be used within a room. For a to-device event, use `EncryptedEventContent`
|
||||||
/// directly.
|
/// directly.
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize)]
|
#[derive(Clone, Debug, PartialEq, Serialize)]
|
||||||
#[serde(rename = "m.room.encrypted", tag = "type")]
|
#[serde(tag = "type", rename = "m.room.encrypted")]
|
||||||
pub struct EncryptedEvent {
|
pub struct EncryptedEvent {
|
||||||
/// The event's content.
|
/// The event's content.
|
||||||
pub content: EncryptedEventContent,
|
pub content: EncryptedEventContent,
|
||||||
@ -39,7 +38,8 @@ pub struct EncryptedEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The payload for `EncryptedEvent`.
|
/// The payload for `EncryptedEvent`.
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq, Serialize)]
|
||||||
|
#[serde(untagged)]
|
||||||
pub enum EncryptedEventContent {
|
pub enum EncryptedEventContent {
|
||||||
/// An event encrypted with *m.olm.v1.curve25519-aes-sha2*.
|
/// An event encrypted with *m.olm.v1.curve25519-aes-sha2*.
|
||||||
OlmV1Curve25519AesSha2(OlmV1Curve25519AesSha2Content),
|
OlmV1Curve25519AesSha2(OlmV1Curve25519AesSha2Content),
|
||||||
@ -92,23 +92,15 @@ impl_room_event!(
|
|||||||
EventType::RoomEncrypted
|
EventType::RoomEncrypted
|
||||||
);
|
);
|
||||||
|
|
||||||
impl Serialize for EncryptedEventContent {
|
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
||||||
where
|
|
||||||
S: Serializer,
|
|
||||||
{
|
|
||||||
match *self {
|
|
||||||
EncryptedEventContent::OlmV1Curve25519AesSha2(ref content) => {
|
|
||||||
content.serialize(serializer)
|
|
||||||
}
|
|
||||||
EncryptedEventContent::MegolmV1AesSha2(ref content) => content.serialize(serializer),
|
|
||||||
_ => panic!("Attempted to serialize __Nonexhaustive variant."),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) mod raw {
|
pub(crate) mod raw {
|
||||||
use super::*;
|
use std::time::SystemTime;
|
||||||
|
|
||||||
|
use ruma_identifiers::{EventId, RoomId, UserId};
|
||||||
|
use serde::{Deserialize, Deserializer};
|
||||||
|
use serde_json::{from_value as from_json_value, Value as JsonValue};
|
||||||
|
|
||||||
|
use super::{MegolmV1AesSha2Content, OlmV1Curve25519AesSha2Content};
|
||||||
|
use crate::{Algorithm, UnsignedData};
|
||||||
|
|
||||||
/// This event type is used when sending encrypted events.
|
/// This event type is used when sending encrypted events.
|
||||||
///
|
///
|
||||||
@ -159,21 +151,21 @@ pub(crate) mod raw {
|
|||||||
{
|
{
|
||||||
use serde::de::Error as _;
|
use serde::de::Error as _;
|
||||||
|
|
||||||
let value: Value = Deserialize::deserialize(deserializer)?;
|
let value: JsonValue = Deserialize::deserialize(deserializer)?;
|
||||||
|
|
||||||
let method_value = match value.get("algorithm") {
|
let method_value = match value.get("algorithm") {
|
||||||
Some(value) => value.clone(),
|
Some(value) => value.clone(),
|
||||||
None => return Err(D::Error::missing_field("algorithm")),
|
None => return Err(D::Error::missing_field("algorithm")),
|
||||||
};
|
};
|
||||||
|
|
||||||
let method = match from_value::<Algorithm>(method_value) {
|
let method = match from_json_value::<Algorithm>(method_value) {
|
||||||
Ok(method) => method,
|
Ok(method) => method,
|
||||||
Err(error) => return Err(D::Error::custom(error.to_string())),
|
Err(error) => return Err(D::Error::custom(error.to_string())),
|
||||||
};
|
};
|
||||||
|
|
||||||
match method {
|
match method {
|
||||||
Algorithm::OlmV1Curve25519AesSha2 => {
|
Algorithm::OlmV1Curve25519AesSha2 => {
|
||||||
let content = match from_value::<OlmV1Curve25519AesSha2Content>(value) {
|
let content = match from_json_value::<OlmV1Curve25519AesSha2Content>(value) {
|
||||||
Ok(content) => content,
|
Ok(content) => content,
|
||||||
Err(error) => return Err(D::Error::custom(error.to_string())),
|
Err(error) => return Err(D::Error::custom(error.to_string())),
|
||||||
};
|
};
|
||||||
@ -181,7 +173,7 @@ pub(crate) mod raw {
|
|||||||
Ok(EncryptedEventContent::OlmV1Curve25519AesSha2(content))
|
Ok(EncryptedEventContent::OlmV1Curve25519AesSha2(content))
|
||||||
}
|
}
|
||||||
Algorithm::MegolmV1AesSha2 => {
|
Algorithm::MegolmV1AesSha2 => {
|
||||||
let content = match from_value::<MegolmV1AesSha2Content>(value) {
|
let content = match from_json_value::<MegolmV1AesSha2Content>(value) {
|
||||||
Ok(content) => content,
|
Ok(content) => content,
|
||||||
Err(error) => return Err(D::Error::custom(error.to_string())),
|
Err(error) => return Err(D::Error::custom(error.to_string())),
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user