Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for lower/upper in rename_all #412

Merged
merged 2 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]))
);
}