textiler_core/
utils.rs

1//! Utils module
2
3use std::hash::{Hash, Hasher};
4use std::ops::Deref;
5
6use heck::ToKebabCase;
7use yew::Hook;
8
9pub mod bounded_float;
10
11pub static CSS_SELECTOR_OPERATORS: &[char] = &['.', '+', '>', '~', '&', ','];
12
13/// Converts to css property
14
15pub fn to_property(key: impl AsRef<str>) -> String {
16    let key = key.as_ref();
17    if (key.starts_with('[') && key.ends_with(']')) || key.starts_with(CSS_SELECTOR_OPERATORS) {
18        key.to_string()
19    } else {
20        key.split_inclusive(CSS_SELECTOR_OPERATORS)
21            .map(|key_part| {
22                let selector_index = key_part.rfind(CSS_SELECTOR_OPERATORS);
23                let selector_op = selector_index.as_ref().map(|index| &key_part[*index..]);
24
25                let reworked = selector_index
26                    .map(|index| &key_part[..index])
27                    .unwrap_or(key_part)
28                    .split('-')
29                    .map(ToKebabCase::to_kebab_case)
30                    .collect::<Vec<String>>()
31                    .join("-");
32
33                format!("{reworked}{}", selector_op.unwrap_or(""))
34            })
35            .collect::<String>()
36    }
37}