ruma_events/room/
name.rs
1use ruma_macros::EventContent;
6use serde::{Deserialize, Serialize};
7
8use crate::EmptyStateKey;
9
10#[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 pub name: String,
19}
20
21impl RoomNameEventContent {
22 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}