tailwindcss_to_rust_macros/
to_option_vec_string.rs

1// This code was copied from Seed (https://github.com/seed-rs/seed) and
2// modified.
3//
4// Copyright 2019 DavidOConnor <david.alan.oconnor@gmail.com>
5//
6// Licensed under the MIT license only.
7
8//! Contains a trait to make the macros work with many types.
9//!
10//! ```rust,ignore
11//! use tailwindcss_to_rust_macros::{C, M, ToOptionvecstring};
12//! ```
13/// You need to make sure this trait is imported by any code that wants to use
14/// the `C!`, `DC!`, or `M!` macros.
15pub trait ToOptionVecString {
16    fn to_option_vec_string(self) -> Option<Vec<String>>;
17}
18
19// ------ Implementations ------
20
21impl<T: ToOptionVecString + Clone> ToOptionVecString for &T {
22    fn to_option_vec_string(self) -> Option<Vec<String>> {
23        self.clone().to_option_vec_string()
24    }
25}
26
27// --- Texts ---
28
29impl ToOptionVecString for String {
30    fn to_option_vec_string(self) -> Option<Vec<String>> {
31        Some(vec![self])
32    }
33}
34
35impl ToOptionVecString for &str {
36    fn to_option_vec_string(self) -> Option<Vec<String>> {
37        Some(vec![self.to_string()])
38    }
39}
40
41// --- Containers ---
42
43impl<T: ToOptionVecString> ToOptionVecString for Option<T> {
44    fn to_option_vec_string(self) -> Option<Vec<String>> {
45        self.and_then(ToOptionVecString::to_option_vec_string)
46    }
47}
48
49impl<T: ToOptionVecString> ToOptionVecString for Vec<T> {
50    fn to_option_vec_string(self) -> Option<Vec<String>> {
51        let classes = self
52            .into_iter()
53            .filter_map(ToOptionVecString::to_option_vec_string)
54            .flatten();
55        Some(classes.collect())
56    }
57}
58
59impl<T: ToOptionVecString + Clone> ToOptionVecString for &[T] {
60    fn to_option_vec_string(self) -> Option<Vec<String>> {
61        let classes = self
62            .iter()
63            .filter_map(ToOptionVecString::to_option_vec_string)
64            .flatten();
65        Some(classes.collect())
66    }
67}