swc_css_codegen/
ctx.rs

1use std::ops::{Deref, DerefMut};
2
3use crate::{writer::CssWriter, CodeGenerator};
4
5impl<W> CodeGenerator<W>
6where
7    W: CssWriter,
8{
9    /// Original context is restored when returned guard is dropped.
10    #[inline]
11    pub(super) fn with_ctx(&mut self, ctx: Ctx) -> WithCtx<W> {
12        let orig_ctx = self.ctx;
13        self.ctx = ctx;
14        WithCtx {
15            orig_ctx,
16            inner: self,
17        }
18    }
19}
20
21#[derive(Debug, Default, Clone, Copy)]
22pub(crate) struct Ctx {
23    pub allow_to_lowercase: bool,
24    pub is_dimension_unit: bool,
25    pub in_single_line_selectors: bool,
26    pub in_list_of_component_values: bool,
27}
28
29pub(super) struct WithCtx<'w, I: 'w + CssWriter> {
30    inner: &'w mut CodeGenerator<I>,
31    orig_ctx: Ctx,
32}
33
34impl<I: CssWriter> Deref for WithCtx<'_, I> {
35    type Target = CodeGenerator<I>;
36
37    fn deref(&self) -> &CodeGenerator<I> {
38        self.inner
39    }
40}
41impl<I: CssWriter> DerefMut for WithCtx<'_, I> {
42    fn deref_mut(&mut self) -> &mut CodeGenerator<I> {
43        self.inner
44    }
45}
46
47impl<I: CssWriter> Drop for WithCtx<'_, I> {
48    fn drop(&mut self) {
49        self.inner.ctx = self.orig_ctx;
50    }
51}