1mod constant;
2mod snake;
3mod title;
4
5use convert_case as cc;
6use std::fmt::{self, Display};
7
8pub use snake::to_snake_case;
9
10#[derive(Clone, Copy, Debug)]
18pub enum Case {
19 Camel,
20 Constant,
21 Kebab,
22 Lower,
23 Sentence,
24 Snake,
25 Title,
26 Upper,
27 UpperCamel,
28 UpperSnake,
29 UpperKebab,
30}
31
32impl Display for Case {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 let label = match self {
35 Self::Camel => "Camel",
36 Self::Constant => "Constant",
37 Self::Kebab => "Kebab",
38 Self::Lower => "Lower",
39 Self::Sentence => "Sentence",
40 Self::Snake => "Snake",
41 Self::Title => "Title",
42 Self::Upper => "Upper",
43 Self::UpperCamel => "UpperCamel",
44 Self::UpperSnake => "UpperSnake",
45 Self::UpperKebab => "UpperKebab",
46 };
47
48 f.write_str(label)
49 }
50}
51
52pub trait Casing<T: std::fmt::Display> {
60 fn to_case(&self, case: Case) -> String;
62
63 fn is_case(&self, case: Case) -> bool;
65}
66
67impl<T: std::fmt::Display> Casing<T> for T
68where
69 String: PartialEq<T>,
70{
71 fn to_case(&self, case: Case) -> String {
72 let s = &self.to_string();
73
74 match case {
75 Case::Lower => s.to_lowercase(),
76 Case::Upper => s.to_uppercase(),
77 Case::Title => title::to_title_case(s),
78 Case::Snake => snake::to_snake_case(s),
79 Case::UpperSnake => snake::to_snake_case(s).to_uppercase(),
80 Case::Constant => constant::to_constant_case(s).to_uppercase(),
81 Case::Camel => cc::Casing::to_case(s, cc::Case::Camel),
82 Case::Kebab => cc::Casing::to_case(s, cc::Case::Kebab),
83 Case::Sentence => cc::Casing::to_case(s, cc::Case::Sentence),
84 Case::UpperCamel => cc::Casing::to_case(s, cc::Case::UpperCamel),
85 Case::UpperKebab => cc::Casing::to_case(s, cc::Case::Kebab).to_uppercase(),
86 }
87 }
88
89 fn is_case(&self, case: Case) -> bool {
90 &self.to_case(case) == self
91 }
92}