rformat 0.2.0

Runtime formatting library
Documentation
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
use crate::error::{FormatError, Result};
use crate::fmt::violence;
use crate::format_spec::{self, Argument, Count, FormatSpec, Precision};
use crate::prelude::*;

/// Represents a formatting parameter with an identifier and its formattable value.
///
/// The identifier is used for named parameter access in format strings.
/// The formattable is the actual value that will be formatted.
pub struct Parameter<'ident, 'formattable> {
    /// The string identifier of the parameter, used for named parameter access.
    pub identifier: &'ident str,
    /// The formattable value that will be formatted.
    pub formattable: Formattable<'formattable>,
}

/// Creates a formatting parameter from an expression.
///
/// This macro has two forms:
/// - `param!(name = expr)` - Creates a parameter with the specified name and value
/// - `param!(expr)` - Creates a parameter with the name derived from the expression
#[macro_export]
macro_rules! _param {
    ($param_name:ident = $param:expr) => {
        $crate::fmt::format::Parameter {
            identifier: ::core::stringify!($param_name),
            formattable: $crate::formattable::into_formattable!($param),
        }
    };
    ($param:expr) => {
        $crate::fmt::format::Parameter {
            identifier: ::core::stringify!($param),
            formattable: $crate::formattable::into_formattable!($param),
        }
    };
}
pub use _param as param;

/// Creates a vector of formatting parameters.
///
/// This macro takes a comma-separated list of expressions and converts each into a `Parameter`.
/// Each expression is processed by the `param!` macro.
#[macro_export]
macro_rules! _params {
    // Empty list
    () => ([]);

    // Single named parameter
    ($name:ident = $value:expr) => (
        [$crate::fmt::format::param!($name = $value)]
    );

    // Single unnamed parameter
    ($value:expr) => (
        [$crate::fmt::format::param!($value)]
    );

    // Only named parameters
    ($($name:ident = $value:expr),*) => (
        [$($crate::fmt::format::param!($name = $value)),*]
    );

    // Mixed named and unnamed parameters - we need to use TT munching

    // Stop at empty tail.
    (@acc [$($acc:tt)*];) => ([$($acc)*]);

    // Handle last named parameter
    (@acc [$($acc:tt)*]; $name:ident = $value:expr) => (
        [$($acc)*, $crate::fmt::format::param!($name = $value)]
    );

    // Handle last unnamed parameter
    (@acc [$($acc:tt)*]; $value:expr) => (
        [$($acc)*, $crate::fmt::format::param!($value)]
    );

    // Handle a named parameter
    (@acc [$($acc:tt)*]; $name:ident = $value:expr, $($tail:tt)*) => (
        $crate::fmt::format::params!(@acc [$($acc)*, $crate::fmt::format::param!($name = $value)]; $($tail)*)
    );

    // Handle an unnamed parameter
    (@acc [$($acc:tt)*]; $value:expr, $($tail:tt)*) => (
        $crate::fmt::format::params!(@acc [$($acc)*, $crate::fmt::format::param!($value)]; $($tail)*)
    );

    // Start with a named parameter
    ($name:ident = $value:expr, $($tail:tt)*) => (
        $crate::fmt::format::params!(@acc [$crate::fmt::format::param!($name = $value)]; $($tail)*)
    );

    // Start with an unnamed parameter
    ($value:expr, $($tail:tt)*) => (
        $crate::fmt::format::params!(@acc [$crate::fmt::format::param!($value)]; $($tail)*)
    );
}
pub use _params as params;

/// Formats a string according to the given format specification and parameters.
///
/// This function parses the format string, identifies format placeholders,
/// and replaces them with formatted parameter values.
///
/// # Arguments
///
/// * `fmt` - A string containing format specifications
/// * `params` - A slice of parameters to be formatted
///
/// # Returns
///
/// A `Result` containing the formatted string or an error if formatting fails.
///
/// # Format Syntax
///
/// The format string follows Rust's standard format syntax.
pub fn format_string(fmt: &str, params: &[Parameter]) -> Result<String> {
    let mut result = String::new();

    // Iterator over parameters for sequential access
    let mut param_iter = params.iter();

    // Peekable iterator over characters with their positions
    let mut chars = fmt.char_indices().peekable();

    while let Some((i, c)) = chars.next() {
        if c == '{' {
            if let Some((_, '{')) = chars.peek() {
                // Escaped opening brace: `{{` becomes `{`
                chars.next();
                result.push('{');
            } else {
                // Start of a format placeholder
                let mut format = String::new();
                let mut found = false;

                // Collect characters until finding a closing brace
                for (_, next) in chars.by_ref() {
                    if next == '}' {
                        found = true;
                        break;
                    }

                    format.push(next);
                }

                // Error if no closing brace was found
                if !found {
                    return Err(FormatError::UnmatchedOpeningBrace(i, fmt.to_string()));
                }

                format_and_write_value(&mut result, &format, params, &mut param_iter)?;
            }
        } else if c == '}' {
            if let Some((_, '}')) = chars.peek() {
                // Escaped closing brace: `}}` becomes `}`
                chars.next();
                result.push('}');
            } else {
                // Unmatched closing brace is an error
                return Err(FormatError::UnmatchedClosingBrace(i, fmt.to_string()));
            }
        } else {
            // Regular character, just copy to output
            result.push(c);
        }
    }

    Ok(result)
}

