bc_envelope/base/
format_context.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#[cfg(feature = "expression")]
use bc_components::tags::*;
use dcbor::prelude::*;
#[cfg(feature = "expression")]
use std::sync::Arc;
use std::sync::{ Mutex, Once };
#[cfg(feature = "known_value")]
use crate::extension::known_values::{ KnownValuesStore, KNOWN_VALUES };

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

#[cfg(feature = "expression")]
use crate::{ string_utils::StringUtils, Envelope };

/// 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.");
/// # bc_envelope::register_tags();
/// assert_eq!(e.diagnostic_annotated(),
/// indoc! {r#"
/// 200(   / envelope /
///     201("Hello.")   / leaf /
/// )
/// "#}.trim()
/// );
/// ```
#[derive(Clone)]
pub struct FormatContext {
    flat: bool,
    tags: TagsStore,
    #[cfg(feature = "known_value")]
    known_values: KnownValuesStore,
    #[cfg(feature = "expression")]
    functions: FunctionsStore,
    #[cfg(feature = "expression")]
    parameters: ParametersStore,
}

impl FormatContext {
    pub fn new(
        flat: bool,
        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 {
            flat,
            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 is_flat(&self) -> bool {
        self.flat
    }

    pub fn set_flat(mut self, flat: bool) -> Self {
        self.flat = flat;
        self
    }

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

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

    #[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 TagsStoreTrait for FormatContext {
    fn assigned_name_for_tag(&self, tag: &Tag) -> Option<String> {
        self.tags.assigned_name_for_tag(tag)
    }

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

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

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

    fn summarizer(&self, tag: TagValue) -> Option<&CBORSummarizer> {
        self.tags.summarizer(tag)
    }

    fn name_for_value(&self, value: u64) -> String {
        self.tags.name_for_value(value)
    }
}

impl Default for FormatContext {
    fn default() -> Self {
        Self::new(
            false,
            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(|| {
            bc_components::register_tags();
            let tags_binding = dcbor::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(
                false,
                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)
        }
    };
}

#[macro_export]
macro_rules! with_format_context_mut {
    ($action:expr) => {
        {
        let mut binding = $crate::GLOBAL_FORMAT_CONTEXT.get();
        let context = binding.as_mut().unwrap();
        #[allow(clippy::redundant_closure_call)]
        $action(context)
        }
    };
}

pub fn register_tags_in(context: &mut FormatContext) {
    bc_components::register_tags_in(context.tags_mut());

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

        let functions = context.functions().clone();
        context.tags_mut().set_summarizer(
            TAG_FUNCTION,
            Arc::new(move |untagged_cbor: CBOR| {
                let f = Function::from_untagged_cbor(untagged_cbor)?;
                Ok(FunctionsStore::name_for_function(&f, Some(&functions)).flanked_by("«", "»"))
            })
        );

        let parameters = context.parameters().clone();
        context.tags_mut().set_summarizer(
            TAG_PARAMETER,
            Arc::new(move |untagged_cbor: CBOR| {
                let p = Parameter::from_untagged_cbor(untagged_cbor)?;
                Ok(ParametersStore::name_for_parameter(&p, Some(&parameters)).flanked_by("❰", "❱"))
            })
        );

        let known_values = context.known_values().clone();
        context.tags_mut().set_summarizer(
            TAG_KNOWN_VALUE,
            Arc::new(move |untagged_cbor: CBOR| {
                Ok(
                    known_values
                        .name(KnownValue::from_untagged_cbor(untagged_cbor)?)
                        .flanked_by("'", "'")
                )
            })
        );

        let cloned_context = context.clone();
        context.tags_mut().set_summarizer(
            TAG_REQUEST,
            Arc::new(move |untagged_cbor: CBOR| {
                Ok(
                    Envelope::new(untagged_cbor)
                        .format_opt(Some(&cloned_context))
                        .flanked_by("request(", ")")
                )
            })
        );

        let cloned_context = context.clone();
        context.tags_mut().set_summarizer(
            TAG_RESPONSE,
            Arc::new(move |untagged_cbor: CBOR| {
                Ok(
                    Envelope::new(untagged_cbor)
                        .format_opt(Some(&cloned_context))
                        .flanked_by("response(", ")")
                )
            })
        );

        let cloned_context = context.clone();
        context.tags_mut().set_summarizer(
            TAG_EVENT,
            Arc::new(move |untagged_cbor: CBOR| {
                Ok(
                    Envelope::new(untagged_cbor)
                        .format_opt(Some(&cloned_context))
                        .flanked_by("event(", ")")
                )
            })
        );
    }
}

pub fn register_tags() {
    with_format_context_mut!(|context: &mut FormatContext| {
        register_tags_in(context);
    });
}