This project exists to allow one to work with programming identifiers in a more feature-complete way than normal Ident types.
use typed_ident::core::fmt::*;
use typed_ident::presets::unicode::*;
// Validate the format of input to see that it matches expectations.
let identifier = UpperCamelIdent::new("HTTPServer")?;
// Look at all of the segments of the identifier.
println!("identifier segments:");
for segment in identifier.segments() {
println!("* {segment:?}"); // [Chunk("HTTP"), Chunk("Server")]
}
// Convert to a different displayable format.
let lower_snake = identifier.as_lower_snake();
println!("as lower-snake: {lower_snake}"); // "http_server"
// Create a decorated identifier.
let decorated = identifier.with_circumfix("__", "__")?;
println!("with decoration: {decorated}"); // "__HTTPServer__"
For more information, check either the crates page, or read the project documentation.
A lot of the things I work on are code generators. There was no existing crate that satisfied all of my requirements, and it’s important to get the requirements right before you start generating a lot of code.
In particular, the following things were important to me:
- The ability to constrain user input to some specific kind of cased format (e.g.
UpperCamelorlower_snake). - The ability to safely convert between cases without losing important information or invalidating the identifier.
- The ability to make small mutations to existing identifiers (like prefixing, suffixing, etc).
- The ability to inspect the structure of an identifier (its parts, delimiters, words, etc).
One could argue that case conversion is possible today, but if I had to nit-pick none of them quite work exactly as I want (and none of them allow for specific configurations to make them work exactly how I want).
The specific things lacking from other libraries were:
- Handling of capitalization cased conversion for letters which uppercase to multiple letters instead of just one (
ß->SS->Ss). - Boundary detection satisfying Unicode Technical Standard #55.
- Not all of the mutation properties were safely present (such as suffixing or prefixing) while maintaining format invariants.
- Most APIs either had no identifier inspection functions, or they were limited or detached from the type structure.