tgbot/types/definitions/chat/
id.rs

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