Tweld
(you can read it as tiny-weld, token-weld, or just tweld, I am just happy to be here)
Tweld is a procedural macro toolkit and naming DSL for Rust. It allows you to dynamically generate, modify, and compose identifiers directly within your Rust code using a clean, safe, and intuitive @[] syntax (hopefully). Because the parsing happens safely inside the macro, any syntax errors in your modifiers will point exactly to the broken line in your IDE (also hopefully).
weld!;
The name comes from idea of fusing tokens, to help when writing macros, or macros for your macros (which was my initial case).
The @[] "interpolator"
Anything inside the @[] "interpolator" will be fused together. You can use the @[] syntax inside structs, functions, trait implementations, or anywhere an identifier is expected, as well as in the content of string literal.
"@[one - two]"will render"one-two"
It can be used with tokens to create identifiers, or inside a string literal.
weld!;
Inside the the brackets, you can organize it in groups, and apply specific modifiers to that group.
use render_names;
weld!
Modifiers
While inside a group, you can apply a chain of modifiers, where each one will perform an operation on the previous result.
Simple modifiers:
These are self explanatory, being singular and plural just the removal of the letter 's' from the last word.
singular,plural,lower,lowercaseupper,uppercase
Casing style modifiers:
Casing style modifiers make use of the crate heck.
- PascalCase:
pascal,pascalcase,uppercamelcase - camelCase:
lowercamelcase,camelcase,camel - snake_case
snakecase,snake,snekcase,snek - Title Case:
titlecase,title - kebab-case:
kebabcase,kebab - Train-Case:
traincase, `train - SHOUTY-KEBAB-CASE:
shoutykebabcase,shoutykebab - SHOUTY_SNAKE_CASE:
shoutysnakecase,shoutysnake,shoutysnekcase,shoutysnek
Limitations
Given the nature of the syntax, when applied to tokens (as in the body or signature of a function, etc), some of these modifiers will behave a bit different.
kebabcase,shoutykebabcase, andtraincasewon't work,titlecasewill behave likePascalCaseWhen applied to string literals, they will all work as intended
Advanced modifiers:
This crate comes with some modifiers that offer more complex operations that can be pretty helpful, given the right context
replace
It replaces all non-overlapping occurrences of a pattern, with a replacement string.
// will render const a_small_ident = "";
weld!;
substr, substring
This modifier returns the part of this string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied. Both indexes are optional.
// will render const a_long_ident = "";
weld!;