diff --git a/src/command.rs b/src/command.rs index 1aa8ca21..72b05f3e 100644 --- a/src/command.rs +++ b/src/command.rs @@ -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) diff --git a/src/command_attr.rs b/src/command_attr.rs index c8937891..3aeb8e63 100644 --- a/src/command_attr.rs +++ b/src/command_attr.rs @@ -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))?) } diff --git a/src/command_enum.rs b/src/command_enum.rs index 6720c88f..2a882e17 100644 --- a/src/command_enum.rs +++ b/src/command_enum.rs @@ -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 diff --git a/tests/command.rs b/tests/command.rs index 6300c063..f7297c8d 100644 --- a/tests/command.rs +++ b/tests/command.rs @@ -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() ); }