clsx 0.1.1

A flexible class name composition utility for Rust, inspired by clsx and tailwind-merge.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use std::collections::HashMap;

/* -------------------------------------------------------------------------------------------------
 * Internal Macro: __clsx_count_args
 * -----------------------------------------------------------------------------------------------*/

/// Count how many comma-separated arguments appear in a macro invocation.
/// For example: `__clsx_count_args!(a, b, c)` → `3`.
#[doc(hidden)]
#[macro_export]
macro_rules! __clsx_count_args {
    () => (0usize);
    ($head:expr) => (1usize);
    ($head:expr, $($tail:expr),*) => (1usize + $crate::__clsx_count_args!($($tail),*));
}

/* -------------------------------------------------------------------------------------------------
 * Trait: ClsxArg
 * -----------------------------------------------------------------------------------------------*/

/// A trait representing any type that can be converted into one or more class names.
///
/// Implementors of this trait should append their class string(s) into the given `out` buffer,
/// optionally adding a leading space if `out` is not empty.
///
/// ## Common Implementors
/// - `&str` / `String` (directly appended)
/// - Booleans (ignored by default)
/// - Numeric types (converted to string)
/// - `Option<T>` (appended if `Some`, ignored if `None`)
/// - Slices/arrays of any `ClsxArg` type
/// - `HashMap<String, bool>` (keys appended if value is `true`)
/// - Tuples like `(bool, &str)` or `(bool, String)` (included only if boolean is `true`)
/// - Closures returning something that implements `ClsxArg`
pub trait ClsxArg {
    /// Appends this argument's class(es) into `out`.
    ///
    /// Implementors should insert a leading space if `out` is non-empty and a valid class
    /// is about to be appended.
    fn append_to(&self, out: &mut String);
}

/* -------------------------------------------------------------------------------------------------
 * Helper: push_with_space_if_needed
 * -----------------------------------------------------------------------------------------------*/

#[inline]
fn push_with_space_if_needed(out: &mut String, val: &str) {
    if !val.is_empty() {
        if !out.is_empty() {
            out.push(' ');
        }
        out.push_str(val);
    }
}

/* -------------------------------------------------------------------------------------------------
 * Implementations for Strings & Str
 * -----------------------------------------------------------------------------------------------*/

impl ClsxArg for &str {
    #[inline]
    fn append_to(&self, out: &mut String) {
        push_with_space_if_needed(out, self);
    }
}

impl ClsxArg for String {
    #[inline]
    fn append_to(&self, out: &mut String) {
        push_with_space_if_needed(out, self);
    }
}

impl ClsxArg for &String {
    #[inline]
    fn append_to(&self, out: &mut String) {
        push_with_space_if_needed(out, self);
    }
}

impl ClsxArg for &&str {
    #[inline]
    fn append_to(&self, out: &mut String) {
        push_with_space_if_needed(out, self);
    }
}

impl ClsxArg for &&&str {
    #[inline]
    fn append_to(&self, out: &mut String) {
        push_with_space_if_needed(out, self);
    }
}

/* -------------------------------------------------------------------------------------------------
 * Booleans
 * -----------------------------------------------------------------------------------------------*/

/// Booleans don't contribute anything by default.
impl ClsxArg for bool {
    #[inline]
    fn append_to(&self, _out: &mut String) {
        // no-op
    }
}

/* -------------------------------------------------------------------------------------------------
 * Numeric Types
 * -----------------------------------------------------------------------------------------------*/

macro_rules! impl_number {
    ($($t:ty),+) => {
        $(
            impl ClsxArg for $t {
                #[inline]
                fn append_to(&self, out: &mut String) {
                    if !out.is_empty() {
                        out.push(' ');
                    }
                    use std::fmt::Write;
                    let _ = write!(out, "{}", self);
                }
            }
        )+
    }
}

impl_number!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize, f32, f64);

/* -------------------------------------------------------------------------------------------------
 * Collections
 * -----------------------------------------------------------------------------------------------*/

impl<T: ClsxArg> ClsxArg for Option<T> {
    #[inline]
    fn append_to(&self, out: &mut String) {
        if let Some(val) = self {
            val.append_to(out);
        }
    }
}

impl<T: ClsxArg> ClsxArg for Vec<T> {
    #[inline]
    fn append_to(&self, out: &mut String) {
        for item in self {
            item.append_to(out);
        }
    }
}

impl<T: ClsxArg> ClsxArg for &[T] {
    #[inline]
    fn append_to(&self, out: &mut String) {
        for item in *self {
            item.append_to(out);
        }
    }
}

impl<T: ClsxArg, const N: usize> ClsxArg for [T; N] {
    #[inline]
    fn append_to(&self, out: &mut String) {
        for item in self {
            item.append_to(out);
        }
    }
}

/* -------------------------------------------------------------------------------------------------
 * HashMap of (String -> bool)
 * -----------------------------------------------------------------------------------------------*/

impl ClsxArg for HashMap<String, bool> {
    #[inline]
    fn append_to(&self, out: &mut String) {
        for (class_name, flag) in self.iter() {
            if *flag && !class_name.is_empty() {
                push_with_space_if_needed(out, class_name);
            }
        }
    }
}

/* -------------------------------------------------------------------------------------------------
 * Tuples: (bool, &str) and (bool, String)
 * -----------------------------------------------------------------------------------------------*/

impl ClsxArg for (bool, &str) {
    #[inline]
    fn append_to(&self, out: &mut String) {
        if self.0 && !self.1.is_empty() {
            push_with_space_if_needed(out, self.1);
        }
    }
}

