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
//! Provides types associated to compile-time formatting.

use core::fmt;

/// Trait for custom formatting with compile-time format checking
pub trait CustomFormat<const SPEC: u128> {
    /// Formats the value using the given formatter.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use custom_format::compile_time::{self as cfmt, spec, CustomFormat};
    ///
    /// use core::fmt;
    ///
    /// #[derive(Debug)]
    /// struct Hex(u8);
    ///
    /// impl CustomFormat<{ spec("x") }> for Hex {
    ///     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    ///         write!(f, "{:#02x}", self.0)
    ///     }
    /// }
    ///
    /// impl CustomFormat<{ spec("X") }> for Hex {
    ///     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    ///         write!(f, "{:#02X}", self.0)
    ///     }
    /// }
    ///
    /// assert_eq!(cfmt::format!("{0:X?}, {0 :x}, {0 :X}", Hex(0xAB)), "Hex(AB), 0xab, 0xAB");
    /// ```
    ///
    /// The following statement doesn't compile since `"z"` is not a valid format specifier:
    ///
    /// ```rust,compile_fail
    /// # use custom_format::compile_time::{self as cfmt, CustomFormat};
    /// # use custom_format::custom_formatter;
    /// # use core::fmt;
    /// # struct Hex(u8);
    /// # impl CustomFormat<{ cfmt::spec("x") }> for Hex {
    /// #     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    /// #         write!(f, "{:#02x}", self.0)
    /// #     }
    /// # }
    /// # impl CustomFormat<{ cfmt::spec("X") }> for Hex {
    /// #     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    /// #         write!(f, "{:#02X}", self.0)
    /// #     }
    /// # }
    /// cfmt::println!("{ :z}", Hex(0));
    /// ```
    ///
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result;
}

/// Wrapper for custom formatting via its [`Display`](core::fmt::Display) trait.
///
/// The format specifier is a const-generic parameter and is part of the type.
///
#[derive(Debug, Clone)]
pub struct CustomFormatter<'a, T, const SPEC: u128> {
    /// Value to format
    value: &'a T,
}

impl<'a, T, const SPEC: u128> CustomFormatter<'a, T, SPEC> {
    /// Construct a new [`CustomFormatter`] value
    pub fn new(value: &'a T) -> Self {
        Self { value }
    }
}

/// Helper macro for constructing a new [`CustomFormatter`] value from a format specifier
#[macro_export]
macro_rules! custom_formatter {
    ($spec:literal, $value:expr) => {{
        $crate::compile_time::CustomFormatter::<_, { $crate::compile_time::spec($spec) }>::new($value)
    }};
}
pub use custom_formatter;

impl<T: CustomFormat<SPEC>, const SPEC: u128> fmt::Display for CustomFormatter<'_, T, SPEC> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        CustomFormat::fmt(self.value, f)
    }
}

/// Convert a format specifier to a [`u128`], used as a const-generic parameter
pub const fn spec(s: &str) -> u128 {
    let bytes = s.as_bytes();
    let len = s.len();

    if len > 16 {
        #[allow(unconditional_panic)]
        let _ = ["format specifier is limited to 16 bytes"][usize::MAX];
    }

    let mut result = [0u8; 16];

    let mut i = 0;
    while i < len {
        result[i] = bytes[i];
        i += 1;
    }

    u128::from_le_bytes(result)
}

pub use custom_format_macros::compile_time_eprint as eprint;
pub use custom_format_macros::compile_time_eprintln as eprintln;
pub use custom_format_macros::compile_time_format as format;
pub use custom_format_macros::compile_time_format_args as format_args;
pub use custom_format_macros::compile_time_panic as panic;
pub use custom_format_macros::compile_time_print as print;
pub use custom_format_macros::compile_time_println as println;
pub use custom_format_macros::compile_time_write as write;
pub use custom_format_macros::compile_time_writeln as writeln;