use std::fmt::{Display, Formatter};
use serde::{Serialize, Serializer};
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ChatId {
Id(i64),
Username(String),
}
impl ChatId {
#[must_use]
pub const fn id(id: i64) -> Self {
Self::Id(id)
}
#[must_use]
pub fn username(username: impl AsRef<str>) -> Self {
let username: &str = username.as_ref();
if username.starts_with('@') {
Self::Username(username.to_string())
} else {
Self::Username(format!("@{username}"))
}
}
}
impl Display for ChatId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Id(id) => write!(f, "{id}"),
Self::Username(username) => write!(f, "{username}"),
}
}
}
impl Serialize for ChatId {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&self.to_string())
}
}
impl From<i64> for ChatId {
fn from(id: i64) -> Self {
Self::Id(id)
}
}
impl From<&str> for ChatId {
fn from(value: &str) -> Self {
let value: &str = value.trim();
value
.parse::<i64>()
.map_or_else(|_| Self::username(value), Self::Id)
}
}
impl From<String> for ChatId {
fn from(value: String) -> Self {
Self::from(value.as_str())
}
}
#[cfg(test)]
mod tests {
use super::ChatId;
#[test]
fn numeric_id_round_trips() {
let expected: ChatId = ChatId::Id(7_518_714_136);
let actual: ChatId = ChatId::from(7_518_714_136_i64);
assert_eq!(expected, actual);
}
#[test]
fn negative_group_id_is_preserved() {
let expected: ChatId = ChatId::Id(-1_001_234_567_890);
let actual: ChatId = ChatId::from(-1_001_234_567_890_i64);
assert_eq!(expected, actual);
}
#[test]
fn username_gains_a_leading_at_sign() {
let expected: ChatId = ChatId::Username(String::from("@mychannel"));
let actual: ChatId = ChatId::username("mychannel");
assert_eq!(expected, actual);
}
#[test]
fn username_keeps_an_existing_at_sign() {
let expected: ChatId = ChatId::Username(String::from("@mychannel"));
let actual: ChatId = ChatId::username("@mychannel");
assert_eq!(expected, actual);
}
#[test]
fn numeric_string_parses_as_an_id() {
let expected: ChatId = ChatId::Id(7_518_714_136);
let actual: ChatId = ChatId::from("7518714136");
assert_eq!(expected, actual);
}
#[test]
fn non_numeric_string_parses_as_a_username() {
let expected: ChatId = ChatId::Username(String::from("@mychannel"));
let actual: ChatId = ChatId::from("@mychannel");
assert_eq!(expected, actual);
}
#[test]
fn numeric_id_serializes_as_a_string() {
let expected: String = String::from("\"7518714136\"");
let actual: String =
serde_json::to_string(&ChatId::Id(7_518_714_136)).expect("chat id should serialize");
assert_eq!(expected, actual);
}
#[test]
fn username_serializes_as_a_string() {
let expected: String = String::from("\"@mychannel\"");
let actual: String = serde_json::to_string(&ChatId::username("mychannel"))
.expect("chat id should serialize");
assert_eq!(expected, actual);
}
}