Add #[command(rename = "blah")]

This commit is contained in:
Maybe Waffle 2022-10-02 18:13:54 +04:00
parent ff08854ca9
commit f3bb54d670
4 changed files with 35 additions and 16 deletions

View file

@ -1,6 +1,6 @@
use crate::{
command_attr::CommandAttrs, command_enum::CommandEnum,
fields_parse::ParserType, Result,
error::compile_error_at, fields_parse::ParserType, Result,
};
pub(crate) struct Command {
@ -25,15 +25,23 @@ impl Command {
prefix,
description,
rename_rule,
rename,
parser,
// FIXME: error on/do not ignore separator
separator: _,
} = attrs;
let name = rename_rule
.map(|(rr, _)| rr)
.unwrap_or(global_options.rename_rule)
.apply(name);
let name = match (rename, rename_rule) {
(Some((rename, _)), None) => rename,
(Some(_), Some((_, sp))) => {
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
.map(|(p, _)| p)

View file

@ -14,6 +14,7 @@ pub(crate) struct CommandAttrs {
pub prefix: Option<(String, Span)>,
pub description: Option<(String, Span)>,
pub rename_rule: Option<(RenameRule, Span)>,
pub rename: Option<(String, Span)>,
pub parser: Option<(ParserType, Span)>,
pub separator: Option<(String, Span)>,
}
@ -37,6 +38,7 @@ enum CommandAttrKind {
Prefix(String),
Description(String),
RenameRule(RenameRule),
Rename(String),
ParseWith(ParserType),
Separator(String),
}
@ -53,6 +55,7 @@ impl CommandAttrs {
prefix: None,
description: None,
rename_rule: None,
rename: None,
parser: None,
separator: None,
},
@ -77,6 +80,7 @@ impl CommandAttrs {
Prefix(p) => insert(&mut this.prefix, p, attr.sp),
Description(d) => insert(&mut this.description, d, 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),
Separator(s) => insert(&mut this.separator, s, attr.sp),
}?;
@ -101,6 +105,7 @@ impl CommandAttr {
.expect_string()
.and_then(|r| self::RenameRule::parse(&r))?,
),
"rename" => Rename(value.expect_string()?),
"parse_with" => {
ParseWith(value.expect_string().map(|p| ParserType::parse(&p))?)
}

View file

@ -1,6 +1,6 @@
use crate::{
command_attr::CommandAttrs, fields_parse::ParserType,
rename_rules::RenameRule, Result,
command_attr::CommandAttrs, error::compile_error_at,
fields_parse::ParserType, rename_rules::RenameRule, Result,
};
#[derive(Debug)]
@ -18,10 +18,18 @@ impl CommandEnum {
prefix,
description,
rename_rule,
rename,
parser,
separator,
} = 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);
// FIXME: Error on unused separator

View file

@ -249,8 +249,8 @@ fn rename_rules() {
GggGgg,
#[command(rename_rule = "SCREAMING-KEBAB-CASE")]
HhhHhh,
//#[command(rename = "Bar")]
//Foo,
#[command(rename = "Bar")]
Foo,
}
assert_eq!(
@ -285,16 +285,14 @@ fn rename_rules() {
DefaultCommands::HhhHhh,
DefaultCommands::parse("/HHH-HHH", "").unwrap()
);
//assert_eq!(DefaultCommands::Foo, DefaultCommands::parse("/Bar",
// "").unwrap());
assert_eq!(
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!(
"/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()
);
}