koek_classify/
lib.rs

1// Design principles:
2// classified data gains a separate type that wraps unclassified data
3// the data class is a field on the wrapper, pointing to a constant
4// we do not define any data classes in this module, only the wiring for them
5// we process unclassified data by unwrapping it directly
6// we process classified data by using it via one of the data class traits
7
8// What would a "data classes package" provide for us?
9// list of classes
10// redaction configuration for each class
11
12use std::fmt::{self, Display, Formatter};
13
14use koek_redact::Redact;
15
16/// A data class has a name that acts as a key to allow it to be distinguished from other data classes.
17pub struct DataClass {
18    /// A key that allows data of different classes to be easily distinguished.
19    pub name: &'static str,
20
21    /// What to do when displaying a classified value
22    pub display_behavior: DisplayBehavior,
23}
24
25impl DataClass {
26    pub fn classify<TValue>(&'static self, value: TValue) -> Classified<TValue> {
27        Classified {
28            class: self,
29            value
30        }
31    }
32}
33
34pub enum DisplayBehavior {
35    /// Values of this data class are not redacted and will be displayed in the clear.
36    Clear,
37    /// Values of this data class use default redaction behavior from koek-redact.
38    DefaultRedact,
39}
40
41/// Data that has been classified.
42pub struct Classified<TValue> {
43    pub class: &'static DataClass,
44    pub value: TValue,
45}
46
47impl<TValue: Display> Display for Classified<TValue> {
48    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
49        match self.class.display_behavior {
50            DisplayBehavior::DefaultRedact => f.write_str(self.value.redacted().as_str()),
51            DisplayBehavior::Clear => self.value.fmt(f),
52        }
53    }
54}