Derive Serialize for types in key::verification::start

This commit is contained in:
Jonas Platte 2020-04-29 21:45:55 +02:00
parent 146d2715a4
commit a5603aa949
No known key found for this signature in database
GPG Key ID: 7D261D771D915378

View File

@ -1,8 +1,7 @@
//! Types for the *m.key.verification.start* event. //! Types for the *m.key.verification.start* event.
use ruma_identifiers::DeviceId; use ruma_identifiers::DeviceId;
use serde::{ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Serialize};
use serde_json::{from_value, Value};
use super::{ use super::{
HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode, ShortAuthenticationString, HashAlgorithm, KeyAgreementProtocol, MessageAuthenticationCode, ShortAuthenticationString,
@ -14,14 +13,15 @@ use crate::{EventType, InvalidInput, TryFromRaw};
/// ///
/// Typically sent as a to-device event. /// Typically sent as a to-device event.
#[derive(Clone, Debug, PartialEq, Serialize)] #[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename = "m.key.verification.start", tag = "type")] #[serde(tag = "type", rename = "m.key.verification.start")]
pub struct StartEvent { pub struct StartEvent {
/// The event's content. /// The event's content.
pub content: StartEventContent, pub content: StartEventContent,
} }
/// The payload of an *m.key.verification.start* event. /// The payload of an *m.key.verification.start* event.
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(untagged)]
pub enum StartEventContent { pub enum StartEventContent {
/// The *m.sas.v1* verification method. /// The *m.sas.v1* verification method.
MSasV1(MSasV1Content), MSasV1(MSasV1Content),
@ -94,20 +94,11 @@ impl TryFromRaw for StartEventContent {
} }
} }
impl Serialize for StartEventContent {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match *self {
StartEventContent::MSasV1(ref content) => content.serialize(serializer),
_ => panic!("Attempted to serialize __Nonexhaustive variant."),
}
}
}
pub(crate) mod raw { pub(crate) mod raw {
use super::*; use serde::{Deserialize, Deserializer};
use serde_json::{from_value as from_json_value, Value as JsonValue};
use super::{MSasV1Content, VerificationMethod};
/// Begins an SAS key verification process. /// Begins an SAS key verification process.
/// ///
@ -137,21 +128,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("method") { let method_value = match value.get("method") {
Some(value) => value.clone(), Some(value) => value.clone(),
None => return Err(D::Error::missing_field("method")), None => return Err(D::Error::missing_field("method")),
}; };
let method = match from_value::<VerificationMethod>(method_value) { let method = match from_json_value::<VerificationMethod>(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 {
VerificationMethod::MSasV1 => { VerificationMethod::MSasV1 => {
let content = match from_value::<MSasV1Content>(value) { let content = match from_json_value::<MSasV1Content>(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())),
}; };
@ -167,7 +158,8 @@ pub(crate) mod raw {
} }
/// The payload of an *m.key.verification.start* event using the *m.sas.v1* method. /// The payload of an *m.key.verification.start* event using the *m.sas.v1* method.
#[derive(Clone, Debug, PartialEq, Deserialize)] #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(tag = "method", rename = "m.sas.v1")]
pub struct MSasV1Content { pub struct MSasV1Content {
/// The device ID which is initiating the process. /// The device ID which is initiating the process.
pub(crate) from_device: DeviceId, pub(crate) from_device: DeviceId,
@ -285,31 +277,6 @@ impl MSasV1Content {
} }
} }
impl Serialize for MSasV1Content {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("MSasV1Content", 2)?;
state.serialize_field("from_device", &self.from_device)?;
state.serialize_field("transaction_id", &self.transaction_id)?;
state.serialize_field("method", "m.sas.v1")?;
state.serialize_field("key_agreement_protocols", &self.key_agreement_protocols)?;
state.serialize_field("hashes", &self.hashes)?;
state.serialize_field(
"message_authentication_codes",
&self.message_authentication_codes,
)?;
state.serialize_field(
"short_authentication_string",
&self.short_authentication_string,
)?;
state.end()
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value};