Skip to main content

standout_render/style/
value.rs

1//! Style value types for concrete styles and aliases.
2
3use console::Style;
4
5/// A style value that can be either a concrete style or an alias to another style.
6///
7/// This enables layered styling where semantic styles can reference presentation
8/// styles, which in turn reference visual styles with concrete formatting.
9///
10/// # Example
11///
12/// ```rust
13/// use standout::{Theme, StyleValue};
14/// use console::Style;
15///
16/// let theme = Theme::new()
17///     // Visual layer - concrete styles
18///     .add("muted", Style::new().dim())
19///     .add("accent", Style::new().cyan().bold())
20///     // Presentation layer - aliases to visual
21///     .add("disabled", "muted")
22///     // Semantic layer - aliases to presentation
23///     .add("timestamp", "disabled");
24/// ```
25#[derive(Debug, Clone)]
26pub enum StyleValue {
27    /// A concrete style with actual formatting (colors, bold, etc.)
28    Concrete(Style),
29    /// An alias referencing another style by name
30    Alias(String),
31}
32
33impl From<Style> for StyleValue {
34    fn from(style: Style) -> Self {
35        StyleValue::Concrete(style)
36    }
37}
38
39impl From<&str> for StyleValue {
40    fn from(name: &str) -> Self {
41        StyleValue::Alias(name.to_string())
42    }
43}
44
45impl From<String> for StyleValue {
46    fn from(name: String) -> Self {
47        StyleValue::Alias(name)
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_style_value_from_style() {
57        let value: StyleValue = Style::new().bold().into();
58        assert!(matches!(value, StyleValue::Concrete(_)));
59    }
60
61    #[test]
62    fn test_style_value_from_str() {
63        let value: StyleValue = "target".into();
64        match value {
65            StyleValue::Alias(s) => assert_eq!(s, "target"),
66            _ => panic!("Expected Alias"),
67        }
68    }
69
70    #[test]
71    fn test_style_value_from_string() {
72        let value: StyleValue = String::from("target").into();
73        match value {
74            StyleValue::Alias(s) => assert_eq!(s, "target"),
75            _ => panic!("Expected Alias"),
76        }
77    }
78}