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 ruma_common::canonical_json::assert_to_canonical_json_eq;
31    use serde_json::{from_value as from_json_value, json};
32
33    use super::RoomNameEventContent;
34    use crate::OriginalStateEvent;
35
36    #[test]
37    fn serialization() {
38        let content = RoomNameEventContent { name: "The room name".to_owned() };
39
40        assert_to_canonical_json_eq!(
41            content,
42            json!({
43                "name": "The room name",
44            }),
45        );
46    }
47
48    #[test]
49    fn deserialization() {
50        let json_data = json!({
51            "content": {
52                "name": "The room name"
53            },
54            "event_id": "$h29iv0s8:example.com",
55            "origin_server_ts": 1,
56            "room_id": "!n8f893n9:example.com",
57            "sender": "@carl:example.com",
58            "state_key": "",
59            "type": "m.room.name"
60        });
61
62        assert_eq!(
63            from_json_value::<OriginalStateEvent<RoomNameEventContent>>(json_data)
64                .unwrap()
65                .content
66                .name,
67            "The room name"
68        );
69    }
70}