Skip to main content

ferroui_base/media/
font_style.rs

1use std::fmt;
2use std::str::FromStr;
3
4use crate::utilities::FormatError;
5
6/// Defines the available font styles.
7#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
8#[repr(i32)]
9pub enum FontStyle {
10    /// A normal font.
11    #[default]
12    Normal = 0,
13    /// An italic font.
14    Italic = 1,
15    /// An oblique font.
16    Oblique = 2,
17}
18
19impl FontStyle {
20    const NAMES: [(&'static str, FontStyle); 3] =
21        [("Normal", FontStyle::Normal), ("Italic", FontStyle::Italic), ("Oblique", FontStyle::Oblique)];
22
23    /// Parses a style name, ignoring case (C# `Enum.TryParse(token, true, ..)`).
24    pub fn try_parse_ignore_case(s: &str) -> Option<Self> {
25        Self::NAMES.iter().find(|(name, _)| name.eq_ignore_ascii_case(s)).map(|(_, value)| *value)
26    }
27}
28
29impl fmt::Display for FontStyle {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        fmt::Debug::fmt(self, f)
32    }
33}
34
35impl FromStr for FontStyle {
36    type Err = FormatError;
37
38    fn from_str(s: &str) -> Result<Self, Self::Err> {
39        Self::try_parse_ignore_case(s.trim())
40            .ok_or_else(|| FormatError::from_string(format!("Invalid font style: '{s}'.")))
41    }
42}