Skip to content

new!: added cobol_case_with_options and made use it in existing cobol case functions #27

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

Merged
merged 1 commit into from
Feb 20, 2025
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
156 changes: 101 additions & 55 deletions src/caser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2024 Takayuki Sato. All Rights Reserved.
// Copyright (C) 2024-2025 Takayuki Sato. All Rights Reserved.
// This program is free software under MIT License.
// See the file LICENSE in this distribution for more details.

Expand Down Expand Up @@ -62,64 +62,55 @@ pub trait Caser<T: AsRef<str>> {

// cobol case

/// Converts a string to cobol case.
/// Converts the input string to cobol case.
///
/// This method targets the upper and lower cases of ASCII alphabets for
/// capitalization, and all characters except ASCII alphabets and ASCII
/// numbers are replaced to hyphens as word separators.
/// It treats the end of a sequence of non-alphabetical characters as a word boundary,
/// but not the beginning.
///
/// ```rust
/// use stringcase::Caser;
///
/// let cobol = "fooBar100Baz".to_cobol_case();
/// assert_eq!(cobol, "FOO-BAR100-BAZ");
/// let cobol = "foo_bar_baz".to_cobol_case();
/// assert_eq!(cobol, "FOO-BAR-BAZ");
/// ```
fn to_cobol_case(&self) -> String;

/// Converts a string to cobol case.
///
/// This method targets the upper and lower cases of ASCII alphabets and
/// ASCII numbers for capitalization, and all characters except ASCII
/// alphabets and ASCII numbers are replaced to hyphens as word separators.
/// Converts the input string to cobol case with the specified options.
///
/// ```rust
/// use stringcase::Caser;
///
/// let cobol = "fooBar100Baz".to_cobol_case_with_nums_as_word();
/// let opts = stringcase::Options{
/// separate_before_non_alphabets: true,
/// separate_after_non_alphabets: true,
/// separators: "",
/// keep: "",
/// };
/// let cobol = stringcase::cobol_case_with_options("foo_bar_100_baz", &opts);
/// assert_eq!(cobol, "FOO-BAR-100-BAZ");
/// ```
fn to_cobol_case_with_nums_as_word(&self) -> String;
fn to_cobol_case_with_options(&self, opts: &Options) -> String;

/// Converts a string to cobol case using the specified characters as
/// separators.
///
/// This method targets only the upper and lower cases of ASCII alphabets
/// for capitalization, and the characters specified as the second argument
/// of this method are regarded as word separators and are replaced to
/// hyphens.
/// Converts the input string to cobol case.
///
/// ```rust
/// use stringcase::Caser;
///
/// let cobol = "foo-bar100%baz".to_cobol_case_with_sep("- ");
/// assert_eq!(cobol, "FOO-BAR100%-BAZ");
/// ```
/// It treats the begin and the end of a sequence of non-alphabetical characters as a word
/// boundary.
#[deprecated(
since = "0.4.0",
note = "Should use to_cobol_case_with_options instead"
)]
fn to_cobol_case_with_nums_as_word(&self) -> String;

/// Converts the input string to cobol case with the specified separator characters.
#[deprecated(
since = "0.4.0",
note = "Should use to_cobol_case_with_options instead"
)]
fn to_cobol_case_with_sep(&self, seps: &str) -> String;

/// Converts a string to cobol case using characters other than the
/// specified characters as separators.
///
/// This method targets only the upper and lower cases of ASCII alphabets
/// for capitalization, and the characters other than the specified
/// characters as the second argument of this method are regarded as word
/// separators and are replaced to hyphens.
///
/// ```rust
/// use stringcase::Caser;
///
/// let cobol = "foo-bar100%baz".to_cobol_case_with_keep("%");
/// assert_eq!(cobol, "FOO-BAR100%-BAZ");
/// ```
/// Converts the input string to cobol case with the specified characters to be kept.
#[deprecated(
since = "0.4.0",
note = "Should use to_cobol_case_with_options instead"
)]
fn to_cobol_case_with_keep(&self, keeped: &str) -> String;

