classnames-const-rs 0.1.0

A Rust implementation of the classnames utility for conditionally joining class names at compile time.
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented3 out of 3 items with examples
  • Size
  • Source code size: 16.84 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.12 MB This is the summed size of all files generated by rustdoc for all configured targets
  • ร˜ build duration
  • this release: 15s Average build duration of successful builds.
  • all releases: 15s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • flattoride

classnames-const-rs

A Rust macro library for compile-time CSS class name concatenation and processing.

Crates.io Documentation License

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:

[dependencies]
classnames-const-rs = "0.1.0"

Usage

Basic Usage

use classnames_const_rs::*;

const BUTTON_CLASS: &str = classnames_concat!("btn", "btn-primary");
assert_eq!(BUTTON_CLASS, "btn btn-primary");

Handling Messy Whitespace

The macro automatically handles extra whitespace:

use classnames_const_rs::*;

const MESSY_CLASSES: &str = classnames_concat!("  header ", " main  ", "footer  ");
assert_eq!(MESSY_CLASSES, "header main footer");

Empty Strings

Empty strings are handled gracefully:

use classnames_const_rs::*;

const SPARSE_CLASS: &str = classnames_concat!("", "active", "", "highlight");
assert_eq!(SPARSE_CLASS, "active highlight");

Complex Example

use classnames_const_rs::*;

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!(
    BASE_CLASSES,
    RESPONSIVE_CLASSES,
    STATE_CLASSES,
    "p-4 rounded-lg"
);

// 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!(class1, class2, ..., classN)

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 constcat crate for compile-time string concatenation
  • Inspired by the JavaScript classnames library