encre_css/plugins/spacing/margin/
mod.rs

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