Skip to main content

azul_layout/
fmt.rs

1// extra string functions intended for C development
2
3use std::fmt;
4
5use azul_css::{AzString, StringVec};
6
7#[derive(Debug, Clone, PartialEq, PartialOrd)]
8#[repr(C, u8)]
9pub enum FmtValue {
10    Bool(bool),
11    Uchar(u8),
12    Schar(i8),
13    Ushort(u16),
14    Sshort(i16),
15    Uint(u32),
16    Sint(i32),
17    Ulong(u64),
18    Slong(i64),
19    Isize(isize),
20    Usize(usize),
21    Float(f32),
22    Double(f64),
23    Str(AzString),
24    StrVec(StringVec),
25}
26
27impl strfmt::DisplayStr for FmtValue {
28    fn display_str(&self, f: &mut strfmt::Formatter<'_, '_>) -> strfmt::Result<()> {
29        use strfmt::DisplayStr;
30        match self {
31            FmtValue::Bool(v) => format!("{v:?}").display_str(f),
32            FmtValue::Uchar(v) => v.display_str(f),
33            FmtValue::Schar(v) => v.display_str(f),
34            FmtValue::Ushort(v) => v.display_str(f),
35            FmtValue::Sshort(v) => v.display_str(f),
36            FmtValue::Uint(v) => v.display_str(f),
37            FmtValue::Sint(v) => v.display_str(f),
38            FmtValue::Ulong(v) => v.display_str(f),
39            FmtValue::Slong(v) => v.display_str(f),
40            FmtValue::Isize(v) => v.display_str(f),
41            FmtValue::Usize(v) => v.display_str(f),
42            FmtValue::Float(v) => v.display_str(f),
43            FmtValue::Double(v) => v.display_str(f),
44            FmtValue::Str(v) => v.as_str().display_str(f),
45            FmtValue::StrVec(sv) => {
46                "[".display_str(f)?;
47                for (i, s) in sv.as_ref().iter().enumerate() {
48                    if i != 0 {
49                        ", ".display_str(f)?;
50                    }
51                    s.as_str().display_str(f)?;
52                }
53                "]".display_str(f)?;
54                Ok(())
55            }
56        }
57    }
58}
59
60impl fmt::Display for FmtValue {
61    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62        match self {
63            FmtValue::Bool(v) => v.fmt(f),
64            FmtValue::Uchar(v) => v.fmt(f),
65            FmtValue::Schar(v) => v.fmt(f),
66            FmtValue::Ushort(v) => v.fmt(f),
67            FmtValue::Sshort(v) => v.fmt(f),
68            FmtValue::Uint(v) => v.fmt(f),
69            FmtValue::Sint(v) => v.fmt(f),
70            FmtValue::Ulong(v) => v.fmt(f),
71            FmtValue::Slong(v) => v.fmt(f),
72            FmtValue::Isize(v) => v.fmt(f),
73            FmtValue::Usize(v) => v.fmt(f),
74            FmtValue::Float(v) => v.fmt(f),
75            FmtValue::Double(v) => v.fmt(f),
76            FmtValue::Str(v) => v.as_str().fmt(f),
77            FmtValue::StrVec(sv) => {
78                use std::fmt::Debug;
79                let vec: Vec<&str> = sv.as_ref().iter().map(|s| s.as_str()).collect();
80                vec.fmt(f)
81            }
82        }
83    }
84}
85
86#[derive(Debug, Clone, PartialEq, PartialOrd)]
87#[repr(C)]
88pub struct FmtArg {
89    pub key: AzString,
90    pub value: FmtValue,
91}
92
93azul_css::impl_vec!(
94    FmtArg,
95    FmtArgVec,
96    FmtArgVecDestructor,
97    FmtArgVecDestructorType
98);
99azul_css::impl_vec_clone!(FmtArg, FmtArgVec, FmtArgVecDestructor);
100azul_css::impl_vec_debug!(FmtArg, FmtArgVec);
101azul_css::impl_vec_partialeq!(FmtArg, FmtArgVec);
102azul_css::impl_vec_partialord!(FmtArg, FmtArgVec);
103
104pub fn fmt_string(format: AzString, args: FmtArgVec) -> String {
105    use strfmt::Format;
106    let format_map = args
107        .iter()
108        .map(|a| (a.key.clone().into_library_owned_string(), a.value.clone()))
109        .collect();
110    match format.as_str().format(&format_map) {
111        Ok(o) => o,
112        Err(e) => format!("{}", e),
113    }
114}