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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//! Definition of [`SmartDisplay`] and its related items.
//!
//! [`SmartDisplay`] is a trait that allows authors to provide additional information to both the
//! formatter and other users. This information is provided in the form of a metadata type. The only
//! required piece of metadata is the width of the value. This is _before_ it is passed to the
//! formatter (i.e. it does not include any padding added by the formatter). Other information
//! can be stored in a custom metadata type as needed. This information may be made available to
//! downstream users, but it is not required.
//!
//! This module contains the [`SmartDisplay`] and associated items.
//!
//! # Example
//!
//! ```rust
//! use std::fmt;
//!
//! use powerfmt::ext::FormatterExt as _;
//! use powerfmt::smart_display::{self, FormatterOptions, Metadata, SmartDisplay};
//!
//! #[derive(Debug)]
//! struct User {
//!     id: usize,
//! }
//!
//! // If you try to use `UserMetadata` in the `SmartDisplay` implementation, you will get a
//! // compiler error about a private type being used publicly. To avoid this, use this attribute to
//! // declare a private metadata type. You shouldn't need to worry about how this works, but be
//! // aware that any public fields or methods remain usable by downstream users.
//! #[smart_display::private_metadata]
//! struct UserMetadata {
//!     username: String,
//!     legal_name: String,
//! }
//!
//! // This attribute can be applied to `SmartDisplay` implementations. It will generate an
//! // implementation of `Display` that delegates to `SmartDisplay`, avoiding the need to write
//! // boilerplate.
//! #[smart_display::delegate]
//! impl SmartDisplay for User {
//!     type Metadata = UserMetadata;
//!
//!     fn metadata(&self, _: FormatterOptions) -> Metadata<'_, Self> {
//!         // This could be obtained from a database, for example.
//!         let legal_name = "John Doe".to_owned();
//!         let username = "jdoe".to_owned();
//!
//!         // Note that this must be kept in sync with the implementation of `fmt_with_metadata`.
//!         let width = smart_display::padded_width_of!(username, " (", legal_name, ")",);
//!
//!         Metadata::new(
//!             width,
//!             self,
//!             UserMetadata {
//!                 username,
//!                 legal_name,
//!             },
//!         )
//!     }
//!
//!     // Use the now-generated metadata to format the value. Here we use the `pad_with_width`
//!     // method to use the alignment and desired width from the formatter.
//!     fn fmt_with_metadata(
//!         &self,
//!         f: &mut fmt::Formatter<'_>,
//!         metadata: Metadata<Self>,
//!     ) -> fmt::Result {
//!         f.pad_with_width(
//!             metadata.unpadded_width(),
//!             format_args!("{} ({})", metadata.username, metadata.legal_name),
//!         )
//!     }
//! }
//!
//! let user = User { id: 42 };
//! assert_eq!(user.to_string(), "jdoe (John Doe)");
//! assert_eq!(format!("{user:>20}"), "     jdoe (John Doe)");
//! ```

use core::cmp;
use core::convert::Infallible;
use core::fmt::{Alignment, Debug, Display, Formatter, Result};
use core::marker::PhantomData;
use core::mem::MaybeUninit;
use core::ops::Deref;

/// Compute the width of multiple items while optionally declaring the options for each item.
///
/// ```rust
/// # use powerfmt::smart_display;
/// let alpha = 0;
/// let beta = 1;
/// let gamma = 100;
///
/// let width = smart_display::padded_width_of!(
///     alpha, // use the default options
///     beta => width(2), // use the specified options
///     gamma => width(2) sign_plus(true), // use multiple options
/// );
/// assert_eq!(width, 7);
///
/// let formatted = format!("{alpha}{beta:2}{gamma:+2}");
/// assert_eq!(formatted.len(), width);
/// ```
///
/// Supported options are:
///
/// Option                      | Method called
/// ---                         | ---
/// `fill(char)`                | [`FormatterOptions::with_fill`]
/// `sign_plus(bool)`           | [`FormatterOptions::with_sign_plus`]
/// `sign_minus(bool)`          | [`FormatterOptions::with_sign_minus`]
/// `align(Alignment)`          | [`FormatterOptions::with_align`]
/// `width(usize)`              | [`FormatterOptions::with_width`]
/// `precision(usize)`          | [`FormatterOptions::with_precision`]
/// `alternate(bool)`           | [`FormatterOptions::with_alternate`]
/// `sign_aware_zero_pad(bool)` | [`FormatterOptions::with_sign_aware_zero_pad`]
///
/// If there are future additions to [`FormatterOptions`], they will be added to this macro as well.
///
/// Options may be provided in any order and will be called in the order they are provided. The
/// ordering matters if providing both `sign_plus` and `sign_minus`.
#[cfg(doc)]
#[doc(hidden)] // Don't show at crate root.
#[macro_export]
macro_rules! padded_width_of {
    ($($t:tt)*) => {};
}

