swc_css_minifier/compressor/
ctx.rs

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
use std::ops::{Deref, DerefMut};

use super::Compressor;

#[derive(Clone, Copy)]
pub(super) struct Ctx {
    pub in_math_function: bool,

    pub in_logic_combinator_selector: bool,

    pub in_transform_function: bool,

    pub in_keyframe_block: bool,

    pub preserve_alpha_value: bool,
}
impl Default for Ctx {
    fn default() -> Self {
        Self {
            preserve_alpha_value: true,
            in_math_function: false,
            in_logic_combinator_selector: false,
            in_transform_function: false,
            in_keyframe_block: false,
        }
    }
}

impl Compressor {
    /// RAII guard to change context temporarically
    pub(super) fn with_ctx(&mut self, ctx: Ctx) -> WithCtx<'_> {
        let orig_ctx = self.ctx;
        self.ctx = ctx;
        WithCtx {
            pass: self,
            orig_ctx,
        }
    }
}

pub(super) struct WithCtx<'a> {
    pass: &'a mut Compressor,
    orig_ctx: Ctx,
}

impl Deref for WithCtx<'_> {
    type Target = Compressor;

    fn deref(&self) -> &Self::Target {
        self.pass
    }
}

impl DerefMut for WithCtx<'_> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.pass
    }
}

impl Drop for WithCtx<'_> {
    fn drop(&mut self) {
        self.pass.ctx = self.orig_ctx;
    }
}