ruma_events/room/
name.rs

1//! Types for the [`m.room.name`] event.
2//!
3//! [`m.room.name`]: https://spec.matrix.org/latest/client-server-api/#mroomname
4
5use ruma_macros::EventContent;
6use serde::{Deserialize, Serialize};
7
8use crate::EmptyStateKey;
9
10/// The content of an `m.room.name` event.
11///
12/// The room name is a human-friendly string designed to be displayed to the end-user.
13#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
14#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
15#[ruma_event(type = "m.room.name", kind = State, state_key_type = EmptyStateKey)]
16pub struct RoomNameEventContent {
17    /// The name of the room.
18    pub name: String,
19}
20
21impl RoomNameEventContent {
22    /// Create a new `RoomNameEventContent` with the given name.
23    pub fn new(name: String) -> Self {
24        Self { name }
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
31
32    use super::RoomNameEventContent;
33    use crate::OriginalStateEvent;
34
35    #[test]
36    fn serialization() {
37        let content = RoomNameEventContent { name: "The room name".to_owned() };
38
39        let actual = to_json_value(content).unwrap();
40        let expected = json!({
41            "name": "The room name",
42        });
43
44        assert_eq!(actual, expected);
45    }
46
47    #[test]
48    fn deserialization() {
49        let json_data = json!({
50            "content": {
51                "name": "The room name"
52            },
53            "event_id": "$h29iv0s8:example.com",
54            "origin_server_ts": 1,
55            "room_id": "!n8f893n9:example.com",
56            "sender": "@carl:example.com",
57            "state_key": "",
58            "type": "m.room.name"
59        });
60
61        assert_eq!(
62            from_json_value::<OriginalStateEvent<RoomNameEventContent>>(json_data)
63                .unwrap()
64                .content
65                .name,
66            "The room name"
67        );
68    }
69}