#[cfg(not(doc))]
#[allow(missing_docs)] // This is done with `#[cfg(doc)]` to avoid showing the various rules.
#[macro_export]
macro_rules! __not_public_at_root__padded_width_of {
    // Base case
    (@inner [] [$($output:tt)+]) => { $($output)+ };
    (@inner [$e:expr $(, $($remaining:tt)*)?] [$($expansion:tt)+]) => {
        $crate::smart_display::padded_width_of!(@inner [$($($remaining)*)?] [
            $($expansion)+ + $crate::smart_display::Metadata::padded_width_of(
                &$e,
                $crate::smart_display::padded_width_of!(@options)
            )
        ])
    };
    (@inner
        [$e:expr => $($call:ident($call_expr:expr))+ $(, $($remaining:tt)*)?]
        [$($expansion:tt)+]
    ) => {
        $crate::smart_display::padded_width_of!(@inner [$($($remaining)*)?] [
            $($expansion)+ + $crate::smart_display::Metadata::padded_width_of(
                &$e,
                *$crate::smart_display::padded_width_of!(@options $($call($call_expr))+)
            )
        ])
    };

    // Options base case
    (@options_inner [] [$($output:tt)+]) => { $($output)+ };
    (@options_inner [fill($e:expr) $($remaining:tt)*] [$($expansion:tt)*]) => {
        $crate::smart_display::padded_width_of!(@options_inner [$($remaining)*] [
            $($expansion)*.with_fill($e)
        ])
    };
    (@options_inner [sign_plus($e:expr) $($remaining:tt)*] [$($expansion:tt)*]) => {
        $crate::smart_display::padded_width_of!(@options_inner [$($remaining)*] [
            $($expansion)*.with_sign_plus($e)
        ])
    };
    (@options_inner [sign_minus($e:expr) $($remaining:tt)*] [$($expansion:tt)*]) => {
        $crate::smart_display::padded_width_of!(@options_inner [$($remaining)*] [
            $($expansion)*.with_sign_minus($e)
        ])
    };
    (@options_inner [align($e:expr) $($remaining:tt)*] [$($expansion:tt)*]) => {
        $crate::smart_display::padded_width_of!(@options_inner [$($remaining)*] [
            $($expansion)*.with_align(Some($e))
        ])
    };
    (@options_inner [width($e:expr) $($remaining:tt)*] [$($expansion:tt)*]) => {
        $crate::smart_display::padded_width_of!(@options_inner [$($remaining)*] [
            $($expansion)*.with_width(Some($e))
        ])
    };
    (@options_inner [precision($e:expr) $($remaining:tt)*] [$($expansion:tt)*]) => {
        $crate::smart_display::padded_width_of!(@options_inner [$($remaining)*] [
            $($expansion)*.with_precision(Some($e))
        ])
    };
    (@options_inner [alternate($e:expr) $($remaining:tt)*] [$($expansion:tt)*]) => {
        $crate::smart_display::padded_width_of!(@options_inner [$($remaining)*] [
            $($expansion)*.with_width($e)
        ])
    };
    (@options_inner [sign_aware_zero_pad($e:expr) $($remaining:tt)*] [$($expansion:tt)*]) => {
        $crate::smart_display::padded_width_of!(@options_inner [$($remaining)*] [
            $($expansion)*.with_sign_aware_zero_pad($e)
        ])
    };
    // Options entry point
    (@options $($e:tt)*) => {
        $crate::smart_display::padded_width_of!(@options_inner [$($e)*] [
            $crate::smart_display::FormatterOptions::default()
        ])
    };

    // Entry point
    ($($t:tt)*) => {
        $crate::smart_display::padded_width_of!(
            @inner [$($t)*] [0]
        )
    };
}