/// Internal representation of a parsed format placeholder.
#[derive(Debug, PartialEq)]
struct Format<'format_string> {
    /// The argument specification (identifier, position, or None for next parameter)
    argument: Option<Argument<'format_string>>,
    /// The formatting specification that controls how the value is formatted
    format_spec: FormatSpec<'format_string>,
}

/// Formats a value according to the given format specification and appends it to a writer.
///
/// This function parses the format specification, resolves the parameter to format,
/// and applies the formatting rules.
///
/// # Arguments
///
/// * `dst` - The destination to write the formatted value to
/// * `format` - The format specification string
/// * `app_params` - All available parameters
/// * `params_iter` - An iterator over parameters for sequential access
///
/// # Returns
///
/// A `Result` indicating success or failure
fn format_and_write_value<
    'param,
    'ident,
    'formattable,
    W: std::fmt::Write,
    I: Iterator<Item = &'param Parameter<'ident, 'formattable>>,
>(
    dst: &mut W,
    format: &str,
    app_params: &'param [Parameter<'ident, 'formattable>],
    params_iter: &mut I,
) -> Result<()>
where
    'ident: 'param,
    'formattable: 'param,
{
    let format = parse_format(format)?;
    let format_spec = &format.format_spec;

    // Handle precision first, as precision=* will consume a parameter,
    // affecting the parameter iterator state before we get to the main parameter
    let precision = resolve_precision_value(&format_spec.precision, app_params, params_iter)?;

    // Resolve the parameter to format
    let parameter = get_parameter_for_formatting(&format.argument, app_params, params_iter)?;

    // Resolve width value, defaulting to 0 if not specified
    let width = resolve_width_value(format_spec, app_params)?.unwrap_or(0);

    // Select the appropriate formatting method based on the format type
    let out = match format_spec.r#type {
        format_spec::Type::Binary => {
            violence::format_binary(format_spec, precision, width, parameter)
        }
        format_spec::Type::Custom(_) => {
            let as_custom = parameter.formattable.try_as_custom().ok_or_else(|| {
                FormatError::TraitNotImplemented(parameter.identifier.to_string(), "Custom")
            })?;

            as_custom.format(format_spec, precision, width, parameter)
        }
        format_spec::Type::Debug => {
            violence::format_debug(format_spec, precision, width, parameter)
        }
        format_spec::Type::DebugLowerHex => {
            violence::format_debug_lower_hex(format_spec, precision, width, parameter)
        }
        format_spec::Type::DebugUpperHex => {
            violence::format_debug_upper_hex(format_spec, precision, width, parameter)
        }
        format_spec::Type::Display => {
            violence::format_display(format_spec, precision, width, parameter)
        }
        format_spec::Type::LowerExp => {
            violence::format_lower_exp(format_spec, precision, width, parameter)
        }
        format_spec::Type::LowerHex => {
            violence::format_lower_hex(format_spec, precision, width, parameter)
        }
        format_spec::Type::Octal => {
            violence::format_octal(format_spec, precision, width, parameter)
        }
        format_spec::Type::Pointer => {
            violence::format_pointer(format_spec, precision, width, parameter)
        }
        format_spec::Type::UpperExp => {
            violence::format_upper_exp(format_spec, precision, width, parameter)
        }
        format_spec::Type::UpperHex => {
            violence::format_upper_hex(format_spec, precision, width, parameter)
        }
    }?;

    // Replace placeholder characters with the actual fill character
    let mut fill = [0; 4];
    let out = out.replace("\u{001A}", format_spec.fill.encode_utf8(&mut fill));

    write!(dst, "{out}")?;

    Ok(())
}

/// Resolves the precision value from the format specification.
///
/// Precision can be:
/// - A direct integer value
/// - An argument reference to a parameter
/// - A '*' that consumes the next parameter
///
/// # Returns
///
/// `Some(usize)` if precision is specified, `None` otherwise
fn resolve_precision_value<
    'param,
    'ident,
    'formattable,
    I: Iterator<Item = &'param Parameter<'ident, 'formattable>>,
