tgbot/types/chat/id/
mod.rs

1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4
5use crate::types::Integer;
6
7#[cfg(test)]
8mod tests;
9
10/// ID of a chat.
11#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
12#[serde(from = "Integer", into = "Integer")]
13pub struct ChatPeerId(Integer);
14
15impl From<Integer> for ChatPeerId {
16    fn from(value: Integer) -> Self {
17        Self(value)
18    }
19}
20
21impl From<ChatPeerId> for Integer {
22    fn from(value: ChatPeerId) -> Self {
23        value.0
24    }
25}
26
27impl fmt::Display for ChatPeerId {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        self.0.fmt(f)
30    }
31}
32
33impl PartialEq<Integer> for ChatPeerId {
34    fn eq(&self, other: &Integer) -> bool {
35        self.0.eq(other)
36    }
37}
38
39/// Username of a chat in the format `@username`.
40#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
41#[serde(from = "String", into = "String")]
42pub struct ChatUsername(String);
43
44impl From<&str> for ChatUsername {
45    fn from(value: &str) -> Self {
46        Self(String::from(value))
47    }
48}
49
50impl From<String> for ChatUsername {
51    fn from(value: String) -> Self {
52        Self(value)
53    }
54}
55
56impl From<ChatUsername> for String {
57    fn from(value: ChatUsername) -> Self {
58        value.0
59    }
60}
61
62impl fmt::Display for ChatUsername {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        self.0.fmt(f)
65    }
66}
67
68impl PartialEq<String> for ChatUsername {
69    fn eq(&self, other: &String) -> bool {
70        self.0.eq(other)
71    }
72}
73
74impl PartialEq<str> for ChatUsername {
75    fn eq(&self, other: &str) -> bool {
76        self.0.eq(other)
77    }
78}
79
80/// Represents an ID or username of a chat.
81#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
82#[serde(untagged)]
83pub enum ChatId {
84    /// ID of a chat.
85    Id(ChatPeerId),
86    /// Username of a chat in the format `@username`.
87    Username(ChatUsername),
88}
89
90impl fmt::Display for ChatId {
91    fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
92        match self {
93            ChatId::Id(chat_id) => write!(out, "{}", chat_id.0),
94            ChatId::Username(username) => write!(out, "{}", username.0),
95        }
96    }
97}
98
99impl From<ChatPeerId> for ChatId {
100    fn from(value: ChatPeerId) -> Self {
101        ChatId::Id(value)
102    }
103}
104
105impl From<ChatUsername> for ChatId {
106    fn from(value: ChatUsername) -> Self {
107        ChatId::Username(value)
108    }
109}
110
111impl From<&str> for ChatId {
112    fn from(username: &str) -> ChatId {
113        ChatId::Username(String::from(username).into())
114    }
115}
116
117impl From<String> for ChatId {
118    fn from(username: String) -> ChatId {
119        ChatId::Username(username.into())
120    }
121}
122
123impl From<Integer> for ChatId {
124    fn from(id: Integer) -> ChatId {
125        ChatId::Id(id.into())
126    }
127}