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
//! # `explainable`
//!
//! A zero-overhead educational layer for Rust libraries.
//!
//! This crate defines the traits and types that any domain crate can implement
//! to give its users a step-by-step, pedagogical view of an operation chain ---
//! text explanations, before/after visuals, or both --- without touching the
//! domain's hot path.
//!
//! ## Architecture
//!
//! Three interfaces make up the system:
//!
//! | Trait | Implemented by | Purpose |
//! |---|---|---|
//! | [`ExplainDisplay`] | Domain crate | Opaque rendering surface |
//! | [`RenderVisual`] | Domain crate | Produces a before/after [`ExplainDisplay`] |
//! | [`Explainable`] | Domain crate | Opts a type into the system (one line) |
//!
//! The [`#[explainable]`](explainable) attribute macro annotates operation
//! trait definitions in the domain crate. For every method in the trait it
//! generates a matching method on [`Explaining<T>`] that snapshots `inner`
//! before the call, calls through to the real operation, captures after, and
//! accumulates an [`Explanation`].
//!
//! ## Quick start
//!
//! ```rust,ignore
//! // Domain crate --- opt in
//! #[explainable]
//! pub trait MyOperations {
//! fn some_operation(&self) -> Result<Self, MyError>;
//! }
//!
//! impl MyOperationsExplainText for MyType {
//! fn explain_text_some_operation(before: &Self, after: &Self) -> String {
//! format!("Did the thing. Value went from {} to {}.", before, after)
//! }
//! }
//!
//! impl RenderVisual for MyType { /* ... */ }
//! impl Explainable for MyType {}
//!
//! // User --- one extra call to open an explaining chain
//! use my_crate::MyOperationsExt; // bring the extension trait into scope
//!
//! let (result, _explanations) = value
//! .explaining(ExplainMode::Both)
//! .some_operation()
//! .explain();
//! ```
//!
//! ## Overhead model
//!
//! The hot path is completely unaffected. Explanation machinery activates only
//! when [`.explaining()`][Explainable::explaining] is called. Every existing
//! call site remains valid and untouched.
pub use explainable;
// ─── Enums ───────────────────────────────────────────────────────────────────
/// Controls which kinds of explanation are produced for each operation in a
/// chain.
///
/// Chosen once when calling [`.explaining()`][Explainable::explaining] and
/// applied uniformly to every operation in that chain.
///
/// # Examples
///
/// ```rust,ignore
/// value.explaining(ExplainMode::Text) // text only
/// value.explaining(ExplainMode::Visual) // before/after visuals only
/// value.explaining(ExplainMode::Both) // text and visuals
/// ```
// ─── Traits ──────────────────────────────────────────────────────────────────
/// An opaque rendering surface for a single visual explanation.
///
/// Implemented by the domain crate --- `explainable` never renders directly; it
/// only calls [`display`][ExplainDisplay::display] at the right moment.
///
/// The domain crate supplies a concrete type and
/// implements this trait on it. `explainable` stores it as
/// `Box<dyn ExplainDisplay>` and calls `display` when surfacing explanations.
///
/// # Examples
///
/// ```rust,ignore
/// struct MyVisual { html: String }
///
/// impl ExplainDisplay for MyVisual {
/// fn display(&self) {
/// open_in_browser(&self.html);
/// }
/// }
/// ```
/// Implemented by a domain type to supply before/after visual explanations.
///
/// This is the single seam through which `explainable` requests a visual
/// without knowing anything about how it is produced. The domain crate owns
/// its rendering infrastructure entirely.
///
/// # Examples
///
/// ```rust,ignore
/// impl RenderVisual for MyType {
/// fn render_visual(before: &Self, after: &Self) -> Box<dyn ExplainDisplay> {
/// Box::new(MyVisual { html: plot_before_after(before, after) })
/// }
/// }
/// ```
/// Marker trait that opts a type into the explaining system.
///
/// Any type that is [`Clone`] and implements [`RenderVisual`] can implement
/// this trait. No body is required --- the default implementation of
/// [`explaining`][Explainable::explaining] is provided automatically.
///
/// # Examples
///
/// ```rust,ignore
/// // One line --- the macro and the two trait impls do the rest.
/// impl Explainable for MyType {}
/// ```
// ─── Data types ──────────────────────────────────────────────────────────────
/// A single accumulated explanation for one operation in an explaining chain.
///
/// Each field is optional --- which are populated depends on the [`ExplainMode`]
/// chosen at the start of the chain. Proc-macro-generated wrapper methods on
/// [`Explaining`] construct these and push them onto the explanation list.
///
/// Instances are returned inside the [`Vec<Explanation>`] from
/// [`Explaining::explain`], giving callers the ability to inspect or
/// re-surface them after the fact via [`surface`][Explanation::surface].
/// An explaining chain wrapping a value of type `T`.
///
/// Created by calling [`.explaining(mode)`][Explainable::explaining] on any
/// type that implements [`Explainable`]. Methods generated by the
/// `#[explainable]` proc-macro are available on this wrapper --- each delegates
/// to the real operation on `inner`, captures a before/after [`Explanation`],
/// and returns `&mut Self` for method chaining.
///
/// Call [`.explain()`][Explaining::explain] at the end of the chain to surface
/// all accumulated explanations and recover the final value.
///
/// # Examples
///
/// ```rust,ignore
/// let (result, explanations) = value
/// .explaining(ExplainMode::Both)
/// .normalize()
/// .scale(0.5)
/// .trim(100, 200)
/// .explain();
/// ```