mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-03 17:52:12 +01:00
Conditionally add teloxide-macros crate and reexport macros
This commit adds the feature "macros" which conditionally adds the teloxide-macros crate and reexports its macros. All (doc) tests using these macros are also conditionally enabled now. Also change examples accordingly. This commit solves https://github.com/teloxide/teloxide/issues/283
This commit is contained in:
parent
0c6a81e1b6
commit
eed1bbb0fc
7 changed files with 45 additions and 5 deletions
|
@ -30,6 +30,8 @@ bincode-serializer = ["bincode"]
|
||||||
|
|
||||||
frunk- = ["frunk"]
|
frunk- = ["frunk"]
|
||||||
|
|
||||||
|
macros = ["teloxide-macros"]
|
||||||
|
|
||||||
nightly = [] # currently used only for `README.md` tests
|
nightly = [] # currently used only for `README.md` tests
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
@ -57,7 +59,7 @@ serde_cbor = { version = "0.11.1", optional = true }
|
||||||
bincode = { version = "1.3.1", optional = true }
|
bincode = { version = "1.3.1", optional = true }
|
||||||
frunk = { version = "0.3.1", optional = true }
|
frunk = { version = "0.3.1", optional = true }
|
||||||
|
|
||||||
teloxide-macros = { git = "https://github.com/teloxide/teloxide-macros", branch = "master" }
|
teloxide-macros = { git = "https://github.com/teloxide/teloxide-macros", branch = "master", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
smart-default = "0.6.0"
|
smart-default = "0.6.0"
|
||||||
|
|
|
@ -10,7 +10,7 @@ edition = "2018"
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
pretty_env_logger = "0.4.0"
|
pretty_env_logger = "0.4.0"
|
||||||
tokio = { version = "0.2.11", features = ["rt-threaded", "macros"] }
|
tokio = { version = "0.2.11", features = ["rt-threaded", "macros"] }
|
||||||
teloxide = { path = "../../" }
|
teloxide = { path = "../../", features = ["macros"] }
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = true
|
lto = true
|
|
@ -10,4 +10,4 @@ edition = "2018"
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
pretty_env_logger = "0.4.0"
|
pretty_env_logger = "0.4.0"
|
||||||
tokio = { version = "0.2.11", features = ["rt-threaded", "macros"] }
|
tokio = { version = "0.2.11", features = ["rt-threaded", "macros"] }
|
||||||
teloxide = { path = "../../" }
|
teloxide = { path = "../../", features = ["macros"] }
|
||||||
|
|
|
@ -30,10 +30,10 @@
|
||||||
//! skeleton should look like:
|
//! skeleton should look like:
|
||||||
//!
|
//!
|
||||||
//! ```no_run
|
//! ```no_run
|
||||||
|
//! # #[cfg(feature = "macros")] {
|
||||||
//! use std::convert::Infallible;
|
//! use std::convert::Infallible;
|
||||||
//!
|
//!
|
||||||
//! use teloxide::prelude::*;
|
//! use teloxide::{dispatching::dialogue::Transition, prelude::*, teloxide};
|
||||||
//! use teloxide_macros::{teloxide, Transition};
|
|
||||||
//!
|
//!
|
||||||
//! struct _1State;
|
//! struct _1State;
|
||||||
//! struct _2State;
|
//! struct _2State;
|
||||||
|
@ -97,6 +97,7 @@
|
||||||
//! .dispatch()
|
//! .dispatch()
|
||||||
//! .await;
|
//! .await;
|
||||||
//! }
|
//! }
|
||||||
|
//! # }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! - `#[teloxide(subtransition)]` implements [`Subtransition`] for the first
|
//! - `#[teloxide(subtransition)]` implements [`Subtransition`] for the first
|
||||||
|
@ -156,6 +157,10 @@ pub use transition::{
|
||||||
Subtransition, SubtransitionOutputType, Transition, TransitionIn, TransitionOut,
|
Subtransition, SubtransitionOutputType, Transition, TransitionIn, TransitionOut,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[cfg(feature = "macros")]
|
||||||
|
#[cfg_attr(all(docsrs, feature = "nightly"), doc(cfg(feature = "macros")))]
|
||||||
|
pub use teloxide_macros::Transition;
|
||||||
|
|
||||||
#[cfg(feature = "redis-storage")]
|
#[cfg(feature = "redis-storage")]
|
||||||
pub use storage::{RedisStorage, RedisStorageError};
|
pub use storage::{RedisStorage, RedisStorageError};
|
||||||
|
|
||||||
|
|
|
@ -41,6 +41,7 @@
|
||||||
#![allow(clippy::match_bool)]
|
#![allow(clippy::match_bool)]
|
||||||
#![forbid(unsafe_code)]
|
#![forbid(unsafe_code)]
|
||||||
#![cfg_attr(all(feature = "nightly", doctest), feature(external_doc))]
|
#![cfg_attr(all(feature = "nightly", doctest), feature(external_doc))]
|
||||||
|
#![cfg_attr(all(docsrs, feature = "nightly"), feature(doc_cfg))]
|
||||||
|
|
||||||
pub use bot::{Bot, BotBuilder};
|
pub use bot::{Bot, BotBuilder};
|
||||||
pub use dispatching::repls::{
|
pub use dispatching::repls::{
|
||||||
|
@ -61,8 +62,13 @@ pub mod requests;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
|
||||||
|
#[cfg(feature = "macros")]
|
||||||
extern crate teloxide_macros;
|
extern crate teloxide_macros;
|
||||||
|
|
||||||
|
#[cfg(feature = "macros")]
|
||||||
|
#[cfg_attr(all(docsrs, feature = "nightly"), doc(cfg(feature = "macros")))]
|
||||||
|
pub use teloxide_macros::teloxide;
|
||||||
|
|
||||||
#[cfg(all(feature = "nightly", doctest))]
|
#[cfg(all(feature = "nightly", doctest))]
|
||||||
#[doc(include = "../README.md")]
|
#[doc(include = "../README.md")]
|
||||||
enum ReadmeDocTests {}
|
enum ReadmeDocTests {}
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
//!
|
//!
|
||||||
//! # Using BotCommand
|
//! # Using BotCommand
|
||||||
//! ```
|
//! ```
|
||||||
|
//! # #[cfg(feature = "macros")] {
|
||||||
//! use teloxide::utils::command::BotCommand;
|
//! use teloxide::utils::command::BotCommand;
|
||||||
//!
|
//!
|
||||||
//! type UnitOfTime = u8;
|
//! type UnitOfTime = u8;
|
||||||
|
@ -19,6 +20,7 @@
|
||||||
//!
|
//!
|
||||||
//! let command = AdminCommand::parse("/ban 5 h", "bot_name").unwrap();
|
//! let command = AdminCommand::parse("/ban 5 h", "bot_name").unwrap();
|
||||||
//! assert_eq!(command, AdminCommand::Ban(5, 'h'));
|
//! assert_eq!(command, AdminCommand::Ban(5, 'h'));
|
||||||
|
//! # }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! # Using parse_command
|
//! # Using parse_command
|
||||||
|
@ -46,12 +48,16 @@
|
||||||
|
|
||||||
use serde::export::Formatter;
|
use serde::export::Formatter;
|
||||||
use std::{error::Error, fmt::Display};
|
use std::{error::Error, fmt::Display};
|
||||||
|
|
||||||
|
#[cfg(feature = "macros")]
|
||||||
|
#[cfg_attr(all(docsrs, feature = "nightly"), doc(cfg(feature = "macros")))]
|
||||||
pub use teloxide_macros::BotCommand;
|
pub use teloxide_macros::BotCommand;
|
||||||
|
|
||||||
/// An enumeration of bot's commands.
|
/// An enumeration of bot's commands.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
/// ```
|
/// ```
|
||||||
|
/// # #[cfg(feature = "macros")] {
|
||||||
/// use teloxide::utils::command::BotCommand;
|
/// use teloxide::utils::command::BotCommand;
|
||||||
///
|
///
|
||||||
/// type UnitOfTime = u8;
|
/// type UnitOfTime = u8;
|
||||||
|
@ -65,6 +71,7 @@ pub use teloxide_macros::BotCommand;
|
||||||
///
|
///
|
||||||
/// let command = AdminCommand::parse("/ban 5 h", "bot_name").unwrap();
|
/// let command = AdminCommand::parse("/ban 5 h", "bot_name").unwrap();
|
||||||
/// assert_eq!(command, AdminCommand::Ban(5, 'h'));
|
/// assert_eq!(command, AdminCommand::Ban(5, 'h'));
|
||||||
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// ## Enum attributes
|
/// ## Enum attributes
|
||||||
|
@ -86,6 +93,7 @@ pub use teloxide_macros::BotCommand;
|
||||||
///
|
///
|
||||||
/// ### Example
|
/// ### Example
|
||||||
/// ```
|
/// ```
|
||||||
|
/// # #[cfg(feature = "macros")] {
|
||||||
/// use teloxide::utils::command::BotCommand;
|
/// use teloxide::utils::command::BotCommand;
|
||||||
///
|
///
|
||||||
/// #[derive(BotCommand, PartialEq, Debug)]
|
/// #[derive(BotCommand, PartialEq, Debug)]
|
||||||
|
@ -96,6 +104,7 @@ pub use teloxide_macros::BotCommand;
|
||||||
///
|
///
|
||||||
/// let command = Command::parse("/text hello my dear friend!", "").unwrap();
|
/// let command = Command::parse("/text hello my dear friend!", "").unwrap();
|
||||||
/// assert_eq!(command, Command::Text("hello my dear friend!".to_string()));
|
/// assert_eq!(command, Command::Text("hello my dear friend!".to_string()));
|
||||||
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// - `split` - separates a messsage by a given separator (the default is the
|
/// - `split` - separates a messsage by a given separator (the default is the
|
||||||
|
@ -104,6 +113,7 @@ pub use teloxide_macros::BotCommand;
|
||||||
///
|
///
|
||||||
/// ### Example
|
/// ### Example
|
||||||
/// ```
|
/// ```
|
||||||
|
/// # #[cfg(feature = "macros")] {
|
||||||
/// use teloxide::utils::command::BotCommand;
|
/// use teloxide::utils::command::BotCommand;
|
||||||
///
|
///
|
||||||
/// #[derive(BotCommand, PartialEq, Debug)]
|
/// #[derive(BotCommand, PartialEq, Debug)]
|
||||||
|
@ -114,6 +124,7 @@ pub use teloxide_macros::BotCommand;
|
||||||
///
|
///
|
||||||
/// let command = Command::parse("/nums 1 32 -5", "").unwrap();
|
/// let command = Command::parse("/nums 1 32 -5", "").unwrap();
|
||||||
/// assert_eq!(command, Command::Nums(1, 32, -5));
|
/// assert_eq!(command, Command::Nums(1, 32, -5));
|
||||||
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// 5. `#[command(separator = "sep")]`
|
/// 5. `#[command(separator = "sep")]`
|
||||||
|
@ -122,6 +133,7 @@ pub use teloxide_macros::BotCommand;
|
||||||
///
|
///
|
||||||
/// ### Example
|
/// ### Example
|
||||||
/// ```
|
/// ```
|
||||||
|
/// # #[cfg(feature = "macros")] {
|
||||||
/// use teloxide::utils::command::BotCommand;
|
/// use teloxide::utils::command::BotCommand;
|
||||||
///
|
///
|
||||||
/// #[derive(BotCommand, PartialEq, Debug)]
|
/// #[derive(BotCommand, PartialEq, Debug)]
|
||||||
|
@ -132,6 +144,7 @@ pub use teloxide_macros::BotCommand;
|
||||||
///
|
///
|
||||||
/// let command = Command::parse("/nums 1|32|5", "").unwrap();
|
/// let command = Command::parse("/nums 1|32|5", "").unwrap();
|
||||||
/// assert_eq!(command, Command::Nums(1, 32, 5));
|
/// assert_eq!(command, Command::Nums(1, 32, 5));
|
||||||
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// ## Variant attributes
|
/// ## Variant attributes
|
||||||
|
@ -149,6 +162,7 @@ pub use teloxide_macros::BotCommand;
|
||||||
///
|
///
|
||||||
/// ### Example
|
/// ### Example
|
||||||
/// ```
|
/// ```
|
||||||
|
/// # #[cfg(feature = "macros")] {
|
||||||
/// use teloxide::utils::command::{BotCommand, ParseError};
|
/// use teloxide::utils::command::{BotCommand, ParseError};
|
||||||
///
|
///
|
||||||
/// fn accept_two_digits(input: String) -> Result<(u8,), ParseError> {
|
/// fn accept_two_digits(input: String) -> Result<(u8,), ParseError> {
|
||||||
|
@ -172,6 +186,7 @@ pub use teloxide_macros::BotCommand;
|
||||||
/// assert_eq!(command, Command::Num(12));
|
/// assert_eq!(command, Command::Num(12));
|
||||||
/// let command = Command::parse("/num 333", "");
|
/// let command = Command::parse("/num 333", "");
|
||||||
/// assert!(command.is_err());
|
/// assert!(command.is_err());
|
||||||
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// 3. `#[command(prefix = "prefix")]`
|
/// 3. `#[command(prefix = "prefix")]`
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
|
#[cfg(feature = "macros")]
|
||||||
use teloxide::utils::command::{BotCommand, ParseError};
|
use teloxide::utils::command::{BotCommand, ParseError};
|
||||||
|
|
||||||
// We put tests here because macro expand in unit tests in module
|
// We put tests here because macro expand in unit tests in module
|
||||||
// teloxide::utils::command was a failure
|
// teloxide::utils::command was a failure
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "macros")]
|
||||||
fn parse_command_with_args() {
|
fn parse_command_with_args() {
|
||||||
#[command(rename = "lowercase")]
|
#[command(rename = "lowercase")]
|
||||||
#[derive(BotCommand, Debug, PartialEq)]
|
#[derive(BotCommand, Debug, PartialEq)]
|
||||||
|
@ -19,6 +21,7 @@ fn parse_command_with_args() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "macros")]
|
||||||
fn parse_command_with_non_string_arg() {
|
fn parse_command_with_non_string_arg() {
|
||||||
#[command(rename = "lowercase")]
|
#[command(rename = "lowercase")]
|
||||||
#[derive(BotCommand, Debug, PartialEq)]
|
#[derive(BotCommand, Debug, PartialEq)]
|
||||||
|
@ -34,6 +37,7 @@ fn parse_command_with_non_string_arg() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "macros")]
|
||||||
fn attribute_prefix() {
|
fn attribute_prefix() {
|
||||||
#[command(rename = "lowercase")]
|
#[command(rename = "lowercase")]
|
||||||
#[derive(BotCommand, Debug, PartialEq)]
|
#[derive(BotCommand, Debug, PartialEq)]
|
||||||
|
@ -50,6 +54,7 @@ fn attribute_prefix() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "macros")]
|
||||||
fn many_attributes() {
|
fn many_attributes() {
|
||||||
#[command(rename = "lowercase")]
|
#[command(rename = "lowercase")]
|
||||||
#[derive(BotCommand, Debug, PartialEq)]
|
#[derive(BotCommand, Debug, PartialEq)]
|
||||||
|
@ -64,6 +69,7 @@ fn many_attributes() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "macros")]
|
||||||
fn global_attributes() {
|
fn global_attributes() {
|
||||||
#[command(prefix = "!", rename = "lowercase", description = "Bot commands")]
|
#[command(prefix = "!", rename = "lowercase", description = "Bot commands")]
|
||||||
#[derive(BotCommand, Debug, PartialEq)]
|
#[derive(BotCommand, Debug, PartialEq)]
|
||||||
|
@ -79,6 +85,7 @@ fn global_attributes() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "macros")]
|
||||||
fn parse_command_with_bot_name() {
|
fn parse_command_with_bot_name() {
|
||||||
#[command(rename = "lowercase")]
|
#[command(rename = "lowercase")]
|
||||||
#[derive(BotCommand, Debug, PartialEq)]
|
#[derive(BotCommand, Debug, PartialEq)]
|
||||||
|
@ -95,6 +102,7 @@ fn parse_command_with_bot_name() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "macros")]
|
||||||
fn parse_with_split() {
|
fn parse_with_split() {
|
||||||
#[command(rename = "lowercase")]
|
#[command(rename = "lowercase")]
|
||||||
#[command(parse_with = "split")]
|
#[command(parse_with = "split")]
|
||||||
|
@ -111,6 +119,7 @@ fn parse_with_split() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "macros")]
|
||||||
fn parse_with_split2() {
|
fn parse_with_split2() {
|
||||||
#[command(rename = "lowercase")]
|
#[command(rename = "lowercase")]
|
||||||
#[command(parse_with = "split", separator = "|")]
|
#[command(parse_with = "split", separator = "|")]
|
||||||
|
@ -127,6 +136,7 @@ fn parse_with_split2() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "macros")]
|
||||||
fn parse_custom_parser() {
|
fn parse_custom_parser() {
|
||||||
fn custom_parse_function(s: String) -> Result<(u8, String), ParseError> {
|
fn custom_parse_function(s: String) -> Result<(u8, String), ParseError> {
|
||||||
let vec = s.split_whitespace().collect::<Vec<_>>();
|
let vec = s.split_whitespace().collect::<Vec<_>>();
|
||||||
|
@ -154,6 +164,7 @@ fn parse_custom_parser() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "macros")]
|
||||||
fn parse_named_fields() {
|
fn parse_named_fields() {
|
||||||
#[command(rename = "lowercase")]
|
#[command(rename = "lowercase")]
|
||||||
#[command(parse_with = "split")]
|
#[command(parse_with = "split")]
|
||||||
|
@ -170,6 +181,7 @@ fn parse_named_fields() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "macros")]
|
||||||
fn descriptions_off() {
|
fn descriptions_off() {
|
||||||
#[command(rename = "lowercase")]
|
#[command(rename = "lowercase")]
|
||||||
#[derive(BotCommand, Debug, PartialEq)]
|
#[derive(BotCommand, Debug, PartialEq)]
|
||||||
|
|
Loading…
Reference in a new issue