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
use core::fmt;

use crate::{parse::ParseSegment, FormatArgs, FormatError, FormatKey, ToFormatParser};

/// Preparsed formatting terms.
/// 
/// This is faster if you will be using the same format string again and again with
/// different inputs.
/// 
/// ```
/// use runtime_format::{FormatArgs, FormatKey, FormatKeyError, ParsedFmt};
/// use core::fmt;
/// # struct DateTime;
/// # impl DateTime { fn now() -> Self { Self } }
/// # impl DateTime { fn day(&self) -> i32 { 25 } fn short_month_name(&self) -> &'static str { "Jan" } fn year(&self) -> i32 { 2023 } }
/// # impl DateTime { fn hours(&self) -> i32 { 16 } fn minutes(&self) -> i32 { 27 } fn seconds(&self) -> i32 { 53 } }
/// impl FormatKey for DateTime {
///     fn fmt(&self, key: &str, f: &mut fmt::Formatter<'_>) -> Result<(), FormatKeyError> {
///          // ...
/// #        use core::fmt::Write;
/// #        match key {
/// #            "year"    => write!(f, "{}", self.year()).map_err(FormatKeyError::Fmt),
/// #            "month"   => write!(f, "{}", self.short_month_name()).map_err(FormatKeyError::Fmt),
/// #            "day"     => write!(f, "{}", self.day()).map_err(FormatKeyError::Fmt),
/// #            "hours"   => write!(f, "{}", self.hours()).map_err(FormatKeyError::Fmt),
/// #            "minutes" => write!(f, "{}", self.minutes()).map_err(FormatKeyError::Fmt),
/// #            "seconds" => write!(f, "{}", self.seconds()).map_err(FormatKeyError::Fmt),
/// #            _ => Err(FormatKeyError::UnknownKey),
/// #        }
///     }
/// }
///
/// let now = DateTime::now();
/// let fmt = ParsedFmt::new("{month} {day} {year} {hours}:{minutes}:{seconds}").unwrap();
/// let args = FormatArgs::new(&fmt, &now);
/// let expected = "Jan 25 2023 16:27:53";
/// assert_eq!(args.to_string(), expected);
pub struct ParsedFmt<'a> {
    segments: tinyvec::TinyVec<[ParseSegment<'a>; 8]>,
}

impl<'a> ToFormatParser<'a> for ParsedFmt<'a> {
    type Parser = std::iter::Copied<std::slice::Iter<'a, ParseSegment<'a>>>;

    fn to_parser(&'a self) -> Self::Parser {
        self.segments.iter().copied()
    }

    fn unparsed(_: Self::Parser) -> &'a str {
        ""
    }
}

impl fmt::Debug for ParsedFmt<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CompiledFormatter")
            .field("segments", &self.segments)
            .finish()
    }
}

impl<'a> ParsedFmt<'a> {
    /// Parse the given format string.
    ///
    /// # Errors
    /// If the string could not be parsed, or there is a key that is unacceptable.
    pub fn new(s: &'a str) -> Result<Self, FormatError<'a>> {
        let mut segments = s.to_parser();
        let this = Self {
            segments: segments.by_ref().collect(),
        };

        if !segments.s.is_empty() {
            Err(FormatError::Parse(segments.s))
        } else {
            Ok(this)
        }
    }

    /// Return the keys that will be used when formatting.
    /// 
    /// ```
    /// # use runtime_format::ParsedFmt;
    /// let fmt = "Hello, {recipient}. Hope you are having a nice {time_descriptor}.";
    /// let parsed = ParsedFmt::new(fmt).unwrap();
    /// let keys: Vec<_> = parsed.keys().collect();
    /// assert_eq!(keys, ["recipient", "time_descriptor"]);
    /// ```
    pub fn keys(&self) -> impl Iterator<Item = &'_ str> {
        self.segments.iter().filter_map(|segment| match segment {
            ParseSegment::Literal(_) => None,
            ParseSegment::Key(key) => Some(*key),
        })
    }

    /// Combine this parsed format with the given values into a [`FormatArgs`]
    pub fn with_args<'b, F: FormatKey>(&'b self, fmt: &'b F) -> FormatArgs<'b, Self, F> {
        FormatArgs::new(self, fmt)
    }
}

impl<'a> TryFrom<&'a str> for ParsedFmt<'a> {
    type Error = FormatError<'a>;

    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

#[cfg(test)]
mod tests {
    use core::fmt;

    use crate::{FormatError, FormatKey, FormatKeyError};

    use super::ParsedFmt;

    struct Message;
    impl FormatKey for Message {
        fn fmt(&self, key: &str, f: &mut fmt::Formatter<'_>) -> Result<(), FormatKeyError> {
            match key {
                "recipient" => f.write_str("World").map_err(FormatKeyError::Fmt),
                "time_descriptor" => f.write_str("morning").map_err(FormatKeyError::Fmt),
                _ => Err(FormatKeyError::UnknownKey),
            }
        }
    }

    #[test]
    fn compiled_happy_path() {
        let formatter =
            ParsedFmt::new("Hello, {recipient}. Hope you are having a nice {time_descriptor}.")
                .unwrap();
        let expected = "Hello, World. Hope you are having a nice morning.";
        assert_eq!(formatter.with_args(&Message).to_string(), expected);
    }

    #[test]
    fn compiled_failed_parsing() {
        let err =
            ParsedFmt::new("Hello, {recipient}. Hope you are having a nice {time_descriptor.")
                .unwrap_err();
        assert_eq!(err, FormatError::Parse("time_descriptor."));
    }

    #[test]
    fn compiled_keys() {
        let parsed =
            ParsedFmt::new("Hello, {recipient}. Hope you are having a nice {time_descriptr}.")
                .unwrap();
        let keys: Vec<_> = parsed.keys().collect();
        assert_eq!(keys, ["recipient", "time_descriptr"]);
    }
}