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
use std::fmt;
pub trait Context: Default + Clone + fmt::Display + fmt::Debug + Send + Sync + 'static {
fn update(self, replacements: Self) -> Self;
fn is_empty(&self) -> bool;
}
#[derive(Default, Copy, Clone, Debug)]
pub struct NoContext;
impl fmt::Display for NoContext {
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
Ok(())
}
}
impl Context for NoContext {
fn update(self, _replacements: Self) -> Self {
self
}
fn is_empty(&self) -> bool {
true
}
}
#[derive(Default, Clone, Debug)]
pub struct AdhocContext {
data: indexmap::IndexMap<&'static str, Box<dyn AdhocValue>>,
}
impl AdhocContext {
pub fn new() -> Self {
Default::default()
}
pub fn insert<V>(mut self, key: &'static str, value: V) -> Self
where
V: AdhocValue + Clone,
{
self.data.insert(key, Box::new(value));
self
}
}
impl fmt::Display for AdhocContext {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (k, v) in self.data.iter() {
writeln!(f, "{}: {}", k, v)?;
}
Ok(())
}
}
impl Context for AdhocContext {
fn update(mut self, replacements: Self) -> Self {
self.data.extend(replacements.data);
self
}
fn is_empty(&self) -> bool {
self.data.is_empty()
}
}
pub trait AdhocValue: fmt::Display + fmt::Debug + Send + Sync + 'static {
fn clone_box(&self) -> Box<dyn AdhocValue>;
}
impl<V> AdhocValue for V
where
V: Clone + fmt::Display + fmt::Debug + Send + Sync + 'static,
{
fn clone_box(&self) -> Box<dyn AdhocValue> {
Box::new(self.clone())
}
}
impl Clone for Box<dyn AdhocValue> {
fn clone(&self) -> Box<dyn AdhocValue> {
self.clone_box()
}
}
#[cfg(test)]
mod test {
use super::*;
use static_assertions::*;
#[test]
fn no_context() {
assert_impl_all!(
NoContext: Default,
Copy,
Clone,
fmt::Debug,
fmt::Display,
Context
);
}
#[test]
fn adhoc_context() {
assert_impl_all!(NoContext: Default, Clone, fmt::Debug, fmt::Display, Context);
}
}