From b91c79e524b9f58fdde8ce4a1ef4d856005b84b4 Mon Sep 17 00:00:00 2001 From: puh Date: Sat, 4 Feb 2023 15:04:20 +0300 Subject: [PATCH 1/4] fix split parser for tuple struct with len <2 --- crates/teloxide-macros/src/fields_parse.rs | 8 +++--- crates/teloxide/tests/command.rs | 30 +++++++++++++++++++++- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/crates/teloxide-macros/src/fields_parse.rs b/crates/teloxide-macros/src/fields_parse.rs index 5d9d320a..b06e1d47 100644 --- a/crates/teloxide-macros/src/fields_parse.rs +++ b/crates/teloxide-macros/src/fields_parse.rs @@ -125,8 +125,8 @@ fn parser_with_separator<'a>( })?; <#types>::from_str(s).map_err(|e| teloxide::utils::command::ParseError::IncorrectFormat(e.into()))? - } - ),* + }, + )* ) } }; @@ -139,12 +139,12 @@ fn parser_with_separator<'a>( let res = #res; match splitted.next() { - Some(d) => ::std::result::Result::Err(teloxide::utils::command::ParseError::TooManyArguments { + Some(d) if !s.is_empty() => ::std::result::Result::Err(teloxide::utils::command::ParseError::TooManyArguments { expected: #expected, found: #expected + 1, message: format!("Excess argument: {}", d), }), - None => ::std::result::Result::Ok(res) + _ => ::std::result::Result::Ok(res) } } ) diff --git a/crates/teloxide/tests/command.rs b/crates/teloxide/tests/command.rs index cc4cd8a1..245b4c33 100644 --- a/crates/teloxide/tests/command.rs +++ b/crates/teloxide/tests/command.rs @@ -117,7 +117,7 @@ fn parse_with_split() { assert_eq!( DefaultCommands::Start(10, "hello".to_string()), - DefaultCommands::parse("/start 10 hello", "").unwrap() + DefaultCommands::parse("/start 10 hello", "").unwrap(), ); } @@ -138,6 +138,34 @@ fn parse_with_split2() { ); } +#[test] +#[cfg(feature = "macros")] +fn parse_with_split3() { + #[derive(BotCommands, Debug, PartialEq)] + #[command(rename_rule = "lowercase")] + #[command(parse_with = "split")] + enum DefaultCommands { + Start(u8), + Help, + } + + assert_eq!(DefaultCommands::Start(10), DefaultCommands::parse("/start 10", "").unwrap(),); +} + +#[test] +#[cfg(feature = "macros")] +fn parse_with_split4() { + #[derive(BotCommands, Debug, PartialEq)] + #[command(rename_rule = "lowercase")] + #[command(parse_with = "split")] + enum DefaultCommands { + Start(), + Help, + } + + assert_eq!(DefaultCommands::Start(), DefaultCommands::parse("/start", "").unwrap(),); +} + #[test] #[cfg(feature = "macros")] fn parse_custom_parser() { From d961cf79f40509c7f4730f3d94fa18c95808293b Mon Sep 17 00:00:00 2001 From: puh Date: Tue, 7 Feb 2023 10:17:30 +0300 Subject: [PATCH 2/4] Update changelogs --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7b35e27..51150963 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Allow `ChatJoinRequest` updates +- Fix `split` parser for tuple variants with len < 2 ([issue #834](https://github.com/teloxide/teloxide/issues/834)) ### Added From 560812dfccf8162705e8e372b52bf5551287f3ed Mon Sep 17 00:00:00 2001 From: puh Date: Tue, 7 Feb 2023 12:55:20 +0300 Subject: [PATCH 3/4] Fix changelogs and TooManyArguments --- CHANGELOG.md | 1 - crates/teloxide-macros/CHANGELOG.md | 4 ++++ crates/teloxide-macros/src/fields_parse.rs | 5 ++++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51150963..a7b35e27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Allow `ChatJoinRequest` updates -- Fix `split` parser for tuple variants with len < 2 ([issue #834](https://github.com/teloxide/teloxide/issues/834)) ### Added diff --git a/crates/teloxide-macros/CHANGELOG.md b/crates/teloxide-macros/CHANGELOG.md index 19edca63..10b1109e 100644 --- a/crates/teloxide-macros/CHANGELOG.md +++ b/crates/teloxide-macros/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## unreleased +### Fixed + +- Fix `split` parser for tuple variants with len < 2 ([issue #834](https://github.com/teloxide/teloxide/issues/834)) + ## 0.7.1 - 2023-01-17 ### Fixed diff --git a/crates/teloxide-macros/src/fields_parse.rs b/crates/teloxide-macros/src/fields_parse.rs index b06e1d47..896aaaa6 100644 --- a/crates/teloxide-macros/src/fields_parse.rs +++ b/crates/teloxide-macros/src/fields_parse.rs @@ -138,10 +138,13 @@ fn parser_with_separator<'a>( let res = #res; + if !s.is_empty() && splitted.count() { + } + match splitted.next() { Some(d) if !s.is_empty() => ::std::result::Result::Err(teloxide::utils::command::ParseError::TooManyArguments { expected: #expected, - found: #expected + 1, + found: #expected + 1 + splitted.count(), message: format!("Excess argument: {}", d), }), _ => ::std::result::Result::Ok(res) From c29aac96a7056924daa88bd3b138bc326eb59b6b Mon Sep 17 00:00:00 2001 From: puh Date: Wed, 15 Feb 2023 03:35:03 +0300 Subject: [PATCH 4/4] cleanup --- crates/teloxide-macros/src/fields_parse.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/crates/teloxide-macros/src/fields_parse.rs b/crates/teloxide-macros/src/fields_parse.rs index 896aaaa6..d93de04f 100644 --- a/crates/teloxide-macros/src/fields_parse.rs +++ b/crates/teloxide-macros/src/fields_parse.rs @@ -138,9 +138,6 @@ fn parser_with_separator<'a>( let res = #res; - if !s.is_empty() && splitted.count() { - } - match splitted.next() { Some(d) if !s.is_empty() => ::std::result::Result::Err(teloxide::utils::command::ParseError::TooManyArguments { expected: #expected,