Skip to content

Commit

Permalink
Add support for lower/upper in rename_all (#412)
Browse files Browse the repository at this point in the history
Add support for lower/upper in rename_all
  • Loading branch information
Morganamilo committed Jul 30, 2020
1 parent 26856fe commit 9fe3f04
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
4 changes: 4 additions & 0 deletions examples/rename_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
//! - **Snake Case**: Keep all letters lowercase and indicate word boundaries
//! with underscores.
//! - **Verbatim**: Use the original attribute name defined in the code.
//!
//! - **Lower Case**: Keep all letters lowercase and remove word boundaries.
//!
//! - **Upper Case**: Keep all letters upperrcase and remove word boundaries.

use structopt::StructOpt;

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@
//! Usable only on field-level.
//!
//! - [`rename_all`](#specifying-argument-types):
//! [`rename_all = "kebab"/"snake"/"screaming-snake"/"camel"/"pascal"/"verbatim"]`
//! [`rename_all = "kebab"/"snake"/"screaming-snake"/"camel"/"pascal"/"verbatim"/"lower"/"upper"]`
//!
//! Usable both on top level and field level.
//!
Expand Down Expand Up @@ -309,7 +309,7 @@
//! Usable only on field-level.
//!
//! - [`rename_all_env`](##auto-deriving-environment-variables):
//! [`rename_all_env = "kebab"/"snake"/"screaming-snake"/"camel"/"pascal"/"verbatim"]`
//! [`rename_all_env = "kebab"/"snake"/"screaming-snake"/"camel"/"pascal"/"verbatim"/"lower"/"upper"]`
//!
//! Usable both on top level and field level.
//!
Expand Down
8 changes: 8 additions & 0 deletions structopt-derive/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ pub enum CasingStyle {
Snake,
/// Use the original attribute name defined in the code.
Verbatim,
/// Keep all letters lowercase and remove word boundaries.
Lower,
/// Keep all letters uppercase and remove word boundaries.
Upper,
}

#[derive(Clone)]
Expand Down Expand Up @@ -188,6 +192,8 @@ impl CasingStyle {
"screamingsnake" | "screamingsnakecase" => cs(ScreamingSnake),
"snake" | "snakecase" => cs(Snake),
"verbatim" | "verbatimcase" => cs(Verbatim),
"lower" | "lowercase" => cs(Lower),
"upper" | "uppercase" => cs(Upper),
s => abort!(name, "unsupported casing: `{}`", s),
}
}
Expand All @@ -208,6 +214,8 @@ impl Name {
ScreamingSnake => s.to_shouty_snake_case(),
Snake => s.to_snake_case(),
Verbatim => s,
Lower => s.to_snake_case().replace("_", ""),
Upper => s.to_shouty_snake_case().replace("_", ""),
};
quote_spanned!(ident.span()=> #s)
}
Expand Down
28 changes: 28 additions & 0 deletions tests/argument_naming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,31 @@ fn test_rename_all_is_propagation_can_be_overridden() {
Opt::from_clap(&Opt::clap().get_matches_from(&["test", "SECOND_VARIANT", "--foo-option"]))
);
}

#[test]
fn test_lower_is_renamed() {
#[derive(StructOpt, Debug, PartialEq)]
struct Opt {
#[structopt(rename_all = "lower", long)]
foo_option: bool,
}

assert_eq!(
Opt { foo_option: true },
Opt::from_clap(&Opt::clap().get_matches_from(&["test", "--foooption"]))
);
}

#[test]
fn test_upper_is_renamed() {
#[derive(StructOpt, Debug, PartialEq)]
struct Opt {
#[structopt(rename_all = "upper", long)]
foo_option: bool,
}

assert_eq!(
Opt { foo_option: true },
Opt::from_clap(&Opt::clap().get_matches_from(&["test", "--FOOOPTION"]))
);
}

0 comments on commit 9fe3f04

Please sign in to comment.