use std::ops::Deref;
use serde::{Deserialize, Serialize};
use crate::types::User;
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Me {
#[serde(flatten)]
pub user: User,
pub can_join_groups: bool,
pub can_read_all_group_messages: bool,
pub supports_inline_queries: bool,
}
impl Me {
#[must_use]
pub fn username(&self) -> &str {
self.user.username.as_deref().expect("Bots must have usernames")
}
#[must_use]
pub fn mention(&self) -> String {
format!("@{}", self.username())
}
#[must_use]
pub fn tme_url(&self) -> reqwest::Url {
format!("https://t.me/{}", self.username()).parse().unwrap()
}
}
impl Deref for Me {
type Target = User;
fn deref(&self) -> &Self::Target {
&self.user
}
}
#[cfg(test)]
mod tests {
use crate::types::{Me, User, UserId};
#[test]
fn convenience_methods_work() {
let me = Me {
user: User {
id: UserId(42),
is_bot: true,
first_name: "First".to_owned(),
last_name: None,
username: Some("SomethingSomethingBot".to_owned()),
language_code: None,
is_premium: false,
added_to_attachment_menu: false,
},
can_join_groups: false,
can_read_all_group_messages: false,
supports_inline_queries: false,
};
assert_eq!(me.username(), "SomethingSomethingBot");
assert_eq!(me.mention(), "@SomethingSomethingBot");
assert_eq!(me.tme_url(), "https://t.me/SomethingSomethingBot".parse().unwrap());
}
}