matrix_sdk_crypto/types/events/
secret_push.rs1#![allow(unused_assignments)]
22
23use std::collections::BTreeMap;
24
25use ruma::events::secret::request::SecretName;
26use serde::{Deserialize, Serialize};
27use serde_json::Value;
28use zeroize::{Zeroize, ZeroizeOnDrop};
29
30use super::{EventType, ToDeviceEvent};
31
32pub type SecretPushEvent = ToDeviceEvent<SecretPushContent>;
34
35#[derive(Clone, Serialize, Deserialize, Zeroize, ZeroizeOnDrop)]
40pub struct SecretPushContent {
41 #[zeroize(skip)]
43 pub name: SecretName,
44 pub secret: String,
46 #[serde(flatten)]
48 #[zeroize(skip)]
49 other: BTreeMap<String, Value>,
50}
51
52impl SecretPushContent {
53 pub fn new(name: SecretName, secret: String) -> Self {
55 Self { name, secret, other: Default::default() }
56 }
57}
58
59#[cfg(not(tarpaulin_include))]
60impl std::fmt::Debug for SecretPushContent {
61 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62 f.debug_struct("SecretPushContent").field("name", &self.name).finish_non_exhaustive()
63 }
64}
65
66impl EventType for SecretPushContent {
67 const EVENT_TYPE: &'static str = "io.element.msc4385.secret.push";
68}
69
70#[cfg(test)]
71pub(crate) mod tests {
72 use serde_json::{Value, json};
73
74 use super::SecretPushEvent;
75
76 pub(crate) fn json() -> Value {
77 json!({
78 "sender": "@alice:example.org",
79 "content": {
80 "name": "org.example.secret.name",
81 "secret": "ThisIsASecretDon'tTellAnyone"
82 },
83 "type": "io.element.msc4385.secret.push",
84 })
85 }
86
87 #[test]
88 fn deserialization() -> Result<(), serde_json::Error> {
89 let json = json();
90 let event: SecretPushEvent = serde_json::from_value(json.clone())?;
91
92 assert_eq!(event.content.name.as_str(), "org.example.secret.name");
93 assert_eq!(&event.content.secret, "ThisIsASecretDon'tTellAnyone");
94
95 let serialized = serde_json::to_value(event)?;
96 assert_eq!(json, serialized);
97
98 Ok(())
99 }
100}