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

Selection of boundaries for the default implementation of is_case #21

Open
r-bk opened this issue Jul 18, 2024 · 0 comments
Open

Selection of boundaries for the default implementation of is_case #21

r-bk opened this issue Jul 18, 2024 · 0 comments

Comments

@r-bk
Copy link

r-bk commented Jul 18, 2024

@rutrum Hi!
Firstly, thanks for such a useful crate!

I have found the following example which IMO has a not intuitive result.

#[test]
fn test_upper_snake() {
    use convert_case::{Case, Casing};
    assert!("UPPER_CASE_WITH_DIGIT1".is_case(Case::UpperSnake));
}

This test fails, even though UpperSnake case has only a single boundary _ and the string is in capital letters.

This happens because the implementation of is_case initializes the StateConverter with Boundary::defaults() which includes UpperDigit boundary.

Is this by design, or a bug?

If a bug, would the correct implementation of is_case be something like this?

fn is_case(&self, case: Case) -> bool {
    &self.from_case(case).to_case(case) == self
}
r-bk added a commit to r-bk/tighterror that referenced this issue Jul 19, 2024
This commit fixes an issue with `convert_case` crate reported in [1].

The issue is that the default implementation of `is_case` provided by
that crate initializes the converter with almost all available
boundaries.

As a result the following conversion takes place:

"ERR1".to_case(Case::UpperSnake) -> "ERR_1"

Furthermore, the following check fails, while it seems like it
shouldn't because the string is in capital letters.

"ERR1".is_case(Case::UpperSnake)

This commit introduces a new primitive `convert_case` which receives two
case parameters, one for the source case and one of the target case.
This way the converter is initialized with the correct source case
boundaries. And it allows implementation of a new `is_case` primitive
that builds on `convert_case` to select the source case boundaries
correctly.

[1] rutrum/convert-case#21
r-bk added a commit to r-bk/tighterror that referenced this issue Jul 19, 2024
This commit fixes an issue with `convert_case` crate reported in [1].

The issue is that the default implementation of `is_case` provided by
that crate initializes the converter with almost all available
boundaries.

As a result the following conversion takes place:

"ERR1".to_case(Case::UpperSnake) -> "ERR_1"

Furthermore, the following check fails, while it seems like it
shouldn't because the string is in capital letters.

"ERR1".is_case(Case::UpperSnake)

This commit introduces a new primitive `convert_case` which receives two
case parameters, one for the source case and one of the target case.
This way the converter is initialized with the correct source case
boundaries. And it allows implementation of a new `is_case` primitive
that builds on `convert_case` to select the source case boundaries
correctly.

[1] rutrum/convert-case#21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant