fix split parser for tuple struct with len <2

This commit is contained in:
puh 2023-02-04 15:04:20 +03:00
parent 6c6ae76dec
commit b91c79e524
No known key found for this signature in database
GPG key ID: 171E3E1356CEE151
2 changed files with 33 additions and 5 deletions

View file

@ -125,8 +125,8 @@ fn parser_with_separator<'a>(
})?; })?;
<#types>::from_str(s).map_err(|e| teloxide::utils::command::ParseError::IncorrectFormat(e.into()))? <#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; let res = #res;
match splitted.next() { 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, expected: #expected,
found: #expected + 1, found: #expected + 1,
message: format!("Excess argument: {}", d), message: format!("Excess argument: {}", d),
}), }),
None => ::std::result::Result::Ok(res) _ => ::std::result::Result::Ok(res)
} }
} }
) )

View file

@ -117,7 +117,7 @@ fn parse_with_split() {
assert_eq!( assert_eq!(
DefaultCommands::Start(10, "hello".to_string()), 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] #[test]
#[cfg(feature = "macros")] #[cfg(feature = "macros")]
fn parse_custom_parser() { fn parse_custom_parser() {