encre_css/plugins/transform/scale/
mod.rs

1#![doc = include_str!("README.md")]
2#![doc(alias = "transform")]
3use super::CSS_TRANSFORM;
4use crate::prelude::build_plugin::*;
5
6fn scale_can_handle(context: &ContextCanHandle) -> bool {
7    match context.modifier {
8        Modifier::Builtin { value, .. } => value.parse::<usize>().is_ok(),
9        Modifier::Arbitrary { value, .. } => {
10            is_matching_percentage(value) || is_matching_number(value)
11        }
12    }
13}
14
15fn scale_handle(css_properties: &[&str], context: &mut ContextHandle) {
16    match context.modifier {
17        Modifier::Builtin { is_negative, value } => {
18            for css_prop in css_properties {
19                #[allow(clippy::cast_precision_loss)]
20                context.buffer.line(format_args!(
21                    "{}: {}{};",
22                    css_prop,
23                    format_negative(is_negative),
24                    value.parse::<usize>().unwrap() as f32 / 100.,
25                ));
26            }
27        }
28        Modifier::Arbitrary { value, .. } => {
29            for css_prop in css_properties {
30                context
31                    .buffer
32                    .line(format_args!("{css_prop}: {value};"));
33            }
34        }
35    }
36
37    context.buffer.line(CSS_TRANSFORM);
38}
39
40#[derive(Debug)]
41pub(crate) struct PluginDefinition;
42
43impl Plugin for PluginDefinition {
44    fn can_handle(&self, context: ContextCanHandle) -> bool {
45        scale_can_handle(&context)
46    }
47
48    fn handle(&self, context: &mut ContextHandle) {
49        scale_handle(&["--en-scale-x", "--en-scale-y"], context);
50    }
51}
52
53#[derive(Debug)]
54pub(crate) struct PluginXDefinition;
55
56impl Plugin for PluginXDefinition {
57    fn can_handle(&self, context: ContextCanHandle) -> bool {
58        scale_can_handle(&context)
59    }
60
61    fn handle(&self, context: &mut ContextHandle) {
62        scale_handle(&["--en-scale-x"], context);
63    }
64}
65
66#[derive(Debug)]
67pub(crate) struct PluginYDefinition;
68
69impl Plugin for PluginYDefinition {
70    fn can_handle(&self, context: ContextCanHandle) -> bool {
71        scale_can_handle(&context)
72    }
73
74    fn handle(&self, context: &mut ContextHandle) {
75        scale_handle(&["--en-scale-y"], context);
76    }
77}
78
79#[derive(Debug)]
80pub(crate) struct PluginZDefinition;
81
82impl Plugin for PluginZDefinition {
83    fn can_handle(&self, context: ContextCanHandle) -> bool {
84        scale_can_handle(&context)
85    }
86
87    fn handle(&self, context: &mut ContextHandle) {
88        scale_handle(&["--en-scale-z"], context);
89    }
90}