>(
    precision: &Option<Precision>,
    params: &'param [Parameter<'ident, 'formattable>],
    params_iter: &mut I,
) -> Result<Option<usize>>
where
    'ident: 'param,
    'formattable: 'param,
{
    if let Some(precision) = precision {
        let precision = match precision {
            Precision::Star => params_iter.next().ok_or(FormatError::NotEnoughParameters)?,
            Precision::Count(Count::Argument(a)) => lookup_parameter_by_argument(a, params)?,
            Precision::Count(Count::Integer(i)) => return Ok(Some(*i)),
        };

        let param = precision
            .formattable
            .try_as_usize()
            .ok_or(FormatError::PrecisionNotUsize)?
            .as_usize();

        Ok(Some(param))
    } else {
        Ok(None)
    }
}

/// Resolves the width value from the format specification.
///
/// Width can be:
/// - A direct integer value
/// - An argument reference to a parameter
///
/// # Returns
///
/// `Some(usize)` if width is specified, `None` otherwise
fn resolve_width_value<'param, 'ident, 'formattable>(
    format_spec: &FormatSpec,
    params: &'param [Parameter<'ident, 'formattable>],
) -> Result<Option<usize>>
where
    'ident: 'param,
    'formattable: 'param,
{
    Ok(match &format_spec.width {
        Some(w) => {
            let param = match w {
                Count::Argument(a) => lookup_parameter_by_argument(a, params)?,
                Count::Integer(i) => return Ok(Some(*i)),
            };

            Some(
                param
                    .formattable
                    .try_as_usize()
                    .ok_or(FormatError::WidthNotUsize)?
                    .as_usize(),
            )
        }
        None => None,
    })
}

/// Gets the parameter to format based on the argument specification.
///
/// If no argument is specified, takes the next parameter from the iterator.
/// If an argument is specified, looks up the parameter by name or position.
fn get_parameter_for_formatting<
    'param,
    'ident,
    'formattable,
    I: Iterator<Item = &'param Parameter<'ident, 'formattable>>,
>(
    argument: &Option<Argument>,
    params: &'param [Parameter<'ident, 'formattable>],
    params_iter: &mut I,
) -> Result<&'param Parameter<'ident, 'formattable>>
where
    'ident: 'param,
    'formattable: 'param,
{
    Ok(match argument {
        None => params_iter.next().ok_or(FormatError::NotEnoughParameters)?,
        Some(a) => lookup_parameter_by_argument(a, params)?,
    })
}

/// Looks up a parameter by argument specification.
///
/// # Arguments
///
/// * `argument` - The argument specification (identifier or position)
/// * `parameters` - The available parameters
///
/// # Returns
///
/// A reference to the matching parameter or an error if not found
fn lookup_parameter_by_argument<'param, 'ident, 'formattable>(
    argument: &Argument,
    parameters: &'param [Parameter<'ident, 'formattable>],
) -> Result<&'param Parameter<'ident, 'formattable>> {
    match argument {
        Argument::Identifier(i) => parameters
            .iter()
            .find(|a| a.identifier == *i)
            .ok_or_else(|| FormatError::NoNamedParameter(i.to_string())),
        Argument::Integer(i) => parameters
            .get(*i)
            .ok_or(FormatError::NoPositionalParameter(*i)),
    }
}

/// Parses a format string into a `Format` struct.
///
/// Format syntax: https://doc.rust-lang.org/std/fmt/#syntax
///
/// # Arguments
///
/// * `format` - The format string content inside braces (without the braces)
///
/// # Returns
///
/// A parsed `Format` struct or an error if the format is invalid
fn parse_format(format: &str) -> Result<Format> {
    // Split into argument and format specification parts at the first colon
    let (argument, format_spec) = format.split_once(':').unwrap_or((format, ""));

    // Parse the argument part
    let argument = if argument.is_empty() {
        None
    } else if argument.chars().all(|c| c.is_ascii_digit()) {
        Some(Argument::Integer(argument.parse::<usize>()?))
    } else {
        Some(Argument::Identifier(argument))
    };

    // Parse the format specification part
    let format_spec = format_spec::parse_format_spec(format_spec)?;

    Ok(Format {
        argument,
        format_spec,
    })
}

#[cfg(test)]
mod tests {
    use std::f64::consts::PI;

    use crate::{
        fmt::{Custom, format::Parameter},
        format_spec::FormatSpec,
        prelude::*,
    };

