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 5d9d320a..d93de04f 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, + found: #expected + 1 + splitted.count(), 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() {