1
//! CSS properties for flowing content into regions (`flow-into`, `flow-from`).
2

            
3
use alloc::string::{String, ToString};
4

            
5
use crate::{corety::AzString, props::formatter::PrintAsCssValue};
6

            
7
// --- flow-into ---
8
#[allow(variant_size_differences)]
9
// repr(C,u8) FFI enum: boxing the large variant would change the C ABI (api.json bindings); size
10
// disparity accepted
11
/// CSS `flow-into` property — diverts an element's content into a named flow.
12
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
13
#[repr(C, u8)]
14
#[derive(Default)]
15
pub enum FlowInto {
16
    /// Content is not diverted into any named flow (default).
17
    #[default]
18
    None,
19
    /// Content is diverted into the named flow identified by this string.
20
    Named(AzString),
21
}
22

            
23
impl PrintAsCssValue for FlowInto {
24
    fn print_as_css_value(&self) -> String {
25
        match self {
26
            Self::None => "none".to_string(),
27
            Self::Named(s) => s.to_string(),
28
        }
29
    }
30
}
31

            
32
// --- flow-from ---
33
#[allow(variant_size_differences)]
34
// repr(C,u8) FFI enum: boxing the large variant would change the C ABI (api.json bindings); size
35
// disparity accepted
36
/// CSS `flow-from` property — consumes content from a named flow into a region.
37
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
38
#[repr(C, u8)]
39
#[derive(Default)]
40
pub enum FlowFrom {
41
    /// No named flow is consumed (default).
42
    #[default]
43
    None,
44
    /// Content is consumed from the named flow identified by this string.
45
    Named(AzString),
46
}
47

            
48
impl PrintAsCssValue for FlowFrom {
49
    fn print_as_css_value(&self) -> String {
50
        match self {
51
            Self::None => "none".to_string(),
52
            Self::Named(s) => s.to_string(),
53
        }
54
    }
55
}
56

            
57
// Formatting to Rust code
58
impl crate::codegen::format::FormatAsRustCode for FlowInto {
59
    fn format_as_rust_code(&self, _tabs: usize) -> String {
60
        match self {
61
            Self::None => String::from("FlowInto::None"),
62
            Self::Named(s) => format!(
63
                "FlowInto::Named(AzString::from_const_str({:?}))",
64
                s.as_str()
65
            ),
66
        }
67
    }
68
}
69

            
70
impl crate::codegen::format::FormatAsRustCode for FlowFrom {
71
    fn format_as_rust_code(&self, _tabs: usize) -> String {
72
        match self {
73
            Self::None => String::from("FlowFrom::None"),
74
            Self::Named(s) => format!(
75
                "FlowFrom::Named(AzString::from_const_str({:?}))",
76
                s.as_str()
77
            ),
78
        }
79
    }
80
}
81

            
82
// --- PARSERS ---
83

            
84
#[cfg(feature = "parser")]
85
pub mod parser {
86
    #[allow(clippy::wildcard_imports)]
87
    // parser submodule reuses the parent module's value types
88
    use super::*;
89
    use crate::corety::AzString;
90

            
91
    macro_rules! define_flow_parser {
92
        (
93
            $fn_name:ident,
94
            $struct_name:ident,
95
            $error_name:ident,
96
            $error_owned_name:ident,
97
            $prop_name:expr
98
        ) => {
99
            #[derive(Clone, PartialEq, Eq)]
100
            pub enum $error_name<'a> {
101
                InvalidValue(&'a str),
102
            }
103

            
104
            impl_debug_as_display!($error_name<'a>);
105
            impl_display! { $error_name<'a>, {
106
                InvalidValue(v) => format!("Invalid {} value: \"{}\"", $prop_name, v),
107
            }}
108

            
109
            #[derive(Debug, Clone, PartialEq, Eq)]
110
            #[repr(C, u8)]
111
            pub enum $error_owned_name {
112
                InvalidValue(AzString),
113
            }
114

            
115
            impl $error_name<'_> {
116
                #[must_use]
117
                pub fn to_contained(&self) -> $error_owned_name {
118
                    match self {
119
                        Self::InvalidValue(s) => {
120
                            $error_owned_name::InvalidValue(s.to_string().into())
121
                        }
122
                    }
123
                }
124
            }
125

            
126
            impl $error_owned_name {
127
                #[must_use]
128
                pub fn to_shared(&self) -> $error_name<'_> {
129
                    match self {
130
                        Self::InvalidValue(s) => $error_name::InvalidValue(s.as_str()),
131
                    }
132
                }
133
            }
134

            
135
            /// # Errors
136
            ///
137
            /// Returns an error if `input` is not a valid CSS value for this property.
138
6
            pub fn $fn_name(input: &str) -> Result<$struct_name, $error_name<'_>> {
139
6
                let trimmed = input.trim();
140
6
                if trimmed.is_empty() {
141
2
                    return Err($error_name::InvalidValue(input));
142
4
                }
143
4
                match trimmed {
144
4
                    "none" => Ok($struct_name::None),
145
                    // any other value is a custom identifier
146
2
                    ident => Ok($struct_name::Named(ident.to_string().into())),
147
                }
148
6
            }
149
        };
150
    }
151

            
152
    define_flow_parser!(
153
        parse_flow_into,
154
        FlowInto,
155
        FlowIntoParseError,
156
        FlowIntoParseErrorOwned,
157
        "flow-into"
158
    );
159
    define_flow_parser!(
160
        parse_flow_from,
161
        FlowFrom,
162
        FlowFromParseError,
163
        FlowFromParseErrorOwned,
164
        "flow-from"
165
    );
166
}
167

            
168
#[cfg(feature = "parser")]
169
pub use parser::*;
170

            
171
#[cfg(all(test, feature = "parser"))]
172
mod tests {
173
    use super::*;
174

            
175
    #[test]
176
1
    fn test_parse_flow_into() {
177
1
        assert_eq!(parse_flow_into("none").unwrap(), FlowInto::None);
178
1
        assert_eq!(
179
1
            parse_flow_into("my-article-flow").unwrap(),
180
1
            FlowInto::Named("my-article-flow".into())
181
        );
182
1
        assert!(parse_flow_into("").is_err());
183
1
    }
184

            
185
    #[test]
186
1
    fn test_parse_flow_from() {
187
1
        assert_eq!(parse_flow_from("none").unwrap(), FlowFrom::None);
188
1
        assert_eq!(
189
1
            parse_flow_from("  main-thread  ").unwrap(),
190
1
            FlowFrom::Named("main-thread".into())
191
        );
192
1
        assert!(parse_flow_from("").is_err());
193
1
    }
194
}