mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-24 23:57:38 +01:00
Add #[command(rename = "blah")]
This commit is contained in:
parent
ff08854ca9
commit
f3bb54d670
4 changed files with 35 additions and 16 deletions
|
@ -1,6 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
command_attr::CommandAttrs, command_enum::CommandEnum,
|
command_attr::CommandAttrs, command_enum::CommandEnum,
|
||||||
fields_parse::ParserType, Result,
|
error::compile_error_at, fields_parse::ParserType, Result,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) struct Command {
|
pub(crate) struct Command {
|
||||||
|
@ -25,15 +25,23 @@ impl Command {
|
||||||
prefix,
|
prefix,
|
||||||
description,
|
description,
|
||||||
rename_rule,
|
rename_rule,
|
||||||
|
rename,
|
||||||
parser,
|
parser,
|
||||||
// FIXME: error on/do not ignore separator
|
// FIXME: error on/do not ignore separator
|
||||||
separator: _,
|
separator: _,
|
||||||
} = attrs;
|
} = attrs;
|
||||||
|
|
||||||
let name = rename_rule
|
let name = match (rename, rename_rule) {
|
||||||
.map(|(rr, _)| rr)
|
(Some((rename, _)), None) => rename,
|
||||||
.unwrap_or(global_options.rename_rule)
|
(Some(_), Some((_, sp))) => {
|
||||||
.apply(name);
|
return Err(compile_error_at(
|
||||||
|
"`rename_rule` can't be applied to `rename`-d variant",
|
||||||
|
sp,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
(None, Some((rule, _))) => rule.apply(name),
|
||||||
|
(None, None) => global_options.rename_rule.apply(name),
|
||||||
|
};
|
||||||
|
|
||||||
let prefix = prefix
|
let prefix = prefix
|
||||||
.map(|(p, _)| p)
|
.map(|(p, _)| p)
|
||||||
|
|
|
@ -14,6 +14,7 @@ pub(crate) struct CommandAttrs {
|
||||||
pub prefix: Option<(String, Span)>,
|
pub prefix: Option<(String, Span)>,
|
||||||
pub description: Option<(String, Span)>,
|
pub description: Option<(String, Span)>,
|
||||||
pub rename_rule: Option<(RenameRule, Span)>,
|
pub rename_rule: Option<(RenameRule, Span)>,
|
||||||
|
pub rename: Option<(String, Span)>,
|
||||||
pub parser: Option<(ParserType, Span)>,
|
pub parser: Option<(ParserType, Span)>,
|
||||||
pub separator: Option<(String, Span)>,
|
pub separator: Option<(String, Span)>,
|
||||||
}
|
}
|
||||||
|
@ -37,6 +38,7 @@ enum CommandAttrKind {
|
||||||
Prefix(String),
|
Prefix(String),
|
||||||
Description(String),
|
Description(String),
|
||||||
RenameRule(RenameRule),
|
RenameRule(RenameRule),
|
||||||
|
Rename(String),
|
||||||
ParseWith(ParserType),
|
ParseWith(ParserType),
|
||||||
Separator(String),
|
Separator(String),
|
||||||
}
|
}
|
||||||
|
@ -53,6 +55,7 @@ impl CommandAttrs {
|
||||||
prefix: None,
|
prefix: None,
|
||||||
description: None,
|
description: None,
|
||||||
rename_rule: None,
|
rename_rule: None,
|
||||||
|
rename: None,
|
||||||
parser: None,
|
parser: None,
|
||||||
separator: None,
|
separator: None,
|
||||||
},
|
},
|
||||||
|
@ -77,6 +80,7 @@ impl CommandAttrs {
|
||||||
Prefix(p) => insert(&mut this.prefix, p, attr.sp),
|
Prefix(p) => insert(&mut this.prefix, p, attr.sp),
|
||||||
Description(d) => insert(&mut this.description, d, attr.sp),
|
Description(d) => insert(&mut this.description, d, attr.sp),
|
||||||
RenameRule(r) => insert(&mut this.rename_rule, r, attr.sp),
|
RenameRule(r) => insert(&mut this.rename_rule, r, attr.sp),
|
||||||
|
Rename(r) => insert(&mut this.rename, r, attr.sp),
|
||||||
ParseWith(p) => insert(&mut this.parser, p, attr.sp),
|
ParseWith(p) => insert(&mut this.parser, p, attr.sp),
|
||||||
Separator(s) => insert(&mut this.separator, s, attr.sp),
|
Separator(s) => insert(&mut this.separator, s, attr.sp),
|
||||||
}?;
|
}?;
|
||||||
|
@ -101,6 +105,7 @@ impl CommandAttr {
|
||||||
.expect_string()
|
.expect_string()
|
||||||
.and_then(|r| self::RenameRule::parse(&r))?,
|
.and_then(|r| self::RenameRule::parse(&r))?,
|
||||||
),
|
),
|
||||||
|
"rename" => Rename(value.expect_string()?),
|
||||||
"parse_with" => {
|
"parse_with" => {
|
||||||
ParseWith(value.expect_string().map(|p| ParserType::parse(&p))?)
|
ParseWith(value.expect_string().map(|p| ParserType::parse(&p))?)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
command_attr::CommandAttrs, fields_parse::ParserType,
|
command_attr::CommandAttrs, error::compile_error_at,
|
||||||
rename_rules::RenameRule, Result,
|
fields_parse::ParserType, rename_rules::RenameRule, Result,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -18,10 +18,18 @@ impl CommandEnum {
|
||||||
prefix,
|
prefix,
|
||||||
description,
|
description,
|
||||||
rename_rule,
|
rename_rule,
|
||||||
|
rename,
|
||||||
parser,
|
parser,
|
||||||
separator,
|
separator,
|
||||||
} = attrs;
|
} = attrs;
|
||||||
|
|
||||||
|
if let Some((_rename, sp)) = rename {
|
||||||
|
return Err(compile_error_at(
|
||||||
|
"`rename` attribute can only be applied to enums *variants*",
|
||||||
|
sp,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
let mut parser = parser.map(|(p, _)| p).unwrap_or(ParserType::Default);
|
let mut parser = parser.map(|(p, _)| p).unwrap_or(ParserType::Default);
|
||||||
|
|
||||||
// FIXME: Error on unused separator
|
// FIXME: Error on unused separator
|
||||||
|
|
|
@ -249,8 +249,8 @@ fn rename_rules() {
|
||||||
GggGgg,
|
GggGgg,
|
||||||
#[command(rename_rule = "SCREAMING-KEBAB-CASE")]
|
#[command(rename_rule = "SCREAMING-KEBAB-CASE")]
|
||||||
HhhHhh,
|
HhhHhh,
|
||||||
//#[command(rename = "Bar")]
|
#[command(rename = "Bar")]
|
||||||
//Foo,
|
Foo,
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -285,16 +285,14 @@ fn rename_rules() {
|
||||||
DefaultCommands::HhhHhh,
|
DefaultCommands::HhhHhh,
|
||||||
DefaultCommands::parse("/HHH-HHH", "").unwrap()
|
DefaultCommands::parse("/HHH-HHH", "").unwrap()
|
||||||
);
|
);
|
||||||
//assert_eq!(DefaultCommands::Foo, DefaultCommands::parse("/Bar",
|
assert_eq!(
|
||||||
// "").unwrap());
|
DefaultCommands::Foo,
|
||||||
|
DefaultCommands::parse("/Bar", "").unwrap()
|
||||||
|
);
|
||||||
|
|
||||||
// assert_eq!(
|
|
||||||
// "/aaaaaa\n/BBBBBB\n/CccCcc\n/dddDdd\n/eee_eee\n/FFF_FFF\n/ggg-ggg\n/
|
|
||||||
// HHH-HHH\n/Bar", DefaultCommands::descriptions().to_string()
|
|
||||||
// );
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"/aaaaaa\n/BBBBBB\n/CccCcc\n/dddDdd\n/eee_eee\n/FFF_FFF\n/ggg-ggg\n/\
|
"/aaaaaa\n/BBBBBB\n/CccCcc\n/dddDdd\n/eee_eee\n/FFF_FFF\n/ggg-ggg\n/\
|
||||||
HHH-HHH",
|
HHH-HHH\n/Bar",
|
||||||
DefaultCommands::descriptions().to_string()
|
DefaultCommands::descriptions().to_string()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue