textiler_core/
style.rs

1//! Controls the general style and approach of components
2
3use serde::{Deserialize, Serialize};
4use std::env::var;
5use std::fmt::{Display, Formatter};
6use strum::{AsRefStr, EnumIter, IntoEnumIterator};
7use yew::html::{ImplicitClone, IntoPropValue};
8
9/// The variant of this component to use
10#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, AsRefStr, EnumIter)]
11pub enum Variant {
12    /// Plain theme
13    #[default]
14    Plain,
15    /// Outlined theme
16    Outlined,
17    /// A softer theme
18    Soft,
19    /// A solid theme
20    Solid,
21}
22
23impl ImplicitClone for Variant {}
24
25impl Display for Variant {
26    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
27        write!(f, "{}", self.as_ref().to_lowercase())
28    }
29}
30
31/// The main color scheme of this component to use
32#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, AsRefStr, EnumIter)]
33pub enum Color {
34    #[default]
35    Neutral,
36    Primary,
37    Success,
38    Fatal,
39    Warn,
40}
41
42impl ImplicitClone for Color {}
43impl Display for Color {
44    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
45        write!(f, "{}", self.as_ref().to_lowercase())
46    }
47}
48
49/// General size descriptions
50#[derive(
51    Debug, Clone, Copy, Default, PartialEq, Eq, Hash, AsRefStr, EnumIter, Serialize, Deserialize,
52)]
53#[serde(rename_all = "lowercase")]
54pub enum Size {
55    Xs,
56    Sm,
57    #[default]
58    Md,
59    Lg,
60    Xl,
61}
62
63impl ImplicitClone for Size {}
64
65impl IntoPropValue<Size> for &str {
66    fn into_prop_value(self) -> Size {
67        Size::try_from(self).unwrap_or_else(|_| panic!("{self:?} is not a known color"))
68    }
69}
70
71impl TryFrom<&str> for Size {
72    type Error = UnknownSize;
73
74    fn try_from(value: &str) -> Result<Self, Self::Error> {
75        for size in Size::iter() {
76            if size.as_ref().to_lowercase() == value {
77                return Ok(size);
78            }
79        }
80        Err(UnknownSize(value.to_string()))
81    }
82}
83
84#[derive(Debug, thiserror::Error)]
85#[error("No known size {0:?}")]
86pub struct UnknownSize(pub String);
87
88impl Display for Size {
89    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
90        write!(f, "{}", self.as_ref().to_lowercase())
91    }
92}