1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#![doc = include_str!("README.md")]
#![doc(alias = "border")]
use crate::prelude::build_plugin::*;

fn width_can_handle(context: &ContextCanHandle) -> bool {
    match context.modifier {
        Modifier::Builtin { value, .. } => value.is_empty() || value.parse::<usize>().is_ok(),
        Modifier::Arbitrary {
            hint,
            value,
            prefix,
        } => {
            prefix.is_empty()
                && (*hint == "length"
                    || *hint == "line-width"
                    || (hint.is_empty()
                        && (is_matching_length(value) || is_matching_line_width(value))))
        }
    }
}

fn width_handle(css_properties: &[&str], context: &mut ContextHandle) {
    match context.modifier {
        Modifier::Builtin { value, .. } => {
            for css_prop in css_properties {
                context.buffer.line(format_args!(
                    "{}: {}px;",
                    css_prop,
                    if value.is_empty() { "1" } else { value }
                ));
            }
        }
        Modifier::Arbitrary { value, .. } => {
            for css_prop in css_properties {
                context.buffer.line(format_args!("{css_prop}: {value};"));
            }
        }
    }
}

#[derive(Debug)]
pub(crate) struct PluginDefinition;

impl Plugin for PluginDefinition {
    fn can_handle(&self, context: ContextCanHandle) -> bool {
        width_can_handle(&context)
    }

    fn handle(&self, context: &mut ContextHandle) {
        width_handle(&["border-width"], context);
    }
}

#[derive(Debug)]
pub(crate) struct PluginTopDefinition;

impl Plugin for PluginTopDefinition {
    fn can_handle(&self, context: ContextCanHandle) -> bool {
        width_can_handle(&context)
    }

    fn handle(&self, context: &mut ContextHandle) {
        width_handle(&["border-top-width"], context);
    }
}

#[derive(Debug)]
pub(crate) struct PluginBottomDefinition;

impl Plugin for PluginBottomDefinition {
    fn can_handle(&self, context: ContextCanHandle) -> bool {
        width_can_handle(&context)
    }

    fn handle(&self, context: &mut ContextHandle) {
        width_handle(&["border-bottom-width"], context);
    }
}

#[derive(Debug)]
pub(crate) struct PluginLeftDefinition;

impl Plugin for PluginLeftDefinition {
    fn can_handle(&self, context: ContextCanHandle) -> bool {
        width_can_handle(&context)
    }

    fn handle(&self, context: &mut ContextHandle) {
        width_handle(&["border-left-width"], context);
    }
}

#[derive(Debug)]
pub(crate) struct PluginRightDefinition;

impl Plugin for PluginRightDefinition {
    fn can_handle(&self, context: ContextCanHandle) -> bool {
        width_can_handle(&context)
    }

    fn handle(&self, context: &mut ContextHandle) {
        width_handle(&["border-right-width"], context);
    }
}

#[derive(Debug)]
pub(crate) struct PluginStartDefinition;

impl Plugin for PluginStartDefinition {
    fn can_handle(&self, context: ContextCanHandle) -> bool {
        width_can_handle(&context)
    }

    fn handle(&self, context: &mut ContextHandle) {
        width_handle(&["border-inline-start-width"], context);
    }
}

#[derive(Debug)]
pub(crate) struct PluginEndDefinition;

impl Plugin for PluginEndDefinition {
    fn can_handle(&self, context: ContextCanHandle) -> bool {
        width_can_handle(&context)
    }

    fn handle(&self, context: &mut ContextHandle) {
        width_handle(&["border-inline-end-width"], context);
    }
}

#[derive(Debug)]
pub(crate) struct PluginXDefinition;

impl Plugin for PluginXDefinition {
    fn can_handle(&self, context: ContextCanHandle) -> bool {
        width_can_handle(&context)
    }

    fn handle(&self, context: &mut ContextHandle) {
        width_handle(&["border-left-width", "border-right-width"], context);
    }
}

#[derive(Debug)]
pub(crate) struct PluginYDefinition;

impl Plugin for PluginYDefinition {
    fn can_handle(&self, context: ContextCanHandle) -> bool {
        width_can_handle(&context)
    }

    fn handle(&self, context: &mut ContextHandle) {
        width_handle(&["border-top-width", "border-bottom-width"], context);
    }
}