ruma_events/
space_order.rs

1//! Types for the [`m.space_order`] event.
2//!
3//! [`m.space_order`]: https://github.com/matrix-org/matrix-spec-proposals/pull/3230
4
5use ruma_common::OwnedSpaceChildOrder;
6use ruma_macros::EventContent;
7use serde::{Deserialize, Serialize};
8
9/// The content of an `m.space_order` event.
10///
11/// Whether the space has been explicitly ordered.
12///
13/// This event appears in the user's room account data for the space room in
14/// question.
15#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
16#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
17#[ruma_event(type = "org.matrix.msc3230.space_order", alias = "m.space_order", kind = RoomAccountData)]
18pub struct SpaceOrderEventContent {
19    /// The current space order.
20    pub order: OwnedSpaceChildOrder,
21}
22
23impl SpaceOrderEventContent {
24    /// Creates a new `SpaceOrderEventContent` with the given order.
25    pub fn new(order: OwnedSpaceChildOrder) -> Self {
26        Self { order }
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use assert_matches2::assert_matches;
33    use ruma_common::SpaceChildOrder;
34    use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
35
36    use super::SpaceOrderEventContent;
37    use crate::{AnyRoomAccountDataEvent, RoomAccountDataEvent};
38
39    #[test]
40    fn deserialize() {
41        let raw_unstable_space_order = json!({
42            "type": "org.matrix.msc3230.space_order",
43            "content": {
44                "order": "a",
45            },
46        });
47        let unstable_space_order_account_data =
48            from_json_value::<AnyRoomAccountDataEvent>(raw_unstable_space_order).unwrap();
49        assert_matches!(
50            unstable_space_order_account_data,
51            AnyRoomAccountDataEvent::SpaceOrder(unstable_space_order)
52        );
53        assert_eq!(unstable_space_order.content.order, SpaceChildOrder::parse("a").unwrap());
54
55        let raw_space_order = json!({
56            "type": "m.space_order",
57            "content": {
58                "order": "b",
59            },
60        });
61        let space_order_account_data =
62            from_json_value::<AnyRoomAccountDataEvent>(raw_space_order).unwrap();
63        assert_matches!(space_order_account_data, AnyRoomAccountDataEvent::SpaceOrder(space_order));
64        assert_eq!(space_order.content.order, SpaceChildOrder::parse("b").unwrap());
65    }
66
67    #[test]
68    fn serialize() {
69        let space_order = SpaceOrderEventContent::new(SpaceChildOrder::parse("a").unwrap());
70        let space_order_account_data = RoomAccountDataEvent { content: space_order };
71        assert_eq!(
72            to_json_value(space_order_account_data).unwrap(),
73            json!({
74                "type": "org.matrix.msc3230.space_order",
75                "content": {
76                    "order": "a",
77                },
78            })
79        );
80    }
81}