44
55use std:: borrow:: Cow ;
66
7- use as_variant:: as_variant;
87use ruma_common:: {
98 OwnedTransactionId ,
109 serde:: { Base64 , JsonObject } ,
1110} ;
1211use ruma_macros:: EventContent ;
13- use serde:: { Deserialize , Deserializer , Serialize , de } ;
12+ use serde:: { Deserialize , Serialize } ;
1413use serde_json:: { Value as JsonValue , from_value as from_json_value} ;
1514
1615use super :: {
@@ -81,19 +80,8 @@ pub enum AcceptMethod {
8180}
8281
8382impl AcceptMethod {
84- /// The value of the `method` field.
85- pub fn method ( & self ) -> & str {
86- match self {
87- Self :: SasV1 ( _) => "m.sas.v1" ,
88- Self :: _Custom( c) => & c. method ,
89- }
90- }
91-
9283 /// The data of this `AcceptMethod`.
9384 ///
94- /// The returned JSON object won't contain the `method` field, use [`.method()`][Self::method]
95- /// to access it.
96- ///
9785 /// Prefer to use the public variants of `AcceptMethod` where possible; this method is meant to
9886 /// be used for custom methods only.
9987 pub fn data ( & self ) -> Cow < ' _ , JsonObject > {
@@ -117,39 +105,21 @@ impl AcceptMethod {
117105impl < ' de > Deserialize < ' de > for AcceptMethod {
118106 fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
119107 where
120- D : Deserializer < ' de > ,
108+ D : serde :: Deserializer < ' de > ,
121109 {
122- let mut data = JsonObject :: deserialize ( deserializer) ?;
123-
124- let method = data
125- . get ( "method" )
126- . and_then ( |value| as_variant ! ( value, JsonValue :: String ) )
127- . ok_or_else ( || de:: Error :: missing_field ( "method" ) ) ?;
128-
129- match method. as_ref ( ) {
130- "m.sas.v1" => from_json_value ( data. into ( ) ) . map ( Self :: SasV1 ) ,
131- _ => {
132- let method = as_variant ! (
133- data. remove( "method" )
134- . expect( "we already checked that the method field is present" ) ,
135- JsonValue :: String
136- )
137- . expect ( "we already checked that the method is a string" ) ;
138-
139- Ok ( Self :: _Custom ( _CustomAcceptMethodContent { method, data } ) )
140- }
141- }
142- . map_err ( de:: Error :: custom)
110+ let data = JsonObject :: deserialize ( deserializer) ?;
111+
112+ Ok ( match from_json_value ( data. clone ( ) . into ( ) ) {
113+ Ok ( sas_v1_content) => AcceptMethod :: SasV1 ( sas_v1_content) ,
114+ Err ( _) => AcceptMethod :: _Custom ( _CustomAcceptMethodContent { data } ) ,
115+ } )
143116 }
144117}
145118
146119/// Method specific content of a unknown key verification method.
147120#[ doc( hidden) ]
148121#[ derive( Clone , Debug , Serialize ) ]
149122pub struct _CustomAcceptMethodContent {
150- /// The name of the method.
151- method : String ,
152-
153123 /// The additional fields that the method contains.
154124 #[ serde( flatten) ]
155125 data : JsonObject ,
@@ -158,7 +128,6 @@ pub struct _CustomAcceptMethodContent {
158128/// The payload of an `m.key.verification.accept` event using the `m.sas.v1` method.
159129#[ derive( Clone , Debug , Deserialize , Serialize ) ]
160130#[ cfg_attr( not( ruma_unstable_exhaustive_types) , non_exhaustive) ]
161- #[ serde( rename = "m.sas.v1" , tag = "method" ) ]
162131pub struct SasV1Content {
163132 /// The key agreement protocol the device is choosing to use, out of the
164133 /// options in the `m.key.verification.start` message.
@@ -260,7 +229,6 @@ mod tests {
260229 key_verification_accept_content,
261230 json!( {
262231 "transaction_id" : "456" ,
263- "method" : "m.sas.v1" ,
264232 "commitment" : "aGVsbG8" ,
265233 "key_agreement_protocol" : "curve25519" ,
266234 "hash" : "sha256" ,
@@ -288,7 +256,6 @@ mod tests {
288256 assert_to_canonical_json_eq ! (
289257 key_verification_accept_content,
290258 json!( {
291- "method" : "m.sas.v1" ,
292259 "commitment" : "aGVsbG8" ,
293260 "key_agreement_protocol" : "curve25519" ,
294261 "hash" : "sha256" ,
@@ -307,7 +274,6 @@ mod tests {
307274 let json = json ! ( {
308275 "transaction_id" : "456" ,
309276 "commitment" : "aGVsbG8" ,
310- "method" : "m.sas.v1" ,
311277 "hash" : "sha256" ,
312278 "key_agreement_protocol" : "curve25519" ,
313279 "message_authentication_code" : "hkdf-hmac-sha256.v2" ,
@@ -329,7 +295,6 @@ mod tests {
329295 "content" : {
330296 "commitment" : "aGVsbG8" ,
331297 "transaction_id" : "456" ,
332- "method" : "m.sas.v1" ,
333298 "key_agreement_protocol" : "curve25519" ,
334299 "hash" : "sha256" ,
335300 "message_authentication_code" : "hkdf-hmac-sha256.v2" ,
@@ -356,7 +321,6 @@ mod tests {
356321 fn in_room_deserialization ( ) {
357322 let json = json ! ( {
358323 "commitment" : "aGVsbG8" ,
359- "method" : "m.sas.v1" ,
360324 "hash" : "sha256" ,
361325 "key_agreement_protocol" : "curve25519" ,
362326 "message_authentication_code" : "hkdf-hmac-sha256.v2" ,
@@ -405,15 +369,13 @@ mod tests {
405369 fn custom_to_device_serialization_roundtrip ( ) {
406370 let json = json ! ( {
407371 "transaction_id" : "456" ,
408- "method" : "m.sas.custom" ,
409372 "test" : "field" ,
410373 } ) ;
411374
412375 let content =
413376 from_json_value :: < ToDeviceKeyVerificationAcceptEventContent > ( json. clone ( ) ) . unwrap ( ) ;
414377
415378 assert_eq ! ( content. transaction_id, "456" ) ;
416- assert_eq ! ( content. method. method( ) , "m.sas.custom" ) ;
417379 let data = & * content. method . data ( ) ;
418380 assert_eq ! ( data. len( ) , 1 ) ;
419381 assert_let ! ( Some ( JsonValue :: String ( value) ) = data. get( "test" ) ) ;
0 commit comments