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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
use Display;
use ;
use crateBuffer;
use crate;
/// [`Display`](std::fmt::Display) with colors
///
/// Write `self` into the buffer with colors.
/// Please use the named colors provided by the color [`Palette`](crate::style::Palette).
/// While it is also possible to use all terminal colors directly, this should only be done as an exception.
/// This way, it is possible to customize the appearance by switching color [`Palette`](crate::style::Palette)s.
///
/// Implementations should not write newlines.
/// For multi-line segments, implement the [`Print`](crate::Print) trait instead.
/// Types that are [`Inline`] *or* [`Display`]
///
/// This is a helper trait that is blanket-implemented for **both** [`Inline`] and [`Display`] types.
/// [`Display`] types are written to the [`Buffer`] without colors.
/// Useful for functions/structs where the marker type `M` can be inferred.
///
/// This trait is sealed and cannot be implemented.
///
/// # Implementation
///
/// Because Rust does not have any specialization or way to express `where T: NOT Trait`, it is not possible to blanket-implement the (exact) same trait twice in such a way that a conflict *could* exist.
/// It would be impossible to blanket-implement a `trait Pushable { … }` for **both** [`Inline`] and [`Display`] types: a type could exist that is itself [`Inline`] as well as [`Display`] and this type would have two different implementations of `Pushable`.
///
/// The workaround used to resolve this is described in detail below.
///
/// This trait has a generic type parameter `M` that exists solely as a "marker" to disambiguate implementations.
/// Then a different empty marker type is specified for each of the implementations:
/// - `impl<T: Display> Pushable<marker::AsDisplay> for T`
/// - `impl<T: Inline> Pushable<marker::AsInline> for T`
///
/// This sidesteps the conflict: `Pushable<AsDisplay>` and `Pushable<AsInline>` are essentially just two different traits - there is no issue with having both implemented for the same type.
/// But then again, two different traits is what we started with: [`Inline`] and [`Display`].
/// So, why bother?
///
/// The trick to using this trait is to be generic over a `T: Pushable<M>` for *any* `M` and getting type inference to figure out the correct `M`.
/// So, for a function to accept any type that is either [`Inline`] or [`Display`], it would look like this:
/// ```
/// # trait Pushable<M> {}
/// fn push_any<M, T: Pushable<M>>(thing: T) {}
/// ```
/// Somewhat surprisingly, this works very well!
/// The marker type is (correctly) inferred for all types that are **either** [`Inline`] or [`Display`].
/// Theoretically, it is even possible to pick which implementations to use when both apply by disambiguating the function call with type annotations like so: `push_any::<marker::AsInline, _>(…)` (note the `_` to have the rest inferred).
/// But because this is inconvenient in practice, the marker types are (currently) not exposed.
///
/// # Caveats
///
/// Arguably, this workaround is only really useful in cases where type inference can be relied on (certainly, those are the only cases where it is convenient).
/// Unfortunately, type inference can only be relied on as much as the types can be relied on to not implement *the other* trait.
/// In practice, this means that one should think about whether a type will need to implement both [`Inline`] and [`Display`] beforehand (and consider whether using a [newtype] instead is better).
/// Otherwise, it is possible to wind up having to fix all the places where type inference was relied on but no longer can be.
///
/// Crucially, **for a type that is [`Inline`] or [`Display`], implementing the other trait as well could be a breaking change**.
/// This is surprising because generally implementing a trait is not considered breaking, but in this case it could lead to code failing to compile with `type annotations needed`.
/// Because of Rust's [orphan rules], this problem is merely theoretical when it comes to third party crates.
/// For any type, such a surprise breaking change could only be introduced by [`conciliator`](crate) implementing [`Inline`] for a third-party type that is already [`Display`] (I won't do this) or a third-party crate deciding to depend on [`conciliator`](crate) and doing so for it's own type (very unlikely).
/// Note though that this only applies because crates are unable to implement `Pushable` directly.
///
/// [newtype]: https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#using-the-newtype-pattern-to-implement-external-traits-on-external-types
/// [orphan rules]: https://rust-lang.github.io/chalk/book/clauses/coherence.html#coherence
/// Private module to seal [`Pushable`]
/// Marker types to disambiguate [`Pushable`] implementations
///
/// See [`Pushable`] for a detailed explanation.
/// Helper type that calls the wrapped closure when [`Inline`]d
///
/// [`Inline`] is implemented only for [`Fn`]`(&mut `[`Buffer`]`)` closures.
/// [`FnMut`] closures don't work because [`Inline::inline`] has an immutable `&self`.
///
/// This is the type returned by [`inline!`](crate::inline).
;
/// Concatenate [`Pushable`] types to become [`Inline`] as one
///
/// This macro takes any number of unambiguously [`Pushable`] values ([Expressions] to be precise) and returns a value that, when [`Inline`]d, pushes all the provided values into the buffer.
/// Simply put, it concatenates all of its arguments into a single value that implements [`Inline`].
///
/// For example:
/// ```
/// let con = conciliator::init();
/// use conciliator::{Conciliator, inline};
/// con.status(inline!("Hello", ' ', "World", '!'));
/// ```
/// Prints:
/// ```text
/// [ > ] Hello World!
/// ```
/// Using it like this, it is only very slightly different to using [`format_args!`], but crucially, it is very easy to adjust this example to color the word `World` with [`Color::Beta`](crate::style::Color::Beta) and to make both words **bold**:
/// ```
/// let con = conciliator::init();
/// use conciliator::{Conciliator, inline, WrapBold as Bold};
/// con.status(inline!(Bold::Plain("Hello"), ' ', Bold::Beta("World"), '!'));
/// ```
///
/// In this case, the [`inline!`](crate::inline) macro is just slightly more concise than appending to the [`status`](crate::Conciliator::status) [`Line`](crate::core::Line) before printing it ([by dropping it](crate::core)), but in other cases, like creating a [`List`](crate::List) with a header, using this macro is significantly more ergonomic than the alternatives.
///
/// E.g.
/// ```
/// let con = conciliator::init();
/// use conciliator::{List, inline, WrapBold::Plain as Bold};
/// List::headless(0..3)
/// .with_count(inline!(Bold("bold"), " numbers"))
/// .with_wrap(Bold)
/// .print_to(&con);
/// ```
/// Prints the list with a header like this:
///
/// `[ > ] 3 `**`bold`**` numbers:`
///
/// [Expressions]: https://doc.rust-lang.org/reference/expressions.html
/// Wraps any [`Display`] type to be [`Inline`]d with the chosen [`Color`]
/// Wraps any [`Display`] type to be [`Inline`]d with the chosen [`Color`] but in **bold**
/// Wraps a [`str`] to be [`Inline`]d as a colored **`[ tag ]`**
///
/// Simply calls [`Paint::tag`] when [`Inline`]d, appending the [`str`] in square brackets, with a trailing space.
/// The color of the brackets is determined by [`Palette::tag`](crate::style::Palette) and the entire segment is in **bold**.
/// ```
/// let con = conciliator::init();
/// use conciliator::{Conciliator, Tag, style::Color};
/// con.line(Tag(Color::Alpha, "×")).push("A new tag!");
/// ```
///
/// ```text
/// [ × ] A new tag!
/// ```
/// (Obviously the formatting & colors aren't reproduced here.)
;
/*
* INLINE
*/
/// Blanket impl for convenience
/*
* PUSHABLE
*/
/*
* LAMBDA FMT
*/
/*
* WRAP & WRAP BOLD
*/
/*
* TAG
*/
/*
* TESTS
*/