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
use crate::rewriter::AsciiCompatibleEncoding;
use encoding_rs::Encoding;
use std::cell::Cell;
use std::ops::Deref;
use std::rc::Rc;

/// A charset encoding that can be shared and modified.
///
/// This is, for instance, used to adapt the charset dynamically in a [crate::HtmlRewriter] if it
/// encounters a `meta` tag that specifies the charset (that behavior is dependent on
/// [crate::Settings::adjust_charset_on_meta_tag]).
#[derive(Clone)]
pub struct SharedEncoding {
    encoding: Rc<Cell<AsciiCompatibleEncoding>>,
}

impl SharedEncoding {
    pub fn new(encoding: AsciiCompatibleEncoding) -> SharedEncoding {
        SharedEncoding {
            encoding: Rc::new(Cell::new(encoding)),
        }
    }

    pub fn get(&self) -> &'static Encoding {
        self.encoding.get().into()
    }

    pub fn set(&self, encoding: AsciiCompatibleEncoding) {
        self.encoding.set(encoding);
    }
}

impl Deref for SharedEncoding {
    type Target = Encoding;

    fn deref(&self) -> &'static Encoding {
        self.get()
    }
}