impl ClsxArg for (bool, String) {
    #[inline]
    fn append_to(&self, out: &mut String) {
        if self.0 && !self.1.is_empty() {
            push_with_space_if_needed(out, &self.1);
        }
    }
}

/* -------------------------------------------------------------------------------------------------
 * Closures returning a ClsxArg
 * -----------------------------------------------------------------------------------------------*/

impl<F, R> ClsxArg for F
where
    F: Fn() -> R,
    R: ClsxArg,
{
    #[inline]
    fn append_to(&self, out: &mut String) {
        (self)().append_to(out);
    }
}

/* -------------------------------------------------------------------------------------------------
 * Macro: clsx!(...)
 * -----------------------------------------------------------------------------------------------*/

/// The `clsx!` macro composes class names in a single pass, appending each
/// argument's class(es) into one final space‐separated `String`.
///
/// This macro also pre‐allocates space for the output `String`, using
/// a rough guess of ~8 characters per argument to reduce reallocation overhead.
///
/// # Examples
///
/// ```rust
/// use clsx::clsx;
///
/// let is_active = true;
/// let classes = clsx!("btn", (is_active, "btn-active"), "p-4");
/// assert_eq!(classes, "btn btn-active p-4");
///
/// // Flatten arrays, slices, etc.:
/// let nested = clsx!(["foo", "bar"], (true, "extra"));
/// assert_eq!(nested, "foo bar extra");
/// ```
#[macro_export]
macro_rules! clsx {
    () => {
        String::new()
    };
    ($($arg:expr),+ $(,)?) => {{
        const __COUNT: usize = $crate::__clsx_count_args!($($arg),*);
        let mut out = String::with_capacity(__COUNT * 8);
        $(
            $crate::ClsxArg::append_to(&$arg, &mut out);
        )+
        out
    }};
}

/* -------------------------------------------------------------------------------------------------
 * Tests
 * -----------------------------------------------------------------------------------------------*/

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    #[test]
    fn test_clsx_basic() {
        let result = clsx!("class1", "class2", "class3");
        assert_eq!(result, "class1 class2 class3");
    }

    #[test]
    fn test_clsx_single_argument() {
        let result = clsx!("solo");
        assert_eq!(result, "solo");
    }

    #[test]
    fn test_clsx_no_arguments() {
        let result = clsx!();
        assert_eq!(result, "");
    }

    #[test]
    fn test_clsx_tuple_true() {
        let is_active = true;
        let classes = clsx!("btn", (is_active, "btn-active"));
        assert_eq!(classes, "btn btn-active");
    }

    #[test]
    fn test_clsx_tuple_false() {
        let is_active = false;
        let classes = clsx!("btn", (is_active, "btn-active"));
        assert_eq!(classes, "btn");
    }

    #[test]
    fn test_clsx_with_conditionals() {
        let is_active = true;
        let is_disabled = false;
        let result = clsx!(
            "btn",
            (is_active, "btn-active"),
            (is_disabled, "btn-disabled"),
        );
        assert_eq!(result, "btn btn-active");
    }

    #[test]
    fn test_clsx_all_false_conditions() {
        let is_active = false;
        let is_disabled = false;
        let result = clsx!(
            "btn",
            (is_active, "btn-active"),
            (is_disabled, "btn-disabled"),
        );
        assert_eq!(result, "btn");
    }

    #[test]
    fn test_clsx_with_options() {
        let some_class: Option<&str> = Some("visible");
        let none_class: Option<&str> = None;
        let result = clsx!(some_class, none_class, "base");
        assert_eq!(result, "visible base");
    }

    #[test]
    fn test_clsx_with_hashmap() {
        let mut map = HashMap::new();
        map.insert("flex".to_string(), true);
        map.insert("hidden".to_string(), false);
        let result = clsx!(map, "base");
        assert_eq!(result, "flex base");
    }

    #[test]
    fn test_clsx_with_closures() {
        let result = clsx!(
            "base",
            || "dynamic".to_string(),
            || if true { "active".to_string() } else { "".to_string() },
        );
        assert_eq!(result, "base dynamic active");
    }

    #[test]
    fn test_clsx_with_closures_false() {
        let flag = false;
        let result = clsx!(
            "something",
            || if flag { "hidden" } else { "" },
        );
        assert_eq!(result, "something");
    }

    #[test]
    fn test_clsx_nested_structures() {
        let array = ["nested1", "nested2"];
        let result = clsx!(
            array,
            (true, "conditional"),
            ["deeply", "nested", "class"]
        );
        assert_eq!(result, "nested1 nested2 conditional deeply nested class");
    }

    #[test]
    fn test_clsx_large_array() {
        let arr = ["one", "two", "three", "four", "five"];
        let result = clsx!(arr, "six");
        assert_eq!(result, "one two three four five six");
    }

    #[test]
    fn test_clsx_with_numerics() {
        let i = 10;
        let f = 3.14;
        let negative = -5i32;
        let result = clsx!("start", i, f, negative, "end");
        assert_eq!(result, "start 10 3.14 -5 end");
    }

    #[test]
    fn test_clsx_with_bools() {
        let result = clsx!("hello", true, false, "world");
        assert_eq!(result, "hello world");
    }

    #[test]
    fn test_clsx_if_expression() {
        let show_extra = false;
        let classes = clsx!(
            "core",
            if show_extra { "extra" } else { "" },
            "final"
        );
        assert_eq!(classes, "core final");
    }

    #[test]
    fn test_clsx_closure_returning_option() {
        let maybe = || -> Option<&'static str> { Some("maybe-yes") };
        let never = || -> Option<&'static str> { None };
        let result = clsx!("start", maybe, never, "end");
        assert_eq!(result, "start maybe-yes end");
    }
}