Skip to content

Commit

Permalink
Merge pull request #688 from teloxide/dev
Browse files Browse the repository at this point in the history
Merge v0.10.1
  • Loading branch information
Hirrolot committed Jul 22, 2022
2 parents 9f1a530 + b80ae1d commit ba545c3
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 7 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## unreleased

## 0.10.1 - 2022-07-22

### Fixed

- Mark the following functions with `#[must_use]` ([PR 457](https://github.com/teloxide/teloxide/pull/457)):
- `TraceStorage::into_inner`.
- `AsyncStopToken::new_pair`.
- `AsyncStopFlag::is_stopped`.
- All from `crate::utils::{html, markdown}`.
- Rendering of GIFs in lib.rs and crates.io ([PR 681](https://github.com/teloxide/teloxide/pull/681)).

## 0.10.0 - 2022-07-21

### Added
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "teloxide"
version = "0.10.0"
version = "0.10.1"
edition = "2021"
description = "An elegant Telegram bots framework for Rust"
repository = "https://github.com/teloxide/teloxide"
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
> [v0.9 -> v0.10 migration guide >>](MIGRATION_GUIDE.md#09---010)
<div align="center">
<img src="../../raw/master/ICON.png" width="250"/>
<img src="./ICON.png" width="250"/>
<h1>teloxide</h1>
<a href="https://docs.rs/teloxide/">
<img src="https://docs.rs/teloxide/badge.svg">
Expand Down Expand Up @@ -103,7 +103,7 @@ async fn main() {
```

<div align="center">
<img src="../../raw/master/media/throw-dice.gif" width="420" />
<img src="./media/throw-dice.gif" width="420" />
</div>

### Commands
Expand Down Expand Up @@ -171,7 +171,7 @@ async fn answer(
```

<div align="center">
<img src="../../raw/master/media/command.gif" width="420" />
<img src="./media/command.gif" width="420" />
</div>

### Dialogues management
Expand Down Expand Up @@ -294,7 +294,7 @@ async fn receive_location(
```

<div align="center">
<img src="../../raw/master/media/dialogue.gif" width="420" />
<img src="./media/dialogue.gif" width="420" />
</div>

[More examples >>](examples/)
Expand Down Expand Up @@ -350,12 +350,13 @@ Feel free to propose your own bot to our collection!
- [`Hermitter/tepe`](https://github.com/Hermitter/tepe) — A CLI to command a bot to send messages and files over Telegram.
- [`myblackbeard/basketball-betting-bot`](https://github.com/myblackbeard/basketball-betting-bot) — The bot lets you bet on NBA games against your buddies.
- [`dracarys18/grpmr-rs`](https://github.com/dracarys18/grpmr-rs) — Modular Telegram Group Manager Bot written in Rust.
- [`ArtHome12/vzmuinebot`](https://github.com/ArtHome12/vzmuinebot) — Telegram bot for food menu navigate.
- [`ArtHome12/cognito_bot`](https://github.com/ArtHome12/cognito_bot) — The bot is designed to anonymize messages to a group.
- [`crapstone/hsctt`](https://codeberg.org/crapstones-bots/hsctt) — A bot that converts HTTP status codes into text.

</details>

See [600+ other public repositories using teloxide >>](https://github.com/teloxide/teloxide/network/dependents)

## Contributing

See [`CONRIBUTING.md`](CONTRIBUTING.md).
3 changes: 2 additions & 1 deletion src/dispatching/dialogue/storage/trace_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ pub struct TraceStorage<S> {
}

impl<S> TraceStorage<S> {
#[must_use]
#[must_use = "This function is pure, that is does nothing unless its output is used"]
pub fn new(inner: Arc<S>) -> Arc<Self> {
Arc::new(Self { inner })
}

#[must_use = "This function is pure, that is does nothing unless its output is used"]
pub fn into_inner(self) -> Arc<S> {
self.inner
}
Expand Down
2 changes: 2 additions & 0 deletions src/dispatching/stop_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct AsyncStopFlag(#[pin] Abortable<Pending<()>>);

impl AsyncStopToken {
/// Create a new token/flag pair.
#[must_use = "This function is pure, that is does nothing unless its output is used"]
pub fn new_pair() -> (Self, AsyncStopFlag) {
let (handle, reg) = AbortHandle::new_pair();
let token = Self(handle);
Expand All @@ -56,6 +57,7 @@ impl StopToken for AsyncStopToken {

impl AsyncStopFlag {
/// Returns true if the stop token linked to `self` was used.
#[must_use = "This function is pure, that is does nothing unless its output is used"]
pub fn is_stopped(&self) -> bool {
self.0.is_aborted()
}
Expand Down
22 changes: 22 additions & 0 deletions src/utils/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use teloxide_core::types::User;
///
/// Passed string will not be automatically escaped because it can contain
/// nested markup.
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn bold(s: &str) -> String {
format!("<b>{}</b>", s)
}
Expand All @@ -16,6 +18,8 @@ pub fn bold(s: &str) -> String {
///
/// Passed string will not be automatically escaped because it can contain
/// nested markup.
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn italic(s: &str) -> String {
format!("<i>{}</i>", s)
}
Expand All @@ -24,6 +28,8 @@ pub fn italic(s: &str) -> String {
///
/// Passed string will not be automatically escaped because it can contain
/// nested markup.
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn underline(s: &str) -> String {
format!("<u>{}</u>", s)
}
Expand All @@ -32,32 +38,42 @@ pub fn underline(s: &str) -> String {
///
/// Passed string will not be automatically escaped because it can contain
/// nested markup.
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn strike(s: &str) -> String {
format!("<s>{}</s>", s)
}

/// Builds an inline link with an anchor.
///
/// Escapes the passed URL and the link text.
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn link(url: &str, text: &str) -> String {
format!("<a href=\"{}\">{}</a>", escape(url), escape(text))
}

/// Builds an inline user mention link with an anchor.
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn user_mention(user_id: i64, text: &str) -> String {
link(format!("tg://user?id={}", user_id).as_str(), text)
}

/// Formats the code block.
///
/// Escapes HTML characters inside the block.
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn code_block(code: &str) -> String {
format!("<pre>{}</pre>", escape(code))
}

/// Formats the code block with a specific language syntax.
///
/// Escapes HTML characters inside the block.
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn code_block_with_lang(code: &str, lang: &str) -> String {
format!(
"<pre><code class=\"language-{}\">{}</code></pre>",
Expand All @@ -69,6 +85,8 @@ pub fn code_block_with_lang(code: &str, lang: &str) -> String {
/// Formats the string as an inline code.
///
/// Escapes HTML characters inside the block.
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn code_inline(s: &str) -> String {
format!("<code>{}</code>", escape(s))
}
Expand All @@ -80,10 +98,14 @@ pub fn code_inline(s: &str) -> String {
/// they shoudn't be escaped by the [spec].
///
/// [spec]: https://core.telegram.org/bots/api#html-style
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn escape(s: &str) -> String {
s.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;")
}

#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn user_mention_or_link(user: &User) -> String {
match user.mention() {
Some(mention) => mention,
Expand Down
26 changes: 26 additions & 0 deletions src/utils/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use teloxide_core::types::User;
///
/// Passed string will not be automatically escaped because it can contain
/// nested markup.
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn bold(s: &str) -> String {
format!("*{}*", s)
}
Expand All @@ -17,6 +19,8 @@ pub fn bold(s: &str) -> String {
/// Can be safely used with `utils::markdown::underline()`.
/// Passed string will not be automatically escaped because it can contain
/// nested markup.
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn italic(s: &str) -> String {
if s.starts_with("__") && s.ends_with("__") {
format!(r"_{}\r__", &s[..s.len() - 1])
Expand All @@ -30,6 +34,8 @@ pub fn italic(s: &str) -> String {
/// Can be safely used with `utils::markdown::italic()`.
/// Passed string will not be automatically escaped because it can contain
/// nested markup.
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn underline(s: &str) -> String {
// In case of ambiguity between italic and underline entities
// ‘__’ is always greadily treated from left to right as beginning or end of
Expand All @@ -47,39 +53,51 @@ pub fn underline(s: &str) -> String {
///
/// Passed string will not be automatically escaped because it can contain
/// nested markup.
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn strike(s: &str) -> String {
format!("~{}~", s)
}

/// Builds an inline link with an anchor.
///
/// Escapes `)` and ``` characters inside the link url.
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn link(url: &str, text: &str) -> String {
format!("[{}]({})", text, escape_link_url(url))
}

/// Builds an inline user mention link with an anchor.
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn user_mention(user_id: i64, text: &str) -> String {
link(format!("tg://user?id={}", user_id).as_str(), text)
}

/// Formats the code block.
///
/// Escapes ``` and `\` characters inside the block.
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn code_block(code: &str) -> String {
format!("```\n{}\n```", escape_code(code))
}

/// Formats the code block with a specific language syntax.
///
/// Escapes ``` and `\` characters inside the block.
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn code_block_with_lang(code: &str, lang: &str) -> String {
format!("```{}\n{}\n```", escape(lang), escape_code(code))
}

/// Formats the string as an inline code.
///
/// Escapes ``` and `\` characters inside the block.
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn code_inline(s: &str) -> String {
format!("`{}`", escape_code(s))
}
Expand All @@ -88,6 +106,8 @@ pub fn code_inline(s: &str) -> String {
/// v2][spec] message style.
///
/// [spec]: https://core.telegram.org/bots/api#html-style
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn escape(s: &str) -> String {
s.replace('_', r"\_")
.replace('*', r"\*")
Expand All @@ -111,16 +131,22 @@ pub fn escape(s: &str) -> String {

/// Escapes all markdown special characters specific for the inline link URL
/// (``` and `)`).
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn escape_link_url(s: &str) -> String {
s.replace('`', r"\`").replace(')', r"\)")
}

/// Escapes all markdown special characters specific for the code block (``` and
/// `\`).
#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn escape_code(s: &str) -> String {
s.replace('\\', r"\\").replace('`', r"\`")
}

#[must_use = "This function returns a new string, rather than mutating the argument, so calling it \
without using its output does nothing useful"]
pub fn user_mention_or_link(user: &User) -> String {
match user.mention() {
Some(mention) => mention,
Expand Down

0 comments on commit ba545c3

Please sign in to comment.