encre_css/plugins/border/border_radius/
mod.rs

1#![doc = include_str!("README.md")]
2#![doc(alias("border", "rounded"))]
3use crate::prelude::build_plugin::*;
4
5fn radius_can_handle(context: &ContextCanHandle) -> bool {
6    match context.modifier {
7        Modifier::Builtin { value, .. } => {
8            ["xs", "sm", "md", "lg", "xl", "2xl", "3xl", "full", "none"].contains(&&**value)
9        }
10        Modifier::Arbitrary { value, prefix, .. } => {
11            prefix.is_empty()
12                && value
13                    .split(' ')
14                    .all(|v| is_matching_length(v) || is_matching_percentage(v))
15        }
16    }
17}
18
19fn radius_handle(css_properties: &[&str], context: &mut ContextHandle) {
20    match context.modifier {
21        Modifier::Builtin { value, .. } => {
22            for css_prop in css_properties {
23                context.buffer.line(format_args!(
24                    "{}: {};",
25                    css_prop,
26                    match *value {
27                        "none" => "0",
28                        "xs" => "0.125rem",
29                        "sm" => "0.25rem",
30                        "md" => "0.375rem",
31                        "lg" => "0.5rem",
32                        "xl" => "0.75rem",
33                        "2xl" => "1rem",
34                        "3xl" => "1.5rem",
35                        "full" => "9999px",
36                        _ => unreachable!(),
37                    }
38                ));
39            }
40        }
41        Modifier::Arbitrary { value, .. } => {
42            for css_prop in css_properties {
43                context.buffer.line(format_args!("{css_prop}: {value};"));
44            }
45        }
46    }
47}
48
49#[derive(Debug)]
50pub(crate) struct PluginDefinition;
51
52impl Plugin for PluginDefinition {
53    fn can_handle(&self, context: ContextCanHandle) -> bool {
54        radius_can_handle(&context)
55    }
56
57    fn handle(&self, context: &mut ContextHandle) {
58        radius_handle(&["border-radius"], context);
59    }
60}
61
62#[derive(Debug)]
63pub(crate) struct PluginStartDefinition;
64
65impl Plugin for PluginStartDefinition {
66    fn can_handle(&self, context: ContextCanHandle) -> bool {
67        radius_can_handle(&context)
68    }
69
70    fn handle(&self, context: &mut ContextHandle) {
71        radius_handle(
72            &["border-start-start-radius", "border-end-start-radius"],
73            context,
74        );
75    }
76}
77
78#[derive(Debug)]
79pub(crate) struct PluginEndDefinition;
80
81impl Plugin for PluginEndDefinition {
82    fn can_handle(&self, context: ContextCanHandle) -> bool {
83        radius_can_handle(&context)
84    }
85
86    fn handle(&self, context: &mut ContextHandle) {
87        radius_handle(
88            &["border-start-end-radius", "border-end-end-radius"],
89            context,
90        );
91    }
92}
93
94#[derive(Debug)]
95pub(crate) struct PluginStartStartDefinition;
96
97impl Plugin for PluginStartStartDefinition {
98    fn can_handle(&self, context: ContextCanHandle) -> bool {
99        radius_can_handle(&context)
100    }
101
102    fn handle(&self, context: &mut ContextHandle) {
103        radius_handle(&["border-start-start-radius"], context);
104    }
105}
106
107#[derive(Debug)]
108pub(crate) struct PluginStartEndDefinition;
109
110impl Plugin for PluginStartEndDefinition {
111    fn can_handle(&self, context: ContextCanHandle) -> bool {
112        radius_can_handle(&context)
113    }
114
115    fn handle(&self, context: &mut ContextHandle) {
116        radius_handle(&["border-start-end-radius"], context);
117    }
118}
119
120#[derive(Debug)]
121pub(crate) struct PluginEndEndDefinition;
122
123impl Plugin for PluginEndEndDefinition {
124    fn can_handle(&self, context: ContextCanHandle) -> bool {
125        radius_can_handle(&context)
126    }
127
128    fn handle(&self, context: &mut ContextHandle) {
129        radius_handle(&["border-end-end-radius"], context);
130    }
131}
132
133#[derive(Debug)]
134pub(crate) struct PluginEndStartDefinition;
135
136impl Plugin for PluginEndStartDefinition {
137    fn can_handle(&self, context: ContextCanHandle) -> bool {
138        radius_can_handle(&context)
139    }
140
141    fn handle(&self, context: &mut ContextHandle) {
142        radius_handle(&["border-end-start-radius"], context);
143    }
144}
145
146#[derive(Debug)]
147pub(crate) struct PluginTopRightDefinition;
148
149impl Plugin for PluginTopRightDefinition {
150    fn can_handle(&self, context: ContextCanHandle) -> bool {
151        radius_can_handle(&context)
152    }
153
154    fn handle(&self, context: &mut ContextHandle) {
155        radius_handle(&["border-top-right-radius"], context);
156    }
157}
158
159#[derive(Debug)]
160pub(crate) struct PluginTopLeftDefinition;
161
162impl Plugin for PluginTopLeftDefinition {
163    fn can_handle(&self, context: ContextCanHandle) -> bool {
164        radius_can_handle(&context)
165    }
166
167    fn handle(&self, context: &mut ContextHandle) {
168        radius_handle(&["border-top-left-radius"], context);
169    }
170}
171
172#[derive(Debug)]
173pub(crate) struct PluginBottomRightDefinition;
174
175impl Plugin for PluginBottomRightDefinition {
176    fn can_handle(&self, context: ContextCanHandle) -> bool {
177        radius_can_handle(&context)
178    }
179
180    fn handle(&self, context: &mut ContextHandle) {
181        radius_handle(&["border-bottom-right-radius"], context);
182    }
183}
184
185#[derive(Debug)]
186pub(crate) struct PluginBottomLeftDefinition;
187
188impl Plugin for PluginBottomLeftDefinition {
189    fn can_handle(&self, context: ContextCanHandle) -> bool {
190        radius_can_handle(&context)
191    }
192
193    fn handle(&self, context: &mut ContextHandle) {
194        radius_handle(&["border-bottom-left-radius"], context);
195    }
196}
197
198#[derive(Debug)]
199pub(crate) struct PluginTopDefinition;
200
201impl Plugin for PluginTopDefinition {
202    fn can_handle(&self, context: ContextCanHandle) -> bool {
203        radius_can_handle(&context)
204    }
205
206    fn handle(&self, context: &mut ContextHandle) {
207        radius_handle(
208            &["border-top-left-radius", "border-top-right-radius"],
209            context,
210        );
211    }
212}
213
214#[derive(Debug)]
215pub(crate) struct PluginBottomDefinition;
216
217impl Plugin for PluginBottomDefinition {
218    fn can_handle(&self, context: ContextCanHandle) -> bool {
219        radius_can_handle(&context)
220    }
221
222    fn handle(&self, context: &mut ContextHandle) {
223        radius_handle(
224            &["border-bottom-left-radius", "border-bottom-right-radius"],
225            context,
226        );
227    }
228}
229
230#[derive(Debug)]
231pub(crate) struct PluginLeftDefinition;
232
233impl Plugin for PluginLeftDefinition {
234    fn can_handle(&self, context: ContextCanHandle) -> bool {
235        radius_can_handle(&context)
236    }
237
238    fn handle(&self, context: &mut ContextHandle) {
239        radius_handle(
240            &["border-top-left-radius", "border-bottom-left-radius"],
241            context,
242        );
243    }
244}
245
246#[derive(Debug)]
247pub(crate) struct PluginRightDefinition;
248
249impl Plugin for PluginRightDefinition {
250    fn can_handle(&self, context: ContextCanHandle) -> bool {
251        radius_can_handle(&context)
252    }
253
254    fn handle(&self, context: &mut ContextHandle) {
255        radius_handle(
256            &["border-top-right-radius", "border-bottom-right-radius"],
257            context,
258        );
259    }
260}