Skip to main content

teloxide/utils/
command.rs

1//! Command parsers.
2//!
3//! You can either create an `enum` with derived [`BotCommands`], containing
4//! commands of your bot, or use functions, which split input text into a string
5//! command with its arguments.
6//!
7//! # Using BotCommands
8//!
9//! ```
10//! # #[cfg(feature = "macros")] {
11//! use teloxide::utils::command::BotCommands;
12//!
13//! type UnitOfTime = u8;
14//!
15//! #[derive(BotCommands, PartialEq, Debug)]
16//! #[command(rename_rule = "lowercase", parse_with = "split")]
17//! enum AdminCommand {
18//!     Mute(UnitOfTime, char),
19//!     Ban(UnitOfTime, char),
20//! }
21//!
22//! let command = AdminCommand::parse("/ban 5 h", "bot_name").unwrap();
23//! assert_eq!(command, AdminCommand::Ban(5, 'h'));
24//! # }
25//! ```
26//!
27//! # Using parse_command
28//!
29//! ```
30//! use teloxide::utils::command::parse_command;
31//!
32//! let (command, args) = parse_command("/ban@MyBotName 3 hours", "MyBotName").unwrap();
33//! assert_eq!(command, "ban");
34//! assert_eq!(args, vec!["3", "hours"]);
35//! ```
36//!
37//! # Using parse_command_with_prefix
38//!
39//! ```
40//! use teloxide::utils::command::parse_command_with_prefix;
41//!
42//! let text = "!ban 3 hours";
43//! let (command, args) = parse_command_with_prefix("!", text, "").unwrap();
44//! assert_eq!(command, "ban");
45//! assert_eq!(args, vec!["3", "hours"]);
46//! ```
47//!
48//! See [examples/admin] as a more complicated examples.
49//!
50//! [examples/admin]: https://github.com/teloxide/teloxide/blob/master/crates/teloxide/examples/admin.rs
51
52use core::fmt;
53use std::{
54    error::Error,
55    fmt::{Display, Formatter, Write},
56};
57
58use teloxide_core::types::{BotCommand, BotFullInfo};
59#[cfg(feature = "macros")]
60pub use teloxide_macros::BotCommands;
61
62/// An enumeration of bot's commands.
63///
64/// # Example
65/// ```
66/// # #[cfg(feature = "macros")] {
67/// use teloxide::utils::command::BotCommands;
68///
69/// type UnitOfTime = u8;
70///
71/// #[derive(BotCommands, PartialEq, Debug)]
72/// #[command(rename_rule = "lowercase", parse_with = "split")]
73/// enum AdminCommand {
74///     Mute(UnitOfTime, char),
75///     Ban(UnitOfTime, char),
76/// }
77///
78/// let command = AdminCommand::parse("/ban 5 h", "bot_name").unwrap();
79/// assert_eq!(command, AdminCommand::Ban(5, 'h'));
80/// # }
81/// ```
82///
83/// # Enum attributes
84///  1. `#[command(rename_rule = "rule")]` Rename all commands by `rule`.
85///     Allowed rules are `lowercase`, `UPPERCASE`, `PascalCase`, `camelCase`,
86///     `snake_case`, `SCREAMING_SNAKE_CASE`, `kebab-case`, and
87///     `SCREAMING-KEBAB-CASE`.
88///
89///  2. `#[command(prefix = "prefix")]` Change a prefix for all commands (the
90///     default is `/`).
91///
92///  3. `#[command(description = "description")]` and `/// description` Add a
93///     summary description of commands before all commands.
94///
95///  4. `#[command(parse_with = "parser")]` Change the parser of arguments.
96///     Possible values:
97///       - `default` - the same as the unspecified parser. It only puts all
98///         text after the first space into the first argument, which must
99///         implement [`FromStr`].
100///
101/// ## Example
102/// ```
103/// # #[cfg(feature = "macros")] {
104/// use teloxide::utils::command::BotCommands;
105///
106/// #[derive(BotCommands, PartialEq, Debug)]
107/// #[command(rename_rule = "lowercase")]
108/// enum Command {
109///     Text(String),
110/// }
111///
112/// let command = Command::parse("/text hello my dear friend!", "").unwrap();
113/// assert_eq!(command, Command::Text("hello my dear friend!".to_string()));
114/// # }
115/// ```
116///
117///  - `split` - separates a message by a given separator (the default is the
118///    space character) and parses each part into the corresponding arguments,
119///    which must implement [`FromStr`].
120///
121/// ## Example
122/// ```
123/// # #[cfg(feature = "macros")] {
124/// use teloxide::utils::command::BotCommands;
125///
126/// #[derive(BotCommands, PartialEq, Debug)]
127/// #[command(rename_rule = "lowercase", parse_with = "split")]
128/// enum Command {
129///     Nums(u8, u16, i32),
130/// }
131///
132/// let command = Command::parse("/nums 1 32 -5", "").unwrap();
133/// assert_eq!(command, Command::Nums(1, 32, -5));
134/// # }
135/// ```
136///
137/// 5. `#[command(separator = "sep")]` Specify separator used by the `split`
138///    parser. It will be ignored when accompanied by another type of parsers.
139///
140/// ## Example
141/// ```
142/// # #[cfg(feature = "macros")] {
143/// use teloxide::utils::command::BotCommands;
144///
145/// #[derive(BotCommands, PartialEq, Debug)]
146/// #[command(rename_rule = "lowercase", parse_with = "split", separator = "|")]
147/// enum Command {
148///     Nums(u8, u16, i32),
149/// }
150///
151/// let command = Command::parse("/nums 1|32|5", "").unwrap();
152/// assert_eq!(command, Command::Nums(1, 32, 5));
153/// # }
154/// ```
155///
156/// 6. `#[command(command_separator = "sep")]` Specify separator between command
157///    and args. Default is a space character.
158///
159/// ## Example
160/// ```
161/// # #[cfg(feature = "macros")] {
162/// use teloxide::utils::command::BotCommands;
163///
164/// #[derive(BotCommands, PartialEq, Debug)]
165/// #[command(
166///     rename_rule = "lowercase",
167///     parse_with = "split",
168///     separator = "_",
169///     command_separator = "_"
170/// )]
171/// enum Command {
172///     Nums(u8, u16, i32),
173/// }
174///
175/// let command = Command::parse("/nums_1_32_5", "").unwrap();
176/// assert_eq!(command, Command::Nums(1, 32, 5));
177/// # }
178/// ```
179///
180/// # Variant attributes
181/// All variant attributes override the corresponding `enum` attributes.
182///
183///  1. `#[command(rename_rule = "rule")]` Rename one command by a rule. Allowed
184///     rules are `lowercase`, `UPPERCASE`, `PascalCase`, `camelCase`,
185///     `snake_case`, `SCREAMING_SNAKE_CASE`, `kebab-case`,
186///     `SCREAMING-KEBAB-CASE`.
187///
188///  2. `#[command(rename = "name")]` Rename one command to `name` (literal
189///     renaming; do not confuse with `rename_rule`).
190///
191///  3. `#[command(description = "description")]` and `/// description` Give
192///     your command a description. It will be shown in the help message.
193///
194///  4. `#[command(parse_with = "parser")]` Parse arguments of one command with
195///     a given parser. `parser` must be a function of the signature `fn(String)
196///     -> Result<Tuple, ParseError>`, where `Tuple` corresponds to the
197///     variant's arguments.
198///
199///  5. `#[command(hide)]` Hide a command from the help message. It will still
200///     be parsed.
201///
202/// 6. `#[command(alias = "alias")]` Add an alias to a command. It will be shown
203///    in the help message.
204///
205/// 7. `#[command(aliases = ["alias1", "alias2"])]` Add multiple aliases to a
206///    command. They will be shown in the help message.
207///
208/// 8. `#[command(hide_aliases)]` Hide all aliases of a command from the help
209///    message.
210///
211/// ## Example
212/// ```
213/// # #[cfg(feature = "macros")] {
214/// use teloxide::utils::command::{BotCommands, ParseError};
215///
216/// fn accept_two_digits(input: String) -> Result<(u8,), ParseError> {
217///     match input.len() {
218///         2 => {
219///             let num = input.parse::<u8>().map_err(|e| ParseError::IncorrectFormat(e.into()))?;
220///             Ok((num,))
221///         }
222///         len => Err(ParseError::Custom(format!("Only 2 digits allowed, not {}", len).into())),
223///     }
224/// }
225///
226/// #[derive(BotCommands, PartialEq, Debug)]
227/// #[command(rename_rule = "lowercase")]
228/// enum Command {
229///     #[command(parse_with = accept_two_digits)]
230///     Num(u8),
231/// }
232///
233/// let command = Command::parse("/num 12", "").unwrap();
234/// assert_eq!(command, Command::Num(12));
235/// let command = Command::parse("/num 333", "");
236/// assert!(command.is_err());
237/// # }
238/// ```
239///
240///  5. `#[command(prefix = "prefix")]`
241///  6. `#[command(separator = "sep")]`
242///
243/// These attributes just override the corresponding `enum` attributes for a
244/// specific variant.
245///
246/// [`FromStr`]: https://doc.rust-lang.org/std/str/trait.FromStr.html
247/// [`BotCommands`]: crate::utils::command::BotCommands
248pub trait BotCommands: Sized {
249    /// Parses a command.
250    ///
251    /// `bot_username` is required to parse commands like
252    /// `/cmd@username_of_the_bot`.
253    fn parse(s: &str, bot_username: &str) -> Result<Self, ParseError>;
254
255    /// Returns descriptions of the commands suitable to be shown to the user
256    /// (for example when `/help` command is used).
257    fn descriptions() -> CommandDescriptions<'static>;
258
259    /// Returns a vector of [`BotCommand`] that can be used with
260    /// [`set_my_commands`].
261    ///
262    /// [`BotCommand`]: crate::types::BotCommand
263    /// [`set_my_commands`]: crate::requests::Requester::set_my_commands
264    fn bot_commands() -> Vec<BotCommand>;
265}
266
267pub type PrefixedBotCommand = String;
268pub type BotName = String;
269
270/// Errors returned from [`BotCommands::parse`].
271///
272/// [`BotCommands::parse`]: BotCommands::parse
273#[derive(Debug)]
274pub enum ParseError {
275    TooFewArguments {
276        expected: usize,
277        found: usize,
278        message: String,
279    },
280    TooManyArguments {
281        expected: usize,
282        found: usize,
283        message: String,
284    },
285
286    /// Redirected from [`FromStr::from_str`].
287    ///
288    /// [`FromStr::from_str`]: https://doc.rust-lang.org/std/str/trait.FromStr.html#tymethod.from_str
289    IncorrectFormat(Box<dyn Error + Send + Sync + 'static>),
290
291    UnknownCommand(PrefixedBotCommand),
292    WrongBotName(BotName),
293
294    /// A custom error which you can return from your custom parser.
295    Custom(Box<dyn Error + Send + Sync + 'static>),
296}
297
298/// Command descriptions that can be shown to the user (e.g. as a part of
299/// `/help` message)
300///
301/// Most of the time you don't need to create this struct yourself as it's
302/// returned from [`BotCommands::descriptions`].
303#[derive(Debug, Clone)]
304pub struct CommandDescriptions<'a> {
305    global_description: Option<&'a str>,
306    descriptions: &'a [CommandDescription<'a>],
307    bot_username: Option<&'a str>,
308}
309
310/// Description of a particular command, used in [`CommandDescriptions`].
311#[derive(Debug, Clone)]
312pub struct CommandDescription<'a> {
313    /// Prefix of the command, usually `/`.
314    pub prefix: &'a str,
315    /// The command itself, e.g. `start`.
316    pub command: &'a str,
317    /// The command aliases, e.g. `["help", "h"]`.
318    pub aliases: &'a [&'a str],
319    /// Human-readable description of the command.
320    pub description: &'a str,
321}
322
323impl<'a> CommandDescriptions<'a> {
324    /// Creates new [`CommandDescriptions`] from a list of command descriptions.
325    #[must_use]
326    pub const fn new(descriptions: &'a [CommandDescription<'a>]) -> Self {
327        Self { global_description: None, descriptions, bot_username: None }
328    }
329
330    /// Sets the global description of these commands.
331    #[must_use]
332    pub fn global_description(self, global_description: &'a str) -> Self {
333        Self { global_description: Some(global_description), ..self }
334    }
335
336    /// Sets the username of the bot.
337    ///
338    /// After this method is called, returned instance of
339    /// [`CommandDescriptions`] will append `@bot_username` to all commands.
340    /// This is useful in groups, to disambiguate commands for different bots.
341    ///
342    /// ## Examples
343    ///
344    /// ```
345    /// use teloxide::utils::command::{CommandDescription, CommandDescriptions};
346    ///
347    /// let descriptions = CommandDescriptions::new(&[
348    ///     CommandDescription {
349    ///         prefix: "/",
350    ///         command: "start",
351    ///         description: "start this bot",
352    ///         aliases: &[],
353    ///     },
354    ///     CommandDescription {
355    ///         prefix: "/",
356    ///         command: "help",
357    ///         description: "show this message",
358    ///         aliases: &[],
359    ///     },
360    /// ]);
361    ///
362    /// assert_eq!(descriptions.to_string(), "/start — start this bot\n/help — show this message");
363    /// assert_eq!(
364    ///     descriptions.username("username_of_the_bot").to_string(),
365    ///     "/start@username_of_the_bot — start this bot\n/help@username_of_the_bot — show this \
366    ///      message"
367    /// );
368    /// ```
369    #[must_use]
370    pub fn username(self, bot_username: &'a str) -> Self {
371        Self { bot_username: Some(bot_username), ..self }
372    }
373
374    /// Sets the username of the bot.
375    ///
376    /// This is the same as [`username`], but uses value returned from `get_me`
377    /// method to get the username.
378    ///
379    /// [`username`]: self::CommandDescriptions::username
380    #[must_use]
381    pub fn username_from_me(self, me: &'a BotFullInfo) -> CommandDescriptions<'a> {
382        match me.username() {
383            Some(username) => self.username(username),
384            None => self,
385        }
386    }
387}
388
389/// Parses a string into a command with args.
390///
391/// This function is just a shortcut for calling [`parse_command_with_prefix`]
392/// with the default prefix `/`.
393///
394/// ## Example
395/// ```
396/// use teloxide::utils::command::parse_command;
397///
398/// let text = "/mute@my_admin_bot 5 hours";
399/// let (command, args) = parse_command(text, "my_admin_bot").unwrap();
400/// assert_eq!(command, "mute");
401/// assert_eq!(args, vec!["5", "hours"]);
402/// ```
403///
404/// If the name of a bot does not match, it will return `None`:
405/// ```
406/// use teloxide::utils::command::parse_command;
407///
408/// let result = parse_command("/ban@MyNameBot1 3 hours", "MyNameBot2");
409/// assert!(result.is_none());
410/// ```
411///
412/// [`parse_command_with_prefix`]:
413/// crate::utils::command::parse_command_with_prefix
414pub fn parse_command<N>(text: &str, bot_name: N) -> Option<(&str, Vec<&str>)>
415where
416    N: AsRef<str>,
417{
418    parse_command_with_prefix("/", text, bot_name)
419}
420
421/// Parses a string into a command with args (custom prefix).
422///
423/// `prefix`: symbols, which denote start of a command.
424///
425/// ## Example
426/// ```
427/// use teloxide::utils::command::parse_command_with_prefix;
428///
429/// let text = "!mute 5 hours";
430/// let (command, args) = parse_command_with_prefix("!", text, "").unwrap();
431/// assert_eq!(command, "mute");
432/// assert_eq!(args, vec!["5", "hours"]);
433/// ```
434///
435/// If the name of a bot does not match, it will return `None`:
436/// ```
437/// use teloxide::utils::command::parse_command_with_prefix;
438///
439/// let result = parse_command_with_prefix("!", "!ban@MyNameBot1 3 hours", "MyNameBot2");
440/// assert!(result.is_none());
441/// ```
442pub fn parse_command_with_prefix<'a, N>(
443    prefix: &str,
444    text: &'a str,
445    bot_name: N,
446) -> Option<(&'a str, Vec<&'a str>)>
447where
448    N: AsRef<str>,
449{
450    if !text.starts_with(prefix) {
451        return None;
452    }
453    let mut words = text.split_whitespace();
454    let mut split = words.next()?[prefix.len()..].split('@');
455    let command = split.next()?;
456    let bot = split.next();
457    match bot {
458        Some(name) if name.eq_ignore_ascii_case(bot_name.as_ref()) => {}
459        None => {}
460        _ => return None,
461    }
462    Some((command, words.collect()))
463}
464
465impl Display for ParseError {
466    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
467        match self {
468            ParseError::TooFewArguments { expected, found, message } => write!(
469                f,
470                "Too few arguments (expected {expected}, found {found}, message = '{message}')"
471            ),
472            ParseError::TooManyArguments { expected, found, message } => write!(
473                f,
474                "Too many arguments (expected {expected}, found {found}, message = '{message}')"
475            ),
476            ParseError::IncorrectFormat(e) => write!(f, "Incorrect format of command args: {e}"),
477            ParseError::UnknownCommand(e) => write!(f, "Unknown command: {e}"),
478            ParseError::WrongBotName(n) => write!(f, "Wrong bot name: {n}"),
479            ParseError::Custom(e) => write!(f, "{e}"),
480        }
481    }
482}
483
484impl std::error::Error for ParseError {}
485
486impl Display for CommandDescriptions<'_> {
487    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
488        if let Some(global_description) = self.global_description {
489            f.write_str(global_description)?;
490            f.write_str("\n\n")?;
491        }
492
493        let format_command = |command: &str, prefix: &str, formater: &mut fmt::Formatter<'_>| {
494            formater.write_str(prefix)?;
495            formater.write_str(command)?;
496            if let Some(username) = self.bot_username {
497                formater.write_char('@')?;
498                formater.write_str(username)?;
499            }
500            fmt::Result::Ok(())
501        };
502
503        let mut write = |&CommandDescription { prefix, command, aliases, description }, nls| {
504            if nls {
505                f.write_char('\n')?;
506            }
507
508            format_command(command, prefix, f)?;
509            for alias in aliases {
510                f.write_str(", ")?;
511                format_command(alias, prefix, f)?;
512            }
513
514            if !description.is_empty() {
515                f.write_str(" — ")?;
516                f.write_str(description)?;
517            }
518
519            fmt::Result::Ok(())
520        };
521
522        if let Some(descr) = self.descriptions.first() {
523            write(descr, false)?;
524            for descr in &self.descriptions[1..] {
525                write(descr, true)?;
526            }
527        }
528
529        Ok(())
530    }
531}
532
533// The rest of tests are integration due to problems with macro expansion in
534// unit tests.
535#[cfg(test)]
536mod tests {
537    use super::*;
538
539    #[test]
540    fn parse_command_with_args_() {
541        let data = "/command arg1 arg2";
542        let expected = Some(("command", vec!["arg1", "arg2"]));
543        let actual = parse_command(data, "");
544        assert_eq!(actual, expected)
545    }
546
547    #[test]
548    fn parse_command_with_args_without_args() {
549        let data = "/command";
550        let expected = Some(("command", vec![]));
551        let actual = parse_command(data, "");
552        assert_eq!(actual, expected)
553    }
554}