#[cfg(not(doc))]
pub use __not_public_at_root__padded_width_of as padded_width_of;
#[cfg(doc)]
#[doc(inline)] // Show in this module.
pub use padded_width_of;
/// Implement [`Display`] for a type by using its implementation of [`SmartDisplay`].
///
/// This attribute is applied to the `SmartDisplay` implementation.
///
/// ```rust,no_run
/// # use powerfmt::smart_display::{self, SmartDisplay, Metadata, FormatterOptions};
/// # struct Foo;
/// #[smart_display::delegate]
/// impl SmartDisplay for Foo {
/// #   type Metadata = ();
/// #   fn metadata(&self, f: FormatterOptions) -> Metadata<Self> {
/// #       todo!()
/// #   }
///     // ...
/// }
/// ```
#[cfg(feature = "macros")]
pub use powerfmt_macros::smart_display_delegate as delegate;
/// Declare a private metadata type for `SmartDisplay`.
///
/// Use this attribute if you want to provide metadata for a type that is not public. Doing
/// this will avoid a compiler error about a private type being used publicly. Keep in mind
/// that any public fields, public methods, and trait implementations _will_ be able to be used
/// by downstream users.
///
/// To avoid accidentally exposing details, such as when all fields are public or if the type
/// is a unit struct, the type is annotated with `#[non_exhaustive]` automatically.
///
/// ```rust,no_run
/// # use powerfmt::smart_display;
/// /// Metadata for `Foo`
/// #[smart_display::private_metadata]
/// #[derive(Debug)]
/// pub(crate) struct FooMetadata {
///     pub(crate) expensive_to_calculate: usize,
/// }
/// ```
#[cfg(feature = "macros")]
pub use powerfmt_macros::smart_display_private_metadata as private_metadata;

#[derive(Debug)]
enum FlagBit {
    SignPlus,
    SignMinus,
    Alternate,
    SignAwareZeroPad,
    WidthIsInitialized,
    PrecisionIsInitialized,
}

/// Configuration for formatting.
///
/// This struct is obtained from a [`Formatter`]. It provides the same functionality as that of a
/// reference to a `Formatter`. However, it is not possible to construct a `Formatter`, which is
/// necessary for some use cases of [`SmartDisplay`]. `FormatterOptions` implements [`Default`] and
/// has builder methods to alleviate this.
#[derive(Clone, Copy)]
pub struct FormatterOptions {
    flags: u8,
    fill: char,
    align: Option<Alignment>,
    width: MaybeUninit<usize>,
    precision: MaybeUninit<usize>,
}

impl Debug for FormatterOptions {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        f.debug_struct("FormatterOptions")
            .field("fill", &self.fill)
            .field("align", &self.align())
            .field("width", &self.width())
            .field("precision", &self.precision())
            .field("sign_plus", &self.sign_plus())
            .field("sign_minus", &self.sign_minus())
            .field("alternate", &self.alternate())
            .field("sign_aware_zero_pad", &self.sign_aware_zero_pad())
            .finish()
    }
}

impl Default for FormatterOptions {
    #[inline]
    fn default() -> Self {
        Self {
            flags: 0,
            fill: ' ',
            align: None,
            width: MaybeUninit::uninit(),
            precision: MaybeUninit::uninit(),
        }
    }
}

impl FormatterOptions {
    /// Sets the fill character to use whenever there is alignment.
    #[inline]
    pub fn with_fill(&mut self, c: char) -> &mut Self {
        self.fill = c;
        self
    }

