encre_css/plugins/transform/translate/
mod.rs

1#![doc = include_str!("README.md")]
2#![doc(alias = "transform")]
3use super::CSS_TRANSFORM;
4use crate::prelude::build_plugin::*;
5
6use std::borrow::Cow;
7
8fn translate_can_handle(context: &ContextCanHandle) -> bool {
9    match context.modifier {
10        Modifier::Builtin { value, .. } => {
11            spacing::is_matching_builtin_spacing(value) || *value == "auto" || *value == "full"
12        }
13        Modifier::Arbitrary { value, .. } => {
14            is_matching_length(value) || is_matching_percentage(value)
15        }
16    }
17}
18
19fn translate_handle(css_prop: &str, context: &mut ContextHandle) {
20    match context.modifier {
21        Modifier::Builtin { is_negative, value } => context.buffer.line(format_args!(
22            "{}: {};",
23            css_prop,
24            if *value == "auto" {
25                Cow::from("auto")
26            } else if *value == "full" && *is_negative {
27                Cow::from("-100%")
28            } else if *value == "full" {
29                Cow::from("100%")
30            } else {
31                spacing::get(value, *is_negative).unwrap()
32            },
33        )),
34        Modifier::Arbitrary { value, .. } => {
35            context.buffer.line(format_args!("{css_prop}: {value};"));
36        }
37    }
38
39    context.buffer.line(CSS_TRANSFORM);
40}
41
42#[derive(Debug)]
43pub(crate) struct PluginXDefinition;
44
45impl Plugin for PluginXDefinition {
46    fn can_handle(&self, context: ContextCanHandle) -> bool {
47        translate_can_handle(&context)
48    }
49
50    fn handle(&self, context: &mut ContextHandle) {
51        translate_handle("--en-translate-x", context);
52    }
53}
54
55#[derive(Debug)]
56pub(crate) struct PluginYDefinition;
57
58impl Plugin for PluginYDefinition {
59    fn can_handle(&self, context: ContextCanHandle) -> bool {
60        translate_can_handle(&context)
61    }
62
63    fn handle(&self, context: &mut ContextHandle) {
64        translate_handle("--en-translate-y", context);
65    }
66}
67
68#[derive(Debug)]
69pub(crate) struct PluginZDefinition;
70
71impl Plugin for PluginZDefinition {
72    fn can_handle(&self, context: ContextCanHandle) -> bool {
73        translate_can_handle(&context)
74    }
75
76    fn handle(&self, context: &mut ContextHandle) {
77        translate_handle("--en-translate-z", context);
78    }
79}