    macro_rules! assert_format {
        ($fmt:expr) => {
            ::core::assert_eq!(
                ::std::format!($fmt),
                $crate::prelude::rformat!($fmt).unwrap()
            )
        };
        ($fmt:expr, $($args:tt)*) => {
            ::core::assert_eq!(
                ::std::format!($fmt, $($args)*),
                $crate::prelude::rformat!($fmt, $($args)*).unwrap()
            )
        };
    }

    #[test]
    fn format_parameters() {
        assert_format!("{}{}{}", 5, 6, 7);
    }

    #[test]
    fn format_positional_parameters() {
        assert_format!("{2}{1}{0}", 5, 6, 7);
    }

    #[test]
    fn format_named_parameters() {
        let param_1 = 5;
        let param_2 = 6;
        let param_3 = 7;

        assert_eq!(
            format!("{param_2}{param_1}{param_3}"),
            rformat!("{param_2}{param_1}{param_3}", param_1, param_2, param_3).unwrap()
        );
    }

    #[test]
    fn format_width() {
        let width = 10;

        assert_format!("{:5}{:3$}{:w$}", 5, 6, 7, 3, w = width);
    }

    #[test]
    fn format_precision() {
        let precision: usize = 3;

        assert_format!("{0:.5} {0:.1$} {0:.p$}", 123.456789, 2, p = precision);
    }

    #[test]
    fn test_alignment_specifications() {
        // Test left, right, and center alignment with width
        assert_format!("{:<5}", 42);
        assert_format!("{:>5}", 42);
        assert_format!("{:^5}", 42);

        let array = [1, 2, 3];
        assert_format!("{:<5?}", array);
        assert_format!("{:>5?}", array);
        assert_format!("{:^5?}", array);
    }

    #[test]
    fn test_fill_character() {
        // Test with different fill characters
        assert_format!("{:*>7}", 42);
        assert_format!("{:A<7}", 42);
        assert_format!("{:🎉^7}", 42);

        let array = [1, 2, 3];
        assert_format!("{:*<5?}", array);
        assert_format!("{:A>5?}", array);
        assert_format!("{:🎉^5?}", array);
    }

    #[test]
    fn test_number_formatting() {
        // Test sign specifications
        assert_format!("{:+}", 42);
        assert_format!("{:+}", -42i64);
        assert_format!("{:-}", 42);
        assert_format!("{:-}", -42i64);
        assert_format!("{}", 42i64);
        assert_format!("{}", -42i64);

        // Test alternate form (#)
        assert_format!("{:#o}", 42);
        assert_format!("{:#x}", 42);

        // Test zero padding
        assert_format!("{:04}", 42);

        // Test precision with floating point
        assert_format!("{:.2}", 42.5);
    }

    #[test]
    fn test_all_format_types() {
        // Create test values
        let integer = 42;
        let negative = -42i32;
        let float = PI;
        let string = "hello";
        let character = 'A';
        let boolean = true;
        let array = [1, 2, 3];

        // Test Display format
        assert_format!("{:}", integer);
        assert_format!("{}", boolean);

        // Test Debug format
        assert_format!("{:?}", integer);
        assert_format!("{:?}", string);
        assert_format!("{:?}", array);

        // Test LowerHex format
        assert_format!("{:x}", integer);
        assert_format!("{:x}", negative);

        // Test UpperHex format
        assert_format!("{:X}", integer);
        assert_format!("{:X}", negative);

        // Test Octal format
        assert_format!("{:o}", integer);

        // Test Binary format
        assert_format!("{:b}", integer);

        // Test LowerExp format
        assert_format!("{:e}", float);

        // Test UpperExp format
        assert_format!("{:E}", float);

        // Test Pointer format.
        let ptr = &character;
        assert_format!("{:p}", ptr);
    }

    #[test]
    fn test_precision_variations() {
        // Test precision on different types
        let float = PI;
        let string = "hello";

        // Float precision
        assert_format!("{:.3}", float);
        assert_format!("{:.1}", float);
        assert_format!("{:.0}", float);

        // String truncation
        assert_format!("{:.2}", string);
        assert_format!("{:.10}", string);

        // Width and precision together
        assert_format!("{:6.2}", float);
        assert_format!("{:<6.3}", string);

        // Dynamic precision
        let precision = 2;
        assert_format!("{:.1$}", float, precision);
    }

