pub(crate) fn compose<'a>(parts: impl IntoIterator<Item = Option<&'a str>>) -> String {
let mut out = String::new();
for part in parts.into_iter().flatten() {
if part.is_empty() {
continue;
}
if !out.is_empty() {
out.push(' ');
}
out.push_str(part);
}
out
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum Glass {
#[default]
Off,
On,
Subtle,
Strong,
}
impl From<bool> for Glass {
fn from(value: bool) -> Self {
if value { Self::On } else { Self::Off }
}
}
impl Glass {
pub(crate) fn class(self, base: &str) -> Option<String> {
let intensity = match self {
Self::Off => return None,
Self::On => None,
Self::Subtle => Some("ui-glass-subtle"),
Self::Strong => Some("ui-glass-strong"),
};
let glass = format!("{base}--glass");
Some(compose([Some(glass.as_str()), intensity]))
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Orientation {
#[default]
Horizontal,
Vertical,
}
impl Orientation {
pub fn as_str(self) -> &'static str {
match self {
Self::Horizontal => "horizontal",
Self::Vertical => "vertical",
}
}
}
#[cfg(test)]
mod tests {
use super::{Glass, compose};
#[test]
fn compose_skips_missing_and_empty_parts() {
assert_eq!(compose([Some("a"), None, Some(""), Some("b")]), "a b");
}
#[test]
fn glass_classes_follow_lumen_react() {
assert_eq!(Glass::Off.class("ui-card"), None);
assert_eq!(
Glass::On.class("ui-card").as_deref(),
Some("ui-card--glass")
);
assert_eq!(
Glass::Strong.class("ui-card").as_deref(),
Some("ui-card--glass ui-glass-strong")
);
}
}