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.
1415//! Types for `m.dummy` to-device events.
1617use std::collections::BTreeMap;
1819use serde::{Deserialize, Serialize};
20use serde_json::Value;
2122use super::{EventType, ToDeviceEvent};
2324/// The `m.dummy` to-device event.
25pub type DummyEvent = ToDeviceEvent<DummyEventContent>;
2627/// 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)]
32other: BTreeMap<String, Value>,
33}
3435impl DummyEventContent {
36/// Create a new `m.dummy` event content.
37pub fn new() -> Self {
38 Default::default()
39 }
40}
4142impl EventType for DummyEventContent {
43const EVENT_TYPE: &'static str = "m.dummy";
44}
4546#[cfg(test)]
47pub(super) mod tests {
48use serde_json::{json, Value};
4950use super::DummyEvent;
5152pub fn json() -> Value {
53json!({
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 }
6263#[test]
64fn deserialization() -> Result<(), serde_json::Error> {
65let json = json();
66let event: DummyEvent = serde_json::from_value(json.clone())?;
6768let serialized = serde_json::to_value(event)?;
69assert_eq!(json, serialized);
7071Ok(())
72 }
73}