// kebab case
Expand Down Expand Up @@ -418,7 +409,13 @@ impl<T: AsRef<str>> Caser<T> for T {
// camel case

fn to_camel_case(&self) -> String {
camel_case(&self.as_ref())
let opts = Options {
separate_before_non_alphabets: false,
separate_after_non_alphabets: true,
separators: "",
keep: "",
};
camel_case_with_options(&self.as_ref(), &opts)
}

fn to_camel_case_with_options(&self, opts: &Options) -> String {
Expand Down Expand Up @@ -448,19 +445,47 @@ impl<T: AsRef<str>> Caser<T> for T {
// cobol case

fn to_cobol_case(&self) -> String {
cobol_case(&self.as_ref())
let opts = Options {
separate_before_non_alphabets: false,
separate_after_non_alphabets: true,
separators: "",
keep: "",
};
cobol_case_with_options(&self.as_ref(), &opts)
}

fn to_cobol_case_with_options(&self, opts: &Options) -> String {
cobol_case_with_options(&self.as_ref(), opts)
}

fn to_cobol_case_with_nums_as_word(&self) -> String {
cobol_case_with_nums_as_word(&self.as_ref())
let opts = Options {
separate_before_non_alphabets: true,
separate_after_non_alphabets: true,
separators: "",
keep: "",
};
cobol_case_with_options(&self.as_ref(), &opts)
}

fn to_cobol_case_with_sep(&self, seps: &str) -> String {
cobol_case_with_sep(&self.as_ref(), seps)
let opts = Options {
separate_before_non_alphabets: false,
separate_after_non_alphabets: true,
separators: seps,
keep: "",
};
cobol_case_with_options(&self.as_ref(), &opts)
}

fn to_cobol_case_with_keep(&self, keeped: &str) -> String {
cobol_case_with_keep(&self.as_ref(), keeped)
fn to_cobol_case_with_keep(&self, kept: &str) -> String {
let opts = Options {
separate_before_non_alphabets: false,
separate_after_non_alphabets: true,
separators: "",
keep: kept,
};
cobol_case_with_options(&self.as_ref(), &opts)
}

// kebab case
Expand Down Expand Up @@ -614,31 +639,52 @@ mod tests_of_caser {

#[test]
fn it_should_convert_to_cobol_case_with_nums_as_word() {
let result = "foo_bar100%BAZQux".to_cobol_case_with_nums_as_word();
let opts = Options {
separate_before_non_alphabets: true,
separate_after_non_alphabets: true,
separators: "",
keep: "",
};

let result = "foo_bar100%BAZQux".to_cobol_case_with_options(&opts);
assert_eq!(result, "FOO-BAR-100-BAZ-QUX");

let string = String::from("foo_bar100%BAZQux");
let result = string.to_cobol_case_with_nums_as_word();
let result = string.to_cobol_case_with_options(&opts);
assert_eq!(result, "FOO-BAR-100-BAZ-QUX");
}

#[test]
fn it_should_convert_to_cobol_case_with_sep() {
let result = "foo_bar100%BAZQux".to_cobol_case_with_sep("_");
let opts = Options {
separate_before_non_alphabets: false,
separate_after_non_alphabets: true,
separators: "_",
keep: "",
};

let result = "foo_bar100%BAZQux".to_cobol_case_with_options(&opts);
assert_eq!(result, "FOO-BAR100%-BAZ-QUX");

let string = String::from("foo_bar100%BAZQux");
let result = string.to_cobol_case_with_sep("_");
let result = string.to_cobol_case_with_options(&opts);
assert_eq!(result, "FOO-BAR100%-BAZ-QUX");
}

#[test]
fn it_should_convert_to_cobol_case_with_keep() {
let result = "foo_bar100%BAZQux".to_cobol_case_with_keep("%");
let opts = Options {
separate_before_non_alphabets: false,
separate_after_non_alphabets: true,
separators: "",
keep: "%",
};

let result = "foo_bar100%BAZQux".to_cobol_case_with_options(&opts);
assert_eq!(result, "FOO-BAR100%-BAZ-QUX");

let string = String::from("foo_bar100%BAZQux");
let result = string.to_cobol_case_with_keep("%");
let result = string.to_cobol_case_with_options(&opts);
assert_eq!(result, "FOO-BAR100%-BAZ-QUX");
}

Expand Down
Loading