matrix_sdk_crypto/types/events/
dummy.rs

1// Copyright 2023 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Types for `m.dummy` to-device events.
16
17use std::collections::BTreeMap;
18
19use serde::{Deserialize, Serialize};
20use serde_json::Value;
21
22use super::{EventType, ToDeviceEvent};
23
24/// The `m.dummy` to-device event.
25pub type DummyEvent = ToDeviceEvent<DummyEventContent>;
26
27/// The content of an `m.dummy` event.
28#[derive(Clone, Debug, Default, Serialize, Deserialize)]
29pub struct DummyEventContent {
30    /// Any other, custom and non-specced fields of the content.
31    #[serde(flatten)]
32    other: BTreeMap<String, Value>,
33}
34
35impl DummyEventContent {
36    /// Create a new `m.dummy` event content.
37    pub fn new() -> Self {
38        Default::default()
39    }
40}
41
42impl EventType for DummyEventContent {
43    const EVENT_TYPE: &'static str = "m.dummy";
44}
45
46#[cfg(test)]
47pub(super) mod tests {
48    use serde_json::{json, Value};
49
50    use super::DummyEvent;
51
52    pub fn json() -> Value {
53        json!({
54            "sender": "@alice:example.org",
55            "content": {
56                "m.custom": "something custom",
57            },
58            "type": "m.dummy",
59            "m.custom.top": "something custom in the top",
60        })
61    }
62
63    #[test]
64    fn deserialization() -> Result<(), serde_json::Error> {
65        let json = json();
66        let event: DummyEvent = serde_json::from_value(json.clone())?;
67
68        let serialized = serde_json::to_value(event)?;
69        assert_eq!(json, serialized);
70
71        Ok(())
72    }
73}