classnames-const-rs
A Rust macro library for compile-time CSS class name concatenation and processing.
Features
- ๐ Zero runtime overhead - Everything happens at compile time
- ๐งน Automatic whitespace handling - Removes extra spaces and normalizes whitespace
- ๐ Multiple class concatenation - Combine any number of class names
- ๐ Type-safe - Compile-time string processing with full type safety
- ๐ฏ Simple API - Easy to use macro interface
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
Basic Usage
use *;
const BUTTON_CLASS: &str = classnames_concat!;
assert_eq!;
Handling Messy Whitespace
The macro automatically handles extra whitespace:
use *;
const MESSY_CLASSES: &str = classnames_concat!;
assert_eq!;
Empty Strings
Empty strings are handled gracefully:
use *;
const SPARSE_CLASS: &str = classnames_concat!;
assert_eq!;
Complex Example
use *;
const BASE_CLASSES: &str = "container mx-auto";
const RESPONSIVE_CLASSES: &str = "md:w-1/2 lg:w-1/3";
const STATE_CLASSES: &str = "hover:bg-blue-500 focus:outline-none";
const COMPONENT_CLASSES: &str = classnames_concat!;
// Result: "container mx-auto md:w-1/2 lg:w-1/3 hover:bg-blue-500 focus:outline-none p-4 rounded-lg"
API Reference
classnames_concat!
Concatenates multiple class name strings with automatic whitespace handling.
Syntax:
classnames_concat!
Features:
- Removes leading and trailing whitespace
- Collapses multiple consecutive spaces into single spaces
- Handles empty strings gracefully
- Works with any number of arguments
trim_format!
Internal macro for whitespace normalization. Generally not needed for direct use.
Use Cases
This library is perfect for:
- ๐จ CSS-in-Rust applications where you need to build class strings
- ๐ Web frameworks that generate HTML with dynamic classes
- ๐ฑ UI libraries that combine multiple styling concerns
- ๐ง Build-time optimizations where class strings are known at compile time
Performance
Since all processing happens at compile time, there is zero runtime overhead. The resulting strings are embedded directly into your binary as static string literals.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Built on top of the excellent
constcatcrate for compile-time string concatenation - Inspired by the JavaScript
classnameslibrary