encre_css/plugins/border/border_width/
mod.rs

1#![doc = include_str!("README.md")]
2#![doc(alias = "border")]
3use crate::prelude::build_plugin::*;
4
5fn width_can_handle(context: &ContextCanHandle) -> bool {
6    match context.modifier {
7        Modifier::Builtin { value, .. } => value.is_empty() || value.parse::<usize>().is_ok(),
8        Modifier::Arbitrary {
9            hint,
10            value,
11            prefix,
12        } => {
13            prefix.is_empty()
14                && (*hint == "length"
15                    || *hint == "line-width"
16                    || (hint.is_empty()
17                        && (is_matching_length(value) || is_matching_line_width(value))))
18        }
19    }
20}
21
22fn width_handle(css_properties: &[&str], context: &mut ContextHandle) {
23    match context.modifier {
24        Modifier::Builtin { value, .. } => {
25            for css_prop in css_properties {
26                context.buffer.line(format_args!(
27                    "{}: {}px;",
28                    css_prop,
29                    if value.is_empty() { "1" } else { value }
30                ));
31            }
32        }
33        Modifier::Arbitrary { value, .. } => {
34            for css_prop in css_properties {
35                context.buffer.line(format_args!("{css_prop}: {value};"));
36            }
37        }
38    }
39}
40
41#[derive(Debug)]
42pub(crate) struct PluginDefinition;
43
44impl Plugin for PluginDefinition {
45    fn can_handle(&self, context: ContextCanHandle) -> bool {
46        width_can_handle(&context)
47    }
48
49    fn handle(&self, context: &mut ContextHandle) {
50        width_handle(&["border-width"], context);
51    }
52}
53
54#[derive(Debug)]
55pub(crate) struct PluginTopDefinition;
56
57impl Plugin for PluginTopDefinition {
58    fn can_handle(&self, context: ContextCanHandle) -> bool {
59        width_can_handle(&context)
60    }
61
62    fn handle(&self, context: &mut ContextHandle) {
63        width_handle(&["border-top-width"], context);
64    }
65}
66
67#[derive(Debug)]
68pub(crate) struct PluginBottomDefinition;
69
70impl Plugin for PluginBottomDefinition {
71    fn can_handle(&self, context: ContextCanHandle) -> bool {
72        width_can_handle(&context)
73    }
74
75    fn handle(&self, context: &mut ContextHandle) {
76        width_handle(&["border-bottom-width"], context);
77    }
78}
79
80#[derive(Debug)]
81pub(crate) struct PluginLeftDefinition;
82
83impl Plugin for PluginLeftDefinition {
84    fn can_handle(&self, context: ContextCanHandle) -> bool {
85        width_can_handle(&context)
86    }
87
88    fn handle(&self, context: &mut ContextHandle) {
89        width_handle(&["border-left-width"], context);
90    }
91}
92
93#[derive(Debug)]
94pub(crate) struct PluginRightDefinition;
95
96impl Plugin for PluginRightDefinition {
97    fn can_handle(&self, context: ContextCanHandle) -> bool {
98        width_can_handle(&context)
99    }
100
101    fn handle(&self, context: &mut ContextHandle) {
102        width_handle(&["border-right-width"], context);
103    }
104}
105
106#[derive(Debug)]
107pub(crate) struct PluginStartDefinition;
108
109impl Plugin for PluginStartDefinition {
110    fn can_handle(&self, context: ContextCanHandle) -> bool {
111        width_can_handle(&context)
112    }
113
114    fn handle(&self, context: &mut ContextHandle) {
115        width_handle(&["border-inline-start-width"], context);
116    }
117}
118
119#[derive(Debug)]
120pub(crate) struct PluginEndDefinition;
121
122impl Plugin for PluginEndDefinition {
123    fn can_handle(&self, context: ContextCanHandle) -> bool {
124        width_can_handle(&context)
125    }
126
127    fn handle(&self, context: &mut ContextHandle) {
128        width_handle(&["border-inline-end-width"], context);
129    }
130}
131
132#[derive(Debug)]
133pub(crate) struct PluginXDefinition;
134
135impl Plugin for PluginXDefinition {
136    fn can_handle(&self, context: ContextCanHandle) -> bool {
137        width_can_handle(&context)
138    }
139
140    fn handle(&self, context: &mut ContextHandle) {
141        width_handle(&["border-inline-width"], context);
142    }
143}
144
145#[derive(Debug)]
146pub(crate) struct PluginYDefinition;
147
148impl Plugin for PluginYDefinition {
149    fn can_handle(&self, context: ContextCanHandle) -> bool {
150        width_can_handle(&context)
151    }
152
153    fn handle(&self, context: &mut ContextHandle) {
154        width_handle(&["border-block-width"], context);
155    }
156}