error_stack/fmt/
config.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
65
66
67
use crate::fmt::{Charset, ColorMode};
#[cfg(any(feature = "std", feature = "hooks"))]
use crate::fmt::{Format, HookContext};

#[cfg(any(feature = "std", feature = "hooks"))]
pub(crate) struct Config {
    context: HookContext<()>,
}

#[cfg(any(feature = "std", feature = "hooks"))]
impl Config {
    pub(crate) fn new(color_mode: ColorMode, charset: Charset, alternate: bool) -> Self {
        let context = HookContext::new(Format::new(alternate, color_mode, charset));

        Self { context }
    }

    pub(crate) fn load(alternate: bool) -> Self {
        let color_mode = ColorMode::load();
        let charset = Charset::load();

        Self::new(color_mode, charset, alternate)
    }

    pub(crate) fn context<T>(&mut self) -> &mut HookContext<T> {
        self.context.cast()
    }

    pub(crate) const fn color_mode(&self) -> ColorMode {
        self.context.color_mode()
    }

    pub(crate) const fn charset(&self) -> Charset {
        self.context.charset()
    }
}

#[cfg(not(any(feature = "std", feature = "hooks")))]
pub(crate) struct Config {
    color_mode: ColorMode,
    charset: Charset,
}

#[cfg(not(any(feature = "std", feature = "hooks")))]
impl Config {
    pub(crate) const fn new(color_mode: ColorMode, charset: Charset, _alternate: bool) -> Self {
        Self {
            color_mode,
            charset,
        }
    }

    pub(crate) fn load(alternate: bool) -> Self {
        let color_mode = ColorMode::load();
        let charset = Charset::load();

        Self::new(color_mode, charset, alternate)
    }

    pub(crate) const fn color_mode(&self) -> ColorMode {
        self.color_mode
    }

    pub(crate) const fn charset(&self) -> Charset {
        self.charset
    }
}