From b3c0d00b7d4a61550e214612bd5d4f1c76bc7149 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sat, 2 Apr 2022 22:56:32 +0400 Subject: [PATCH] "override" `mention` and `tme_url` on `Me` That's not **really** an override, but by adding an inherent methods we "hide" methods from `Deref`, that allows us to return non-option types from `me.mention()` and `me.tme_url()` -- bots must always have usernames. --- CHANGELOG.md | 1 + src/types/me.rs | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f9f518f6..efe3a738 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `UserId::{url, is_anonymous, is_channel, is_telegram}` convenience functions ([#197][pr197]) - `User::{tme_url, preferably_tme_url}` convenience functions ([#197][pr197]) - `Me::username` and `Deref` implementation for `Me` ([#197][pr197]) +- `Me::{mention, tme_url}` ([#197][pr197]) [pr197]: https://github.com/teloxide/teloxide-core/pull/197 diff --git a/src/types/me.rs b/src/types/me.rs index d404690a..5bc6a7cd 100644 --- a/src/types/me.rs +++ b/src/types/me.rs @@ -32,6 +32,16 @@ impl Me { .as_deref() .expect("Bots must have usernames") } + + /// Returns a username mention of this bot. + pub fn mention(&self) -> String { + format!("@{}", self.username()) + } + + /// Returns an URL that links to this bot in the form of `t.me/<...>`. + pub fn tme_url(&self) -> reqwest::Url { + format!("https://t.me/{}", self.username()).parse().unwrap() + } } impl Deref for Me {