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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use bc_components::tags::GLOBAL_TAGS;
use dcbor::prelude::*;
use std::sync::{Once, Mutex};
#[cfg(feature = "known_value")]
use crate::extension::known_values::{KnownValuesStore, KNOWN_VALUES};

#[cfg(feature = "expression")]
use crate::extension::expression::{FunctionsStore, ParametersStore, GLOBAL_FUNCTIONS, GLOBAL_PARAMETERS};

/// The envelope formatting functions take a `FormatContext` as an argument. This type
/// defines information about CBOR tags, known values, functions and parameters that
/// are used to annotate the output of the formatting functions.
///
/// The `with_format_context!` macro can be used to access the global format context:
///
/// ```
/// # use bc_envelope::{Envelope, with_format_context};
/// # use indoc::indoc;
/// # let e = Envelope::new("Hello.");
/// with_format_context!(|context| {
///     assert_eq!(e.diagnostic_opt(true, Some(context)),
///     indoc! {r#"
///     200(   / envelope /
///        201("Hello.")   / leaf /
///     )
///     "#}.trim()
///     );
/// });
/// ```
#[derive(Clone, Debug)]
pub struct FormatContext {
    tags: TagsStore,
    #[cfg(feature = "known_value")]
    known_values: KnownValuesStore,
    #[cfg(feature = "expression")]
    functions: FunctionsStore,
    #[cfg(feature = "expression")]
    parameters: ParametersStore,
}

impl FormatContext {
    pub fn new(
        tags: Option<&TagsStore>,
        #[cfg(feature = "known_value")]
        known_values: Option<&KnownValuesStore>,
        #[cfg(feature = "expression")]
        functions: Option<&FunctionsStore>,
        #[cfg(feature = "expression")]
        parameters: Option<&ParametersStore>,
    ) -> Self {
        Self {
            tags: tags.cloned().unwrap_or_default(),
            #[cfg(feature = "known_value")]
            known_values: known_values.cloned().unwrap_or_default(),
            #[cfg(feature = "expression")]
            functions: functions.cloned().unwrap_or_default(),
            #[cfg(feature = "expression")]
            parameters: parameters.cloned().unwrap_or_default(),
        }
    }

    pub fn tags(&self) -> &TagsStore {
        &self.tags
    }

    pub fn assigned_name_for_tag(&self, tag: &Tag) -> Option<String> {
        self.tags.assigned_name_for_tag(tag)
    }

    pub fn name_for_tag(&self, tag: &Tag) -> String {
        self.tags.name_for_tag(tag)
    }

    pub fn tag_for_value(&self, value: u64) -> Option<Tag> {
        self.tags.tag_for_value(value)
    }

    pub fn tag_for_name(&self, name: &str) -> Option<Tag> {
        self.tags.tag_for_name(name)
    }

    #[cfg(feature = "known_value")]
    pub fn known_values(&self) -> &KnownValuesStore {
        &self.known_values
    }

    #[cfg(feature = "expression")]
    pub fn functions(&self) -> &FunctionsStore {
        &self.functions
    }

    #[cfg(feature = "expression")]
    pub fn parameters(&self) -> &ParametersStore {
        &self.parameters
    }
}

impl Default for FormatContext {
    fn default() -> Self {
        Self::new(
            None,
            #[cfg(feature = "known_value")]
            None,
            #[cfg(feature = "expression")]
            None,
            #[cfg(feature = "expression")]
            None)
    }
}

pub struct LazyFormatContext {
    init: Once,
    data: Mutex<Option<FormatContext>>,
}

impl LazyFormatContext {
    pub fn get(&self) -> std::sync::MutexGuard<'_, Option<FormatContext>> {
        self.init.call_once(|| {
            let tags_binding = GLOBAL_TAGS.get();
            let tags = tags_binding.as_ref().unwrap();

            #[cfg(feature = "known_value")]
            let known_values_binding = KNOWN_VALUES.get();
            #[cfg(feature = "known_value")]
            let known_values = known_values_binding.as_ref().unwrap();

            #[cfg(feature = "expression")]
            let functions_binding = GLOBAL_FUNCTIONS.get();
            #[cfg(feature = "expression")]
            let functions = functions_binding.as_ref().unwrap();
            #[cfg(feature = "expression")]
            let parameters_binding = GLOBAL_PARAMETERS.get();
            #[cfg(feature = "expression")]
            let parameters = parameters_binding.as_ref().unwrap();

            let context = FormatContext::new(
                Some(tags),
                #[cfg(feature = "known_value")]
                Some(known_values),
                #[cfg(feature = "expression")]
                Some(functions),
                #[cfg(feature = "expression")]
                Some(parameters)
            );
            *self.data.lock().unwrap() = Some(context);
        });
        self.data.lock().unwrap()
    }
}

/// Access using the `with_format_context!` macro.
pub static GLOBAL_FORMAT_CONTEXT: LazyFormatContext = LazyFormatContext {
    init: Once::new(),
    data: Mutex::new(None),
};

/// A macro to access the global format context.
#[macro_export]
macro_rules! with_format_context {
    ($action:expr) => {{
        let binding = $crate::GLOBAL_FORMAT_CONTEXT.get();
        let context = &*binding.as_ref().unwrap();
        #[allow(clippy::redundant_closure_call)]
        $action(context)
    }};
}