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
use std::{iter::Peekable, str::Chars};

use thiserror::Error;
use unicode_segmentation::UnicodeSegmentation;

use crate::{constant_pool::ConstantPoolBuilder, ConstantIndex};

/// The formatting options that are available for interpolated strings
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct StringFormatOptions {
    /// The alignment that padded strings should use
    pub alignment: StringAlignment,
    /// The minimum width that should be taken up by the string
    pub min_width: Option<u32>,
    /// The number of decimal places to use when formatting floats
    pub precision: Option<u32>,
    /// The character that padded strings should use to fill empty space
    pub fill_character: Option<ConstantIndex>,
}

impl StringFormatOptions {
    /// Parses a format string
    pub(crate) fn parse(
        format_string: &str,
        constants: &mut ConstantPoolBuilder,
    ) -> Result<Self, StringFormatError> {
        use FormatParsePosition::*;
        let mut position = Start;
        let mut result = Self::default();
        let mut chars = format_string.chars().peekable();

        let char_to_alignment = |c: char| match c {
            '<' => StringAlignment::Left,
            '^' => StringAlignment::Center,
            '>' => StringAlignment::Right,
            _ => unreachable!(),
        };

        let mut add_string_constant = |s: &str| {
            constants
                .add_string(s)
                .map_err(|_| StringFormatError::InternalError)
        };

        while let Some(next) = chars.next() {
            match (next, chars.peek(), position) {
                // Check for single-char fill character at the start of the string
                (_, Some('<' | '^' | '>'), Start) => {
                    result.fill_character =
                        Some(add_string_constant(&format_string[0..next.len_utf8()])?);
                    result.alignment = char_to_alignment(chars.next().unwrap());
                    position = MinWidth;
                }
                ('<' | '^' | '>', _, Start | Alignment) => {
                    result.alignment = char_to_alignment(next);
                    position = MinWidth;
                }
                ('0', Some('0'..='9'), Start | MinWidth) => {
                    result.fill_character = Some(add_string_constant("0")?);
                    position = MinWidth;
                }
                ('0'..='9', _, Start | MinWidth) => {
                    result.min_width = Some(consume_u32(next, &mut chars)?);
                    position = Precision;
                }
                ('.', Some(_), Start | MinWidth | Precision) => {
                    let first_digit = chars.next().unwrap();
                    result.precision = Some(consume_u32(first_digit, &mut chars)?);
                    position = End;
                }
                (_, _, Start) => {
                    // Unwrapping here is fine, format_string is valid UTF-8
                    let fill = format_string.graphemes(true).next().unwrap();
                    // The fill grapheme cluster can only appear at the start of the format string
                    chars = format_string[fill.len()..].chars().peekable();
                    result.fill_character = Some(add_string_constant(fill)?);
                    position = Alignment;
                }
                (other, _, _) => {
                    return Err(StringFormatError::UnexpectedToken(other));
                }
            }
        }

        Ok(result)
    }
}

// Used during parsing of a format string, see [StringFormatOptions::parse]
#[derive(Copy, Clone, Debug)]
enum FormatParsePosition {
    Start,
    Alignment,
    MinWidth,
    Precision,
    End,
}

fn consume_u32(first: char, chars: &mut Peekable<Chars>) -> Result<u32, StringFormatError> {
    let mut n = first
        .to_digit(10)
        .ok_or(StringFormatError::ExpectedNumber(first))? as u64;
    let index_max = u32::MAX as u64;

    while let Some(n_next @ '0'..='9') = chars.peek().cloned() {
        chars.next();

        n *= 10;
        n += n_next
            .to_digit(10)
            .ok_or(StringFormatError::ExpectedNumber(first))? as u64;

        if n > index_max {
            return Err(StringFormatError::FormatNumberIsTooLarge(n));
        }
    }

    Ok(n as u32)
}

/// Alignment options for formatted strings
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
#[allow(missing_docs)]
#[repr(u8)]
pub enum StringAlignment {
    /// Default alignment is right-aligned for numbers, left-aligned otherwise
    #[default]
    Default,
    Left,
    Center,
    Right,
}

/// An error that represents a problem with the Parser's internal logic, rather than a user error
#[derive(Error, Clone, Debug)]
#[allow(missing_docs)]
pub enum StringFormatError {
    #[error("Expected a number '{0}'")]
    ExpectedNumber(char),
    #[error("{0} is larger than the maximum of {}", u32::MAX)]
    FormatNumberIsTooLarge(u64),
    #[error("An unexpected internal error occurred")]
    InternalError,
    #[error("Unexpected token '{0}'")]
    UnexpectedToken(char),
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_parse_format_string(cases: &[(&str, StringFormatOptions)]) {
        for (options, expected) in cases {
            let mut constants = ConstantPoolBuilder::default();
            assert_eq!(
                *expected,
                StringFormatOptions::parse(options, &mut constants).unwrap()
            );
        }
    }

    #[test]
    fn width_and_precision() {
        test_parse_format_string(&[
            (
                "10",
                StringFormatOptions {
                    min_width: Some(10),
                    ..Default::default()
                },
            ),
            (
                "08",
                StringFormatOptions {
                    fill_character: Some(0),
                    min_width: Some(8),
                    ..Default::default()
                },
            ),
            (
                ".12",
                StringFormatOptions {
                    precision: Some(12),
                    ..Default::default()
                },
            ),
            (
                "5.9",
                StringFormatOptions {
                    min_width: Some(5),
                    precision: Some(9),
                    ..Default::default()
                },
            ),
        ])
    }

    #[test]
    fn fill_and_alignment() {
        test_parse_format_string(&[
            (
                "_^",
                StringFormatOptions {
                    alignment: StringAlignment::Center,
                    fill_character: Some(0),
                    ..Default::default()
                },
            ),
            (
                "πœ‡<.9",
                StringFormatOptions {
                    alignment: StringAlignment::Left,
                    fill_character: Some(0),
                    precision: Some(9),
                    ..Default::default()
                },
            ),
            (
                "🫢🏽>20.10",
                StringFormatOptions {
                    alignment: StringAlignment::Right,
                    fill_character: Some(0),
                    min_width: Some(20),
                    precision: Some(10),
                },
            ),
            (
                "<.8",
                StringFormatOptions {
                    alignment: StringAlignment::Left,
                    precision: Some(8),
                    ..Default::default()
                },
            ),
            (
                "}>2",
                StringFormatOptions {
                    alignment: StringAlignment::Right,
                    fill_character: Some(0),
                    min_width: Some(2),
                    ..Default::default()
                },
            ),
            (
                "8^4",
                StringFormatOptions {
                    alignment: StringAlignment::Center,
                    fill_character: Some(0),
                    min_width: Some(4),
                    ..Default::default()
                },
            ),
        ])
    }
}