Skip to main content

ruma_client_api/
retention.rs

1//! Endpoints for managing message retention periods
2
3use std::str::FromStr;
4
5use ruma_common::{
6    OwnedRoomId,
7    serde::{DisplayAsRefStr, SerializeAsRefStr},
8};
9use serde::{
10    Deserialize, Deserializer,
11    de::{self, Unexpected},
12};
13
14pub mod get_retention_configuration;
15
16/// Represents one or all rooms of a homeserver.
17#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, DisplayAsRefStr, SerializeAsRefStr)]
18#[allow(clippy::exhaustive_enums)]
19pub enum RoomIdOrAllRooms {
20    /// Represents a specific room ID.
21    RoomId(OwnedRoomId),
22
23    /// Represents all rooms on a homeserver.
24    AllRooms,
25}
26
27impl RoomIdOrAllRooms {
28    /// Get the string representation of [`RoomIdOrAllRooms`].
29    ///
30    /// Returns the string representation of the room ID for the [`RoomIdOrAllRooms::RoomId`]
31    /// variant, or "*" for the [`RoomIdOrAllRooms::AllRooms`] variant.
32    pub fn as_str(&self) -> &str {
33        match self {
34            Self::RoomId(room_id) => room_id.as_str(),
35            Self::AllRooms => "*",
36        }
37    }
38}
39
40impl AsRef<str> for RoomIdOrAllRooms {
41    fn as_ref(&self) -> &str {
42        self.as_str()
43    }
44}
45
46impl From<OwnedRoomId> for RoomIdOrAllRooms {
47    fn from(r: OwnedRoomId) -> Self {
48        RoomIdOrAllRooms::RoomId(r)
49    }
50}
51
52impl TryFrom<&str> for RoomIdOrAllRooms {
53    type Error = &'static str;
54
55    fn try_from(room_id_or_wildcard: &str) -> Result<Self, Self::Error> {
56        if room_id_or_wildcard.is_empty() {
57            Err("The Room identifier cannot be empty")
58        } else if "*" == room_id_or_wildcard {
59            Ok(RoomIdOrAllRooms::AllRooms)
60        } else {
61            Ok(RoomIdOrAllRooms::RoomId(
62                room_id_or_wildcard
63                    .try_into()
64                    .map_err(|_| "The Room identifier needs to be a valid room id or *")?,
65            ))
66        }
67    }
68}
69
70impl FromStr for RoomIdOrAllRooms {
71    type Err = &'static str;
72
73    fn from_str(s: &str) -> Result<Self, Self::Err> {
74        s.try_into()
75    }
76}
77
78impl<'de> Deserialize<'de> for RoomIdOrAllRooms {
79    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
80    where
81        D: Deserializer<'de>,
82    {
83        let s = ruma_common::serde::deserialize_cow_str(deserializer)?;
84        RoomIdOrAllRooms::try_from(s.as_ref())
85            .map_err(|_| de::Error::invalid_value(Unexpected::Str(&s), &"a valid room ID or '*'"))
86    }
87}