Skip to main content

ruma_client_api/
retention.rs

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