pub struct CaseDetector { /* private fields */ }Expand description
A detector for determining which cases a string matches.
CaseDetector maintains a pool of cases and provides a method
to detect which cases from the pool match a given string.
§Example
use convert_case::Case;
use convert_case_extras::CaseDetector;
// Default detector with all standard cases
let detector = CaseDetector::default();
let matches = detector.detect_cases("my_variable_name");
assert!(matches.contains(&Case::Snake));
// Custom detector with specific cases
let detector = CaseDetector::new()
.add_case(Case::Snake)
.add_case(Case::Kebab);
let matches = detector.detect_cases("hello-world");
assert_eq!(matches, vec![Case::Kebab]);Implementations§
Source§impl CaseDetector
impl CaseDetector
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new CaseDetector with an empty pool.
Use builder methods like add_case to populate the pool.
Use default instead to use all of the cases available in convert-case.
§Example
use convert_case::Case;
use convert_case_extras::CaseDetector;
let detector = CaseDetector::new()
.add_case(Case::Snake)
.add_case(Case::Kebab);Sourcepub fn add_case(self, case: Case<'static>) -> Self
pub fn add_case(self, case: Case<'static>) -> Self
Adds a case to the pool. Returns self for method chaining.
§Example
use convert_case::Case;
use convert_case_extras::CaseDetector;
let detector = CaseDetector::new()
.add_case(Case::Snake)
.add_case(Case::Kebab);Sourcepub fn add_cases(self, cases: &[Case<'static>]) -> Self
pub fn add_cases(self, cases: &[Case<'static>]) -> Self
Adds multiple cases to the pool. Returns self for method chaining.
§Example
use convert_case::Case;
use convert_case_extras::CaseDetector;
let detector = CaseDetector::new()
.add_cases(&[Case::Snake, Case::Kebab, Case::Camel]);Sourcepub fn remove_case(self, case: Case<'static>) -> Self
pub fn remove_case(self, case: Case<'static>) -> Self
Removes a case from the pool. Returns self for method chaining.
§Example
use convert_case::Case;
use convert_case_extras::CaseDetector;
let detector = CaseDetector::default()
.remove_case(Case::Flat)
.remove_case(Case::UpperFlat);Sourcepub fn remove_cases(self, cases: &[Case<'static>]) -> Self
pub fn remove_cases(self, cases: &[Case<'static>]) -> Self
Removes multiple cases from the pool. Returns self for method chaining.
§Example
use convert_case::Case;
use convert_case_extras::CaseDetector;
let detector = CaseDetector::default()
.remove_cases(&[Case::Flat, Case::UpperFlat]);Sourcepub fn detect_cases<T: AsRef<str>>(&self, s: T) -> Vec<Case<'static>>
pub fn detect_cases<T: AsRef<str>>(&self, s: T) -> Vec<Case<'static>>
Detects all cases from the pool that the given string matches.
A string “matches” a case if converting it to that case produces
the same string (i.e., s.to_case(case) == s).
§Example
use convert_case::Case;
use convert_case_extras::CaseDetector;
let detector = CaseDetector::default();
let matches = detector.detect_cases("hello_world");
assert!(matches.contains(&Case::Snake));
assert!(!matches.contains(&Case::Kebab));
// Single lowercase word matches multiple cases
let matches = detector.detect_cases("word");
assert!(matches.contains(&Case::Snake));
assert!(matches.contains(&Case::Kebab));
assert!(matches.contains(&Case::Flat));Trait Implementations§
Source§impl Clone for CaseDetector
impl Clone for CaseDetector
Source§fn clone(&self) -> CaseDetector
fn clone(&self) -> CaseDetector
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more