    /// Set whether the `+` flag is specified.
    #[inline]
    pub fn with_sign_plus(&mut self, b: bool) -> &mut Self {
        if b {
            self.flags |= 1 << FlagBit::SignPlus as u8;
            self.flags &= !(1 << FlagBit::SignMinus as u8);
        } else {
            self.flags &= !(1 << FlagBit::SignPlus as u8);
        }
        self
    }

    /// Set whether the `-` flag is specified.
    #[inline]
    pub fn with_sign_minus(&mut self, b: bool) -> &mut Self {
        if b {
            self.flags |= 1 << FlagBit::SignMinus as u8;
            self.flags &= !(1 << FlagBit::SignPlus as u8);
        } else {
            self.flags &= !(1 << FlagBit::SignMinus as u8);
        }
        self
    }

    /// Set the flag indicating what form of alignment is requested, if any.
    #[inline]
    pub fn with_align(&mut self, align: Option<Alignment>) -> &mut Self {
        self.align = align;
        self
    }

    /// Set the optional integer width that the output should be.
    #[inline]
    pub fn with_width(&mut self, width: Option<usize>) -> &mut Self {
        if let Some(width) = width {
            self.flags |= 1 << FlagBit::WidthIsInitialized as u8;
            self.width = MaybeUninit::new(width);
        } else {
            self.flags &= !(1 << FlagBit::WidthIsInitialized as u8);
        }
        self
    }

    /// Set the optional precision for numeric types. Alternatively, the maximum width for string
    /// types.
    #[inline]
    pub fn with_precision(&mut self, precision: Option<usize>) -> &mut Self {
        if let Some(precision) = precision {
            self.flags |= 1 << FlagBit::PrecisionIsInitialized as u8;
            self.precision = MaybeUninit::new(precision);
        } else {
            self.flags &= !(1 << FlagBit::PrecisionIsInitialized as u8);
        }
        self
    }

    /// Set whether the `#` flag is specified.
    #[inline]
    pub fn with_alternate(&mut self, b: bool) -> &mut Self {
        if b {
            self.flags |= 1 << FlagBit::Alternate as u8;
        } else {
            self.flags &= !(1 << FlagBit::Alternate as u8);
        }
        self
    }

    /// Set whether the `0` flag is specified.
    #[inline]
    pub fn with_sign_aware_zero_pad(&mut self, b: bool) -> &mut Self {
        if b {
            self.flags |= 1 << FlagBit::SignAwareZeroPad as u8;
        } else {
            self.flags &= !(1 << FlagBit::SignAwareZeroPad as u8);
        }
        self
    }
}

impl FormatterOptions {
    /// Character used as 'fill' whenever there is alignment.
    #[inline]
    #[must_use]
    pub const fn fill(&self) -> char {
        self.fill
    }

    /// Flag indicating what form of alignment was requested.
    #[inline]
    #[must_use]
    pub const fn align(&self) -> Option<Alignment> {
        self.align
    }

    /// Optionally specified integer width that the output should be.
    #[inline]
    #[must_use]
    pub const fn width(&self) -> Option<usize> {
        if (self.flags >> FlagBit::WidthIsInitialized as u8) & 1 == 1 {
            // Safety: `width` is initialized if the flag is set.
            Some(unsafe { self.width.assume_init() })
        } else {
            None
        }
    }

    /// Optionally specified precision for numeric types. Alternatively, the maximum width for
    /// string types.
    #[inline]
    #[must_use]
    pub const fn precision(&self) -> Option<usize> {
        if (self.flags >> FlagBit::PrecisionIsInitialized as u8) & 1 == 1 {
            // Safety: `precision` is initialized if the flag is set.
            Some(unsafe { self.precision.assume_init() })
        } else {
            None
        }
    }

    /// Determines if the `+` flag was specified.
    #[inline]
    #[must_use]
    pub const fn sign_plus(&self) -> bool {
        (self.flags >> FlagBit::SignPlus as u8) & 1 == 1
    }

    /// Determines if the `-` flag was specified.
    #[inline]
    #[must_use]
    pub const fn sign_minus(&self) -> bool {
        (self.flags >> FlagBit::SignMinus as u8) & 1 == 1
    }

