Skip to main content

azul_css/props/layout/
text.rs

1//! CSS property for text-justify
2
3use alloc::string::{String, ToString};
4use core::fmt;
5
6use crate::{format_rust_code::FormatAsRustCode, props::formatter::PrintAsCssValue};
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
9#[repr(C)]
10pub enum LayoutTextJustify {
11    Auto,
12    None,
13    InterWord,
14    InterCharacter,
15    Distribute,
16}
17
18impl Default for LayoutTextJustify {
19    fn default() -> Self {
20        LayoutTextJustify::Auto
21    }
22}
23
24impl PrintAsCssValue for LayoutTextJustify {
25    fn print_as_css_value(&self) -> String {
26        match self {
27            LayoutTextJustify::Auto => "auto",
28            LayoutTextJustify::None => "none",
29            LayoutTextJustify::InterWord => "inter-word",
30            LayoutTextJustify::InterCharacter => "inter-character",
31            LayoutTextJustify::Distribute => "distribute",
32        }
33        .to_string()
34    }
35}
36
37impl FormatAsRustCode for LayoutTextJustify {
38    fn format_as_rust_code(&self, tabs: usize) -> String {
39        format!("LayoutTextJustify::{self:?}")
40    }
41}
42
43#[derive(Debug, Clone, PartialEq)]
44pub enum TextJustifyParseError<'a> {
45    InvalidValue(&'a str),
46}
47
48impl<'a> fmt::Display for TextJustifyParseError<'a> {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        match self {
51            TextJustifyParseError::InvalidValue(s) => {
52                write!(f, "Invalid text-justify value: '{}'.", s)
53            }
54        }
55    }
56}
57
58#[derive(Debug, Clone, PartialEq)]
59pub enum TextJustifyParseErrorOwned {
60    InvalidValue(String),
61}
62
63impl<'a> TextJustifyParseError<'a> {
64    pub fn to_owned(&self) -> TextJustifyParseErrorOwned {
65        match self {
66            TextJustifyParseError::InvalidValue(s) => {
67                TextJustifyParseErrorOwned::InvalidValue((*s).to_string())
68            }
69        }
70    }
71}
72
73impl TextJustifyParseErrorOwned {
74    pub fn to_borrowed(&self) -> TextJustifyParseError {
75        match self {
76            TextJustifyParseErrorOwned::InvalidValue(s) => {
77                TextJustifyParseError::InvalidValue(s.as_str())
78            }
79        }
80    }
81}
82
83pub fn parse_layout_text_justify<'a>(
84    input: &'a str,
85) -> Result<LayoutTextJustify, TextJustifyParseError<'a>> {
86    match input.trim() {
87        "auto" => Ok(LayoutTextJustify::Auto),
88        "none" => Ok(LayoutTextJustify::None),
89        "inter-word" => Ok(LayoutTextJustify::InterWord),
90        "inter-character" => Ok(LayoutTextJustify::InterCharacter),
91        "distribute" => Ok(LayoutTextJustify::Distribute),
92        other => Err(TextJustifyParseError::InvalidValue(other)),
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99    #[test]
100    fn test_parse_layout_text_justify() {
101        assert_eq!(
102            parse_layout_text_justify("auto"),
103            Ok(LayoutTextJustify::Auto)
104        );
105        assert_eq!(
106            parse_layout_text_justify("none"),
107            Ok(LayoutTextJustify::None)
108        );
109        assert_eq!(
110            parse_layout_text_justify("inter-word"),
111            Ok(LayoutTextJustify::InterWord)
112        );
113        assert_eq!(
114            parse_layout_text_justify("inter-character"),
115            Ok(LayoutTextJustify::InterCharacter)
116        );
117        assert_eq!(
118            parse_layout_text_justify("distribute"),
119            Ok(LayoutTextJustify::Distribute)
120        );
121        assert!(parse_layout_text_justify("invalid").is_err());
122    }
123}