From 013cd8e01d9e248f1ac556ca052bfd30576c2f42 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 3 Oct 2022 16:28:45 +0400 Subject: [PATCH] use real `teloxide` for tests --- Cargo.toml | 6 +- micro-teloxide/Cargo.toml | 11 -- micro-teloxide/src/lib.rs | 212 -------------------------------------- tests/command.rs | 6 +- 4 files changed, 7 insertions(+), 228 deletions(-) delete mode 100644 micro-teloxide/Cargo.toml delete mode 100644 micro-teloxide/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 4a4b43e7..6a4b28b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,5 @@ syn = { version = "1.0.13", features = ["full"] } heck = "0.4.0" [dev-dependencies] -teloxide = { path = "micro-teloxide", package = "micro-teloxide" } - -[workspace] -members = [".", "micro-teloxide"] +# XXX: Do not enable `macros` feature +teloxide = { git = "https://github.com/teloxide/teloxide.git", rev = "4246455" } diff --git a/micro-teloxide/Cargo.toml b/micro-teloxide/Cargo.toml deleted file mode 100644 index 653d32d0..00000000 --- a/micro-teloxide/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "micro-teloxide" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -teloxide-macros = { path = ".." } -serde = { version = "1.0.145", features = ["derive"] } -serde_with_macros = "1.5.2" diff --git a/micro-teloxide/src/lib.rs b/micro-teloxide/src/lib.rs deleted file mode 100644 index 1131b329..00000000 --- a/micro-teloxide/src/lib.rs +++ /dev/null @@ -1,212 +0,0 @@ -//! A minimal "mock" of teloxide crate that is needed to test macros. Changes to -//! `teloxide`[`-core`] should be kept in sync with this... somehow -//! -//! This is a price for placing all crates in separate repositories. - -pub use teloxide_macros as macros; - -pub mod utils { - pub mod command { - use std::{ - error::Error, - fmt::{self, Display, Write}, - marker::PhantomData, - }; - - pub use teloxide_macros::BotCommands; - - use crate::types::{BotCommand, Me}; - pub trait BotCommands: Sized { - fn parse(s: &str, bot_username: N) -> Result - where - N: Into; - - fn descriptions() -> CommandDescriptions<'static>; - - fn bot_commands() -> Vec; - - fn ty() -> PhantomData { - PhantomData - } - } - - pub type PrefixedBotCommand = String; - pub type BotName = String; - - #[derive(Debug)] - pub enum ParseError { - TooFewArguments { expected: usize, found: usize, message: String }, - TooManyArguments { expected: usize, found: usize, message: String }, - - IncorrectFormat(Box), - - UnknownCommand(PrefixedBotCommand), - WrongBotName(BotName), - - Custom(Box), - } - - #[derive(Debug, Clone)] - #[allow(dead_code)] - pub struct CommandDescriptions<'a> { - global_description: Option<&'a str>, - descriptions: &'a [CommandDescription<'a>], - bot_username: Option<&'a str>, - } - - #[derive(Debug, Clone)] - pub struct CommandDescription<'a> { - pub prefix: &'a str, - pub command: &'a str, - pub description: &'a str, - } - - impl<'a> CommandDescriptions<'a> { - pub fn new(descriptions: &'a [CommandDescription<'a>]) -> Self { - Self { - global_description: None, - descriptions, - bot_username: None, - } - } - - pub fn global_description( - self, - global_description: &'a str, - ) -> Self { - Self { global_description: Some(global_description), ..self } - } - - pub fn username(self, bot_username: &'a str) -> Self { - Self { bot_username: Some(bot_username), ..self } - } - - pub fn username_from_me( - self, - me: &'a Me, - ) -> CommandDescriptions<'a> { - self.username( - me.user - .username - .as_deref() - .expect("Bots must have usernames"), - ) - } - } - - impl Display for CommandDescriptions<'_> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if let Some(global_description) = self.global_description { - f.write_str(global_description)?; - f.write_str("\n\n")?; - } - - let mut write = - |&CommandDescription { prefix, command, description }, - nls| { - if nls { - f.write_char('\n')?; - } - - f.write_str(prefix)?; - f.write_str(command)?; - - if let Some(username) = self.bot_username { - f.write_char('@')?; - f.write_str(username)?; - } - - if !description.is_empty() { - f.write_str(" — ")?; - f.write_str(description)?; - } - - fmt::Result::Ok(()) - }; - - if let Some(descr) = self.descriptions.first() { - write(descr, false)?; - for descr in &self.descriptions[1..] { - write(descr, true)?; - } - } - - Ok(()) - } - } - } -} - -pub mod types { - use serde::{Deserialize, Serialize}; - - #[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, - } - #[serde_with_macros::skip_serializing_none] - #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] - pub struct User { - pub id: UserId, - pub is_bot: bool, - pub first_name: String, - pub last_name: Option, - pub username: Option, - pub language_code: Option, - #[serde(default, skip_serializing_if = "std::ops::Not::not")] - pub is_premium: bool, - #[serde(default, skip_serializing_if = "std::ops::Not::not")] - pub added_to_attachment_menu: bool, - } - - #[derive( - Clone, - Copy, - Debug, - PartialEq, - Eq, - PartialOrd, - Ord, - Hash, - Serialize, - Deserialize, - )] - #[serde(transparent)] - pub struct UserId(pub u64); - - #[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] - pub struct BotCommand { - pub command: String, - pub description: String, - } - - impl BotCommand { - pub fn new(command: S1, description: S2) -> Self - where - S1: Into, - S2: Into, - { - Self { command: command.into(), description: description.into() } - } - - pub fn command(mut self, val: S) -> Self - where - S: Into, - { - self.command = val.into(); - self - } - - pub fn description(mut self, val: S) -> Self - where - S: Into, - { - self.description = val.into(); - self - } - } -} diff --git a/tests/command.rs b/tests/command.rs index 248ef9e3..f8fe32a7 100644 --- a/tests/command.rs +++ b/tests/command.rs @@ -1,6 +1,10 @@ //! Test for `teloxide-macros` -use teloxide::utils::command::BotCommands; +use teloxide_macros::BotCommands; + +// Import only trait _methods_, such that we can call `parse`, but we also test +// that proc macros works without the trait being imported. +use teloxide::utils::command::BotCommands as _; #[test] fn parse_command_with_args() {