    /// Determines if the `#` flag was specified.
    #[inline]
    #[must_use]
    pub const fn alternate(&self) -> bool {
        (self.flags >> FlagBit::Alternate as u8) & 1 == 1
    }

    /// Determines if the `0` flag was specified.
    #[inline]
    #[must_use]
    pub const fn sign_aware_zero_pad(&self) -> bool {
        (self.flags >> FlagBit::SignAwareZeroPad as u8) & 1 == 1
    }
}

impl From<&Formatter<'_>> for FormatterOptions {
    fn from(value: &Formatter<'_>) -> Self {
        *Self::default()
            .with_fill(value.fill())
            .with_sign_plus(value.sign_plus())
            .with_sign_minus(value.sign_minus())
            .with_align(value.align())
            .with_width(value.width())
            .with_precision(value.precision())
            .with_alternate(value.alternate())
            .with_sign_aware_zero_pad(value.sign_aware_zero_pad())
    }
}

impl From<&mut Formatter<'_>> for FormatterOptions {
    #[inline]
    fn from(value: &mut Formatter<'_>) -> Self {
        (&*value).into()
    }
}

/// Information used to format a value. This is returned by [`SmartDisplay::metadata`].
///
/// This type is generic over any user-provided type. This allows the author to store any
/// information that is needed. For example, a type's implementation of [`SmartDisplay`] may need
/// to calculate something before knowing its width. This calculation can be performed, with the
/// result being stored in the custom metadata type.
///
/// Note that `Metadata` _always_ contains the width of the type. Authors do not need to store this
/// information in their custom metadata type.
///
/// Generally speaking, a type should be able to be formatted using only its metadata, fields, and
/// the formatter. Any other information should be stored in the metadata type.
pub struct Metadata<'a, T>
where
    T: SmartDisplay + ?Sized,
{
    unpadded_width: usize,
    metadata: T::Metadata,
    _value: PhantomData<&'a T>, // variance
}

// manual impls for bounds
impl<T> Debug for Metadata<'_, T>
where
    T: SmartDisplay,
    T::Metadata: Debug,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        f.debug_struct("Metadata")
            .field("unpadded_width", &self.unpadded_width)
            .field("metadata", &self.metadata)
            .finish()
    }
}

impl<T> Clone for Metadata<'_, T>
where
    T: SmartDisplay,
    T::Metadata: Clone,
{
    fn clone(&self) -> Self {
        Self {
            unpadded_width: self.unpadded_width,
            metadata: self.metadata.clone(),
            _value: self._value,
        }
    }
}

impl<T> Copy for Metadata<'_, T>
where
    T: SmartDisplay,
    T::Metadata: Copy,
{
}

impl<'a, T> Metadata<'a, T>
where
    T: SmartDisplay + ?Sized,
{
    /// Creates a new `Metadata` with the given width and metadata. While the width _should_ be
    /// exact, this is not a requirement for soundness.
    pub const fn new(unpadded_width: usize, _value: &T, metadata: T::Metadata) -> Self {
        Self {
            unpadded_width,
            metadata,
            _value: PhantomData,
        }
    }

    /// Reuse the metadata for another type. This is useful when implementing [`SmartDisplay`] for a
    /// type that wraps another type. Both type's metadata type must be the same.
    pub fn reuse<'b, U>(self) -> Metadata<'b, U>
    where
        'a: 'b,
        U: SmartDisplay<Metadata = T::Metadata> + ?Sized,
    {
        Metadata {
            unpadded_width: self.unpadded_width,
            metadata: self.metadata,
            _value: PhantomData,
        }
    }

    /// Obtain the width of the value before padding.
    pub const fn unpadded_width(&self) -> usize {
        self.unpadded_width
    }

    /// Obtain the width of the value after padding.
    pub fn padded_width(&self, f: FormatterOptions) -> usize {
        match f.width() {
            Some(requested_width) => cmp::max(self.unpadded_width(), requested_width),
            None => self.unpadded_width(),
        }
    }
}

