swc_css_minifier/compressor/
ctx.rs

1use std::ops::{Deref, DerefMut};
2
3use super::Compressor;
4
5#[derive(Clone, Copy)]
6pub(super) struct Ctx {
7    pub in_math_function: bool,
8
9    pub in_logic_combinator_selector: bool,
10
11    pub in_transform_function: bool,
12
13    pub in_keyframe_block: bool,
14
15    pub preserve_alpha_value: bool,
16}
17impl Default for Ctx {
18    fn default() -> Self {
19        Self {
20            preserve_alpha_value: true,
21            in_math_function: false,
22            in_logic_combinator_selector: false,
23            in_transform_function: false,
24            in_keyframe_block: false,
25        }
26    }
27}
28
29impl Compressor {
30    /// RAII guard to change context temporarically
31    pub(super) fn with_ctx(&mut self, ctx: Ctx) -> WithCtx<'_> {
32        let orig_ctx = self.ctx;
33        self.ctx = ctx;
34        WithCtx {
35            pass: self,
36            orig_ctx,
37        }
38    }
39}
40
41pub(super) struct WithCtx<'a> {
42    pass: &'a mut Compressor,
43    orig_ctx: Ctx,
44}
45
46impl Deref for WithCtx<'_> {
47    type Target = Compressor;
48
49    fn deref(&self) -> &Self::Target {
50        self.pass
51    }
52}
53
54impl DerefMut for WithCtx<'_> {
55    fn deref_mut(&mut self) -> &mut Self::Target {
56        self.pass
57    }
58}
59
60impl Drop for WithCtx<'_> {
61    fn drop(&mut self) {
62        self.pass.ctx = self.orig_ctx;
63    }
64}