    #[test]
    fn test_complex_combinations() {
        // Alignment + fill + width + sign + zero-padding
        assert_format!("{:+05}", 42);
        assert_format!("{:*^+7}", 42);

        // Alignment + width + precision
        assert_format!("{:7.2}", PI);
        assert_format!("{:<7.2}", PI);

        // # + zero padding + width + type
        assert_format!("{:#06x}", 42);
        assert_format!("{:#6x}", 42);
        assert_format!("{:#06X}", 42);

        // Fill + align + zero + width + precision
        assert_format!("{:06.2}", PI);
        assert_format!("{:#>6.2}", PI);

        // Complex alignment with sign
        assert_format!("{:>+5}", 42);
        assert_format!("{:^+5}", 42);
        assert_format!("{:^5}", -42i32);
    }

    #[test]
    fn test_sign_options() {
        // Sign options with positive numbers
        assert_format!("{}", 42);
        assert_format!("{:+}", 42);
        assert_format!("{:-}", 42);

        // Sign options with negative numbers
        assert_format!("{}", -42i64);
        assert_format!("{:+}", -42i64);
        assert_format!("{:-}", -42i64);

        // Sign options with zero
        assert_format!("{}", 0);
        assert_format!("{:+}", 0);
        assert_format!("{:-}", 0);
    }

    #[test]
    fn test_nested_formats() {
        // Test nested formats where width/precision come from parameters
        let width = 10;
        let precision = 2;
        let number = PI;

        assert_format!("{:1$.2$}", number, width, precision);

        // Test width and precision that refer to other parameters
        assert_format!("{:>wi$.pre$}", number, wi = width, pre = precision);
    }

    #[test]
    fn test_alternate_forms() {
        // Test alternate forms for different types
        assert_format!("{:#b}", 42);
        assert_format!("{:#o}", 42);
        assert_format!("{:#x}", 42);
        assert_format!("{:#X}", 42);

        // Float alternate forms
        assert_format!("{:#}", 3.0);
        assert_format!("{:.0}", 3.0);
        assert_format!("{:#.0}", 3.0);
    }

    #[test]
    fn test_debug_formatting() {
        // Test various debug formatting options
        #[derive(Debug)]
        struct TestStruct {
            _field: i32,
        }

        #[derive(Debug)]
        struct TestStruct2 {
            _struct: TestStruct,
            _field: i32,
        }

        let test_struct = TestStruct2 {
            _struct: TestStruct { _field: 24 },
            _field: 42,
        };
        let test_struct_ref = &test_struct;

        // Basic debug formatting
        assert_format!("{:?}", test_struct_ref);

        // Debug with alternate form
        assert_format!("{:#?}", test_struct_ref);
        assert_format!("{:❌^#20?}", test_struct_ref);
    }

    #[test]
    fn test_unicode_fill() {
        // Test with Unicode fill characters
        assert_format!("{:♥>7}", 42);
        assert_format!("{:♥<7}", 42);
        assert_format!("{:♥^7}", 42);

        // Test with emoji
        assert_format!("{:😊^7}", 42);
    }

    #[test]
    fn test_format_errors() {
        // Test error cases
        assert!(rformat!("}", 42).is_err()); // Unmatched closing brace
        assert!(rformat!("{", 42).is_err()); // Unmatched opening brace
        assert!(rformat!("{} {}", 42).is_err()); // Out of bounds
        assert!(rformat!("{1}", 42).is_err()); // Index out of bounds
        assert!(rformat!("{unknown}", 42).is_err()); // Unknown named parameter
        assert!(rformat!("{:p}", 42).is_err()); // Unsupported format type
    }

    #[test]
    fn test_pointer() {
        let param = 42;
        let param_ref = &param;

        assert_format!("{:p}", param_ref);
    }

    #[test]
    #[should_panic]
    fn test_exception() {
        // Only place where rformat differs from format is in the default alignment of non-numerics.
        // rformat defaults to right-aligned, for both numeric and non-numeric formatters.
        // while fmt uses left-aligned formatting for non-numeric types.
        assert_format!("{:6.3}", "hi");
    }

    impl Custom for i32 {
        fn format(
            &self,
            format_spec: &FormatSpec,
            _precision: Option<usize>,
            _width: usize,
            _parameter: &Parameter,
        ) -> crate::error::Result<String> {
            match format_spec.r#type {
                crate::format_spec::Type::Custom("custom") => Ok(format!("custom: {}", self)),
                _ => Err(crate::error::FormatError::UnsupportedFormatType(
                    format_spec.r#type.to_str().to_string(),
                )),
            }
        }
    }

    #[test]
    fn test_custom() {
        assert_eq!(rformat!("{:custom}", 42i32).unwrap(), "custom: 42");
    }

    #[test]
    fn test_custom_unsupported() {
        assert!(rformat!("{:unsupported}", 42i32).is_err());
    }

    #[test]
    fn test_custom_notimplemented() {
        assert!(rformat!("{:custom}", 42u32).is_err());
    }
}