impl Metadata<'_, Infallible> {
    /// Obtain the width of the value before padding, given the formatter options.
    pub fn unpadded_width_of<T>(value: T, f: FormatterOptions) -> usize
    where
        T: SmartDisplay,
    {
        value.metadata(f).unpadded_width
    }

    /// Obtain the width of the value after padding, given the formatter options.
    pub fn padded_width_of<T>(value: T, f: FormatterOptions) -> usize
    where
        T: SmartDisplay,
    {
        value.metadata(f).padded_width(f)
    }
}

/// Permit using `Metadata` as a smart pointer to the user-provided metadata.
impl<T> Deref for Metadata<'_, T>
where
    T: SmartDisplay + ?Sized,
{
    type Target = T::Metadata;

    fn deref(&self) -> &T::Metadata {
        &self.metadata
    }
}

/// Format trait that allows authors to provide additional information.
///
/// This trait is similar to [`Display`], but allows the author to provide additional information
/// to the formatter. This information is provided in the form of a custom metadata type.
///
/// The only required piece of metadata is the width of the value. This is _before_ it is passed to
/// the formatter (i.e. it does not include any padding added by the formatter). Other information
/// can be stored in a custom metadata type as needed. This information may be made available to
/// downstream users, but it is not required.
///
/// **Note**: While both `fmt_with_metadata` and `fmt` have default implementations, it is strongly
/// recommended to implement only `fmt_with_metadata`. `fmt` should be implemented if and only if
/// the type does not require any of the calculated metadata. In that situation, `fmt_with_metadata`
/// should be omitted.
#[cfg_attr(__powerfmt_docs, rustc_must_implement_one_of(fmt, fmt_with_metadata))]
pub trait SmartDisplay: Display {
    /// User-provided metadata type.
    type Metadata;

    /// Compute any information needed to format the value. This must, at a minimum, determine the
    /// width of the value before any padding is added by the formatter.
    ///
    /// If the type uses other types that implement `SmartDisplay` verbatim, the inner types should
    /// have their metadata calculated and included in the returned value.
    ///
    /// # Lifetimes
    ///
    /// This method's return type contains a lifetime to `self`. This ensures that the metadata will
    /// neither outlive the value nor be invalidated by a mutation of the value (barring interior
    /// mutability).
    ///
    /// ```rust,compile_fail
    /// # use std::fmt;
    /// # use std::fmt::Write;
    /// # use powerfmt::buf::WriteBuffer;
    /// # use powerfmt::smart_display::{self, FormatterOptions, Metadata, SmartDisplay};
    /// #[derive(Debug)]
    /// struct WrappedBuffer(WriteBuffer<128>);
    ///
    /// #[smart_display::delegate]
    /// impl SmartDisplay for WrappedBuffer {
    ///     type Metadata = ();
    ///
    ///     fn metadata(&self, _: FormatterOptions) -> Metadata<'_, Self> {
    ///         Metadata::new(self.0.len(), self, ())
    ///     }
    ///
    ///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    ///         f.pad(self.0.as_str())
    ///     }
    /// }
    ///
    /// let mut buf = WrappedBuffer(WriteBuffer::new());
    /// let metadata = buf.metadata(FormatterOptions::default());
    /// // We cannot mutate the buffer while it is borrowed and use its previous metadata on the
    /// // following line.
    /// write!(buf.0, "Hello, world!")?;
    /// assert_eq!(metadata.width(), 13);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    fn metadata(&self, f: FormatterOptions) -> Metadata<'_, Self>;

    /// Format the value using the given formatter and metadata. The formatted output should have
    /// the width indicated by the metadata. This is before any padding is added by the
    /// formatter.
    ///
    /// If the metadata is not needed, you should implement the `fmt` method instead.
    fn fmt_with_metadata(&self, f: &mut Formatter<'_>, _metadata: Metadata<'_, Self>) -> Result {
        SmartDisplay::fmt(self, f)
    }

    /// Format the value using the given formatter. This is the same as [`Display::fmt`].
    ///
    /// The default implementation of this method calls `fmt_with_metadata` with the result of
    /// `metadata`. Generally speaking, this method should not be implemented. You should implement
    /// the `fmt_with_metadata` method instead.
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        let metadata = self.metadata(f.into());
        self.fmt_with_metadata(f, metadata)
    }
}