const_format/macros/
map_ascii_case.rs

1/// Converts the casing style of a `&'static str` constant,
2/// ignoring non-ascii unicode characters.
3///
4/// This nacro is equivalent to a function with this signature:
5///
6/// ```rust
7/// const fn map_ascii_case(case: const_format::Case, input: &'static str) -> &'static str
8/// # {""}
9/// ```
10///
11/// The [`Case`](enum.Case.html) parameter determines the casing style of the returned string.
12///
13/// # Ascii
14///
15/// This only transforms ascii characters because broader unicode case conversion,
16/// while possible, is much harder to implement purely with `const fn`s.
17///
18/// Non-ascii characters are treated as though they're alphabetic ascii characters.
19///
20/// # Ignored characters
21///
22/// These casing styles treat non-alphanumeric ascii characters as spaces,
23/// removing them from the returned string:
24///
25/// - `Case::Pascal`
26/// - `Case::Camel`
27/// - `Case::Snake`
28/// - `Case::UpperSnake`
29/// - `Case::Kebab`
30/// - `Case::UpperKebab`
31///
32/// # Example
33///
34/// ```rust
35/// use const_format::{Case, map_ascii_case};
36///
37/// {
38///     const LOW: &str = map_ascii_case!(Case::Lower, "hello WORLD");
39///     assert_eq!(LOW, "hello world");
40/// }
41/// {
42///     const IN: &str = "hello WORLD каждому";
43///     const OUT: &str = map_ascii_case!(Case::Upper, IN);
44///     // non-ascii characters are ignored by map_ascii_case.
45///     assert_eq!(OUT, "HELLO WORLD каждому");
46/// }
47///
48/// const IN2: &str = "hello fooкаждому100Bar#qux";
49/// {
50///     const OUT: &str = map_ascii_case!(Case::Pascal, IN2);
51///     assert_eq!(OUT, "HelloFooкаждому100BarQux");
52/// }
53/// {
54///     const OUT: &str = map_ascii_case!(Case::Camel, IN2);
55///     assert_eq!(OUT, "helloFooкаждому100BarQux");
56/// }
57/// {
58///     const OUT: &str = map_ascii_case!(Case::Snake, IN2);
59///     assert_eq!(OUT, "hello_fooкаждому_100_bar_qux");
60/// }
61/// {
62///     const OUT: &str = map_ascii_case!(Case::UpperSnake, IN2);
63///     assert_eq!(OUT, "HELLO_FOOкаждому_100_BAR_QUX");
64/// }
65/// {
66///     const OUT: &str = map_ascii_case!(Case::Kebab, IN2);
67///     assert_eq!(OUT, "hello-fooкаждому-100-bar-qux");
68/// }
69/// {
70///     const OUT: &str = map_ascii_case!(Case::UpperKebab, IN2);
71///     assert_eq!(OUT, "HELLO-FOOкаждому-100-BAR-QUX");
72/// }
73///
74///
75/// ```
76#[macro_export]
77macro_rules! map_ascii_case {
78    ($case:expr, $str:expr) => {
79        $crate::__str_const! {{
80            const S_OSRCTFL4A: &$crate::pmr::str = $str;
81            const CASE_OSRCTFL4A: $crate::Case = $case;
82            {
83                const L: $crate::pmr::usize =
84                    $crate::__ascii_case_conv::size_after_conversion(CASE_OSRCTFL4A, S_OSRCTFL4A);
85
86                const OB: &[$crate::pmr::u8; L] =
87                    &$crate::__ascii_case_conv::convert_str::<L>(CASE_OSRCTFL4A, S_OSRCTFL4A);
88
89                const OS: &$crate::pmr::str = unsafe { $crate::__priv_transmute_bytes_to_str!(OB) };
90
91                OS
92            }
93        }}
94    };
95}