convert strings
init
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Cargo.toml | 16 | ||||
| -rw-r--r-- | LICENSE | 21 | ||||
| -rw-r--r-- | src/lib.rs | 29 |
4 files changed, 67 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..cdf2326 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "strconv" +version = "0.1.0" +edition = "2021" +author = ["bend-n <[email protected]>"] +description = "basic string conversions" +repository = "https://github.com/bend-n/strconv.git" +license = "MIT" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +proc-macro = true + +[dependencies] +syn = "2.0.15" @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 bendn + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..4b9d626 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,29 @@ +use proc_macro::TokenStream; +use syn::{parse_macro_input, LitStr}; + +#[proc_macro] +pub fn kebab2title(input: TokenStream) -> TokenStream { + let input_str = parse_macro_input!(input as LitStr).value(); + + let converted = kebab2title_impl(&input_str); + format!("\"{converted}\"").parse().unwrap() +} + +fn kebab2title_impl(data: &str) -> String { + let mut result = String::with_capacity(data.len()); + let mut capitalize_next = true; + + for c in data.chars() { + if c == '-' { + result.push(' '); + capitalize_next = true; + } else if capitalize_next { + result.push(c.to_ascii_uppercase()); + capitalize_next = false; + } else { + result.push(c); + } + } + + result +} |