omnicoach 0.3.0

Lightweight, high-signal static analysis and command linter for Python and Rust
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
//! Python format strings: f-strings, `str.format` templates and `%` (printf-style) templates,
//! split into literal text and replacement fields, plus PEP 3101 field-name parsing.

use super::logging::extract_logger_call;
use crate::code_lint::ast::tree::{AstNode, ParsedFile, span_from_ruff_range};
use ruff_python_ast::visitor::source_order::{SourceOrderVisitor, walk_arguments, walk_expr};
use ruff_python_ast::{
    Expr, FString, FStringPartRef, InterpolatedElement, InterpolatedStringElement, Operator,
    StringFlags as _, StringLiteral,
};
use ruff_text_size::{Ranged as _, TextRange};

/// `%` conversion types accepted by printf-style formatting.
const PRINTF_CONVERSIONS: &[u8] = b"diouxXeEfFgGcrsa";

/// How a Python string literal is formatted at runtime.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PythonFormatStyle {
    /// An `f` / `F` prefixed literal (`f"... {x} ..."`).
    FString,
    /// The template of `.format(...)`, `.format_map(...)` or `str.format(...)`.
    StrFormat,
    /// The left operand of `%`, or a logger message followed by format arguments.
    Printf,
}

/// One replacement field of a format string.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PythonFormatPlaceholder {
    /// The field as written in source (`{name!r:>8}`, `%(key)s`, `%5d`).
    pub text: String,
    /// The f-string expression, `str.format` field name or `%` mapping key; empty when absent
    /// (`{}`, `%s`).
    pub field: String,
    /// The conversion after `!` in braces (`r`, `s`, `a`), or the `%` conversion type (`s`, `d`).
    pub conversion: Option<String>,
    /// The format spec after `:` in braces, or the `%` flags, width, precision and length.
    pub format_spec: Option<String>,
    /// Whether an f-string field is self-documenting (`{value=}`).
    pub is_self_documenting: bool,
}

/// A formatted Python `string` literal, split into literal text and replacement fields.
///
/// All text is source text: escape sequences are not decoded.
pub struct PythonFormatString<'a> {
    /// The `string` node.
    pub node: AstNode<'a>,
    /// How the literal is formatted.
    pub style: PythonFormatStyle,
    /// Literal text of the strings before `node` in an implicit concatenation
    /// (`"Failed for " f"'{name}'"`), without f-string fields.
    pub preceding_text: String,
    /// Literal text around the fields of `node`: `literals[i]` precedes `placeholders[i]` and
    /// the last entry follows the last field, so there is one more literal than placeholders.
    pub literals: Vec<String>,
    /// The replacement fields of `node`, in source order.
    pub placeholders: Vec<PythonFormatPlaceholder>,
}

/// Collects the formatted string literals in `file`, in source order. Raw, byte and
/// unformatted string literals are skipped.
#[must_use]
pub fn collect_format_strings(file: &ParsedFile) -> Vec<PythonFormatString<'_>> {
    let Some(parsed) = file.py_module() else {
        return Vec::new();
    };
    let mut visitor = FormatStringVisitor {
        file,
        context_style: None,
        call_arg_target: None,
        out: Vec::new(),
    };
    visitor.visit_body(&parsed.syntax().body);
    visitor.out
}

struct FormatStringVisitor<'a> {
    file: &'a ParsedFile,
    context_style: Option<PythonFormatStyle>,
    call_arg_target: Option<(TextRange, PythonFormatStyle)>,
    out: Vec<PythonFormatString<'a>>,
}

impl FormatStringVisitor<'_> {
    fn visit_fstring(
        &mut self,
        expr_fstring: &ruff_python_ast::ExprFString,
        current_style: Option<PythonFormatStyle>,
    ) {
        let mut preceding_text = String::new();
        for part in &expr_fstring.value {
            match part {
                FStringPartRef::Literal(lit) => {
                    if let Some(style) = current_style {
                        self.push_plain_part(lit, style, &mut preceding_text);
                    } else {
                        let content_range = lit.content_range();
                        preceding_text.push_str(
                            &self.file.source[usize::from(content_range.start())
                                ..usize::from(content_range.end())],
                        );
                    }
                }
                FStringPartRef::FString(fstring) => {
                    let TemplateParts {
                        literals,
                        fields: interpolations,
                    } = fstring_segments_and_interpolations(fstring, &self.file.source);
                    if !fstring.flags.prefix().is_raw() {
                        let placeholders = interpolations
                            .iter()
                            .map(|interp| fstring_placeholder(interp, &self.file.source))
                            .collect();
                        self.out.push(PythonFormatString {
                            node: AstNode::from_span(
                                self.file,
                                span_from_ruff_range(fstring.range),
                            ),
                            style: PythonFormatStyle::FString,
                            preceding_text: preceding_text.clone(),
                            literals: literals.clone(),
                            placeholders,
                        });
                    }
                    for segment in &literals {
                        preceding_text.push_str(segment);
                    }
                    for interpolation in interpolations {
                        self.visit_expr(&interpolation.expression);
                        if let Some(spec) = &interpolation.format_spec {
                            for element in &spec.elements {
                                self.visit_interpolated_string_element(element);
                            }
                        }
                    }
                }
            }
        }
    }

    fn visit_call(&mut self, call: &ruff_python_ast::ExprCall) {
        if let Expr::Attribute(attr) = call.func.as_ref()
            && matches!(attr.attr.as_str(), "format" | "format_map")
        {
            self.context_style = Some(PythonFormatStyle::StrFormat);
            self.visit_expr(&attr.value);
            self.context_style = None;
        } else {
            self.visit_expr(&call.func);
        }

        let func_span = span_from_ruff_range(call.func.range());
        let func_text = &self.file.source[func_span.start..func_span.end];
        let target = if func_text == "str.format" {
            call.arguments
                .args
                .iter()
                .find(|arg| !matches!(arg, Expr::Starred(_)))
                .map(|arg| (arg.range(), PythonFormatStyle::StrFormat))
        } else {
            extract_logger_call(call, self.file)
                .filter(|lc| lc.uses_printf && lc.has_trailing_positional_args)
                .map(|lc| (lc.message_range, PythonFormatStyle::Printf))
        };

        let prev_target = self.call_arg_target;
        self.call_arg_target = target;
        walk_arguments(self, &call.arguments);
        self.call_arg_target = prev_target;
    }

    fn push_plain_part(
        &mut self,
        part: &StringLiteral,
        style: PythonFormatStyle,
        preceding_text: &mut String,
    ) {
        let content_range = part.content_range();
        let content =
            &self.file.source[usize::from(content_range.start())..usize::from(content_range.end())];
        if !part.flags.prefix().is_raw() {
            let TemplateParts {
                literals,
                fields: placeholders,
            } = match style {
                PythonFormatStyle::StrFormat => split_brace_fields(content),
                PythonFormatStyle::Printf => split_printf_fields(content),
                PythonFormatStyle::FString => unreachable!(),
            };
            self.out.push(PythonFormatString {
                node: AstNode::from_span(self.file, span_from_ruff_range(part.range)),
                style,
                preceding_text: preceding_text.clone(),
                literals,
                placeholders,
            });
        }
        preceding_text.push_str(content);
    }
}

impl SourceOrderVisitor<'_> for FormatStringVisitor<'_> {
    fn visit_expr(&mut self, expr: &Expr) {
        let current_style = self.context_style.take().or_else(|| {
            self.call_arg_target
                .filter(|(range, _)| *range == expr.range())
                .map(|(_, style)| style)
        });

        match expr {
            Expr::StringLiteral(string_literal) => {
                if let Some(style) = current_style {
                    let mut preceding_text = String::new();
                    for part in string_literal.value.as_slice() {
                        self.push_plain_part(part, style, &mut preceding_text);
                    }
                }
            }
            Expr::FString(expr_fstring) => self.visit_fstring(expr_fstring, current_style),
            Expr::Call(call) => self.visit_call(call),
            Expr::BinOp(bin_op) => {
                self.context_style =
                    (bin_op.op == Operator::Mod).then_some(PythonFormatStyle::Printf);
                self.visit_expr(&bin_op.left);
                self.context_style = None;
                self.visit_expr(&bin_op.right);
            }
            _ => {
                walk_expr(self, expr);
            }
        }
    }
}

/// Describes an f-string `InterpolatedElement` node.
fn fstring_placeholder(
    interpolation: &InterpolatedElement,
    source: &str,
) -> PythonFormatPlaceholder {
    let text_span = span_from_ruff_range(interpolation.range);
    let expr_span = span_from_ruff_range(interpolation.expression.range());
    PythonFormatPlaceholder {
        text: source[text_span.start..text_span.end].to_owned(),
        field: source[expr_span.start..expr_span.end].trim().to_owned(),
        conversion: interpolation.conversion.to_char().map(String::from),
        format_spec: interpolation.format_spec.as_ref().map(|spec| {
            let spec_span = span_from_ruff_range(spec.range);
            let spec_text = &source[spec_span.start..spec_span.end];
            spec_text.strip_prefix(':').unwrap_or(spec_text).to_owned()
        }),
        is_self_documenting: interpolation.debug_text.is_some(),
    }
}

/// Template text split into literal segments around its replacement fields.
struct TemplateParts<F> {
    /// Literal text around `fields`: `literals[i]` precedes `fields[i]` and the last entry
    /// follows the last field, so there is one more literal than fields.
    literals: Vec<String>,
    /// The replacement fields, in source order.
    fields: Vec<F>,
}

/// Splits an `FString` node into its literal text segments (between delimiters and
/// `InterpolatedElement` children) and its `InterpolatedElement` nodes, using raw source byte
/// offsets so escape sequences are not decoded.
fn fstring_segments_and_interpolations<'a>(
    fstring: &'a FString,
    source: &str,
) -> TemplateParts<&'a InterpolatedElement> {
    let content_start = usize::from(fstring.range.start() + fstring.flags.opener_len());
    let content_end = usize::from(fstring.range.end() - fstring.flags.closer_len());

    let interpolations: Vec<&'a InterpolatedElement> = fstring
        .elements
        .iter()
        .filter_map(|element| match element {
            InterpolatedStringElement::Interpolation(interpolation) => Some(interpolation),
            InterpolatedStringElement::Literal(_) => None,
        })
        .collect();

    let mut segments = Vec::with_capacity(interpolations.len() + 1);
    let mut cursor = content_start;
    for interpolation in &interpolations {
        let interp_start = usize::from(interpolation.range.start());
        let interp_end = usize::from(interpolation.range.end());
        segments.push(
            source
                .get(cursor..interp_start)
                .unwrap_or_default()
                .to_owned(),
        );
        cursor = interp_end;
    }
    segments.push(
        source
            .get(cursor..content_end)
            .unwrap_or_default()
            .to_owned(),
    );
    TemplateParts {
        literals: segments,
        fields: interpolations,
    }
}

/// Splits `str.format` template text into literals and `{...}` fields. Escaped `{{` and `}}`
/// stay in the literals.
fn split_brace_fields(content: &str) -> TemplateParts<PythonFormatPlaceholder> {
    let bytes = content.as_bytes();
    let mut literals = Vec::new();
    let mut placeholders = Vec::new();
    let mut literal_start = 0;
    let mut index = 0;
    while index < bytes.len() {
        let current = bytes[index];
        let is_escaped = (current == b'{' && bytes.get(index + 1) == Some(&b'{'))
            || (current == b'}' && bytes.get(index + 1) == Some(&b'}'));
        if is_escaped {
            index += 2;
            continue;
        }
        if current == b'{'
            && let Some(close_index) = find_brace_field_end(bytes, index)
        {
            literals.push(content[literal_start..index].to_owned());
            placeholders.push(brace_placeholder(&content[index..=close_index]));
            index = close_index + 1;
            literal_start = index;
            continue;
        }
        index += 1;
    }
    literals.push(content[literal_start..].to_owned());
    TemplateParts {
        literals,
        fields: placeholders,
    }
}

/// Finds the closing `}` byte offset of a `{...}` field opening at `open_index`, returning
/// `None` if an inner `{` comes first.
fn find_brace_field_end(bytes: &[u8], open_index: usize) -> Option<usize> {
    let mut scan = open_index + 1;
    while scan < bytes.len() {
        let current = bytes[scan];
        if current == b'}' && bytes.get(scan + 1) != Some(&b'}') {
            return Some(scan);
        }
        if current == b'{' {
            return None;
        }
        scan += 1;
    }
    None
}

/// Describes a `{field!conversion:format_spec}` field written as `text`.
///
/// The field name ends at the first `:`, so a `:` inside an index (`{a[:]}`) starts the spec.
fn brace_placeholder(text: &str) -> PythonFormatPlaceholder {
    let inner = &text[1..text.len() - 1];
    let (header, format_spec) = match inner.split_once(':') {
        Some((header, format_spec)) => (header, Some(format_spec.to_owned())),
        None => (inner, None),
    };
    let (field, conversion) = match header.split_once('!') {
        Some((field, conversion)) => (field, Some(conversion.to_owned())),
        None => (header, None),
    };
    PythonFormatPlaceholder {
        text: text.to_owned(),
        field: field.to_owned(),
        conversion,
        format_spec,
        is_self_documenting: false,
    }
}

/// Splits printf-style template text into literals and `%` fields. Escaped `%%` and `%`
/// signs that start no valid field stay in the literals.
fn split_printf_fields(content: &str) -> TemplateParts<PythonFormatPlaceholder> {
    let bytes = content.as_bytes();
    let mut literals = Vec::new();
    let mut placeholders = Vec::new();
    let mut literal_start = 0;
    let mut index = 0;
    while index < bytes.len() {
        if bytes[index] != b'%' {
            index += 1;
            continue;
        }
        if bytes.get(index + 1) == Some(&b'%') {
            index += 2;
            continue;
        }
        if let Some(placeholder) = parse_printf_field(content, index) {
            literals.push(content[literal_start..index].to_owned());
            index += placeholder.text.len();
            placeholders.push(placeholder);
            literal_start = index;
            continue;
        }
        index += 1;
    }
    literals.push(content[literal_start..].to_owned());
    TemplateParts {
        literals,
        fields: placeholders,
    }
}

/// Parses the `%[(key)][flags][width][.precision][length]type` field starting at
/// `percent_index`; its `text` spans `content` from `percent_index` to the end of the field.
fn parse_printf_field(content: &str, percent_index: usize) -> Option<PythonFormatPlaceholder> {
    let bytes = content.as_bytes();
    let mut cursor = percent_index + 1;
    let mut field = "";
    if bytes.get(cursor) == Some(&b'(') {
        let close_offset = content[cursor + 1..].find(')')?;
        field = &content[cursor + 1..cursor + 1 + close_offset];
        cursor += close_offset + 2;
    }
    let spec_start = cursor;
    while bytes
        .get(cursor)
        .is_some_and(|byte| b"#0- +".contains(byte))
    {
        cursor += 1;
    }
    skip_printf_count(bytes, &mut cursor);
    if bytes.get(cursor) == Some(&b'.') {
        cursor += 1;
        skip_printf_count(bytes, &mut cursor);
    }
    while bytes.get(cursor).is_some_and(|byte| b"hlL".contains(byte)) {
        cursor += 1;
    }
    let conversion = *bytes.get(cursor)?;
    if !PRINTF_CONVERSIONS.contains(&conversion) {
        return None;
    }
    let end = cursor + 1;
    Some(PythonFormatPlaceholder {
        text: content[percent_index..end].to_owned(),
        field: field.to_owned(),
        conversion: Some(char::from(conversion).to_string()),
        format_spec: (cursor > spec_start).then(|| content[spec_start..cursor].to_owned()),
        is_self_documenting: false,
    })
}

/// Advances `cursor` past a printf width or precision (`*` or digits).
fn skip_printf_count(bytes: &[u8], cursor: &mut usize) {
    if bytes.get(*cursor) == Some(&b'*') {
        *cursor += 1;
        return;
    }
    while bytes.get(*cursor).is_some_and(u8::is_ascii_digit) {
        *cursor += 1;
    }
}

// `None` rather than an empty list: `str.format` raises on unbalanced braces, so callers must be
// able to tell an invalid template apart from a valid one with no named fields.
/// Returns the identifier roots of the named PEP 3101 replacement fields in `message`.
///
/// Roots come in source order (`order` for `{order.id}`, `width` for `{:>{width}}`); the result
/// is `None` if `message` has unbalanced braces. Positional fields (`{}`, `{0}`) have no named root.
#[must_use]
pub fn named_format_field_roots(message: &str) -> Option<Vec<String>> {
    let mut cursor = 0;
    let mut roots = Vec::new();
    while cursor < message.len() {
        let rest = &message[cursor..];
        if rest.starts_with("{{") || rest.starts_with("}}") {
            cursor += 2;
        } else if rest.starts_with('}') {
            return None;
        } else if rest.starts_with('{') {
            let field = parse_replacement_field(message, cursor + 1)?;
            roots.extend(
                field
                    .roots
                    .into_iter()
                    .filter(|root| is_python_identifier(root))
                    .map(str::to_owned),
            );
            cursor = field.end;
        } else {
            cursor += rest.chars().next()?.len_utf8();
        }
    }
    Some(roots)
}

/// Validates a PEP 3101 `field_name` (`arg_name("." attribute | "[" index "]")*`) and returns
/// its root `arg_name` slice.
#[must_use]
pub fn extract_valid_field_root(field_name: &str) -> Option<&str> {
    let split_at = field_name.find(['.', '[']).unwrap_or(field_name.len());
    let root = &field_name[..split_at];
    let valid_root = root.is_empty()
        || root.chars().all(|character| character.is_ascii_digit())
        || is_python_identifier(root);
    if !valid_root {
        return None;
    }

    let mut tail = &field_name[split_at..];
    while !tail.is_empty() {
        if let Some(after_dot) = tail.strip_prefix('.') {
            let end = after_dot.find(['.', '[']).unwrap_or(after_dot.len());
            if !is_python_identifier(&after_dot[..end]) {
                return None;
            }
            tail = &after_dot[end..];
        } else {
            let after_bracket = tail.strip_prefix('[')?;
            let close = after_bracket.find(']')?;
            if close == 0 {
                return None;
            }
            tail = &after_bracket[close + 1..];
        }
    }
    Some(root)
}

/// The root `arg_name`s of one parsed `{...}` replacement field.
struct ReplacementFieldRoots<'a> {
    /// The byte index after the field's closing `}`.
    end: usize,
    /// The root `arg_name`s found inside the field, in source order.
    roots: Vec<&'a str>,
}

/// Parses one `{...}` replacement field whose body starts at `start` (immediately after `{`).
fn parse_replacement_field(message: &str, start: usize) -> Option<ReplacementFieldRoots<'_>> {
    let mut cursor = start;
    let mut in_brackets = false;
    let mut delimiter = None;

    while cursor < message.len() {
        let character = message[cursor..].chars().next()?;
        match character {
            '[' if !in_brackets => in_brackets = true,
            ']' if in_brackets => in_brackets = false,
            '{' if !in_brackets => return None,
            ':' | '}' if !in_brackets => {
                delimiter = Some((cursor, character));
                break;
            }
            _ => {}
        }
        cursor += character.len_utf8();
    }

    let (delimiter_index, delimiter_char) = delimiter?;
    let header = &message[start..delimiter_index];
    let field_name = match header.split_once('!') {
        Some((before, "r" | "s" | "a")) => Some(before),
        Some(_) => None,
        None => Some(header),
    };

    let mut roots = Vec::new();
    if let Some(root) = field_name.and_then(extract_valid_field_root) {
        roots.push(root);
    }

    if delimiter_char == '}' {
        return Some(ReplacementFieldRoots {
            end: delimiter_index + 1,
            roots,
        });
    }
    let next_cursor = parse_format_spec_section(message, delimiter_index + 1, &mut roots)?;
    Some(ReplacementFieldRoots {
        end: next_cursor,
        roots,
    })
}

/// Parses the `:format_spec` portion of a PEP 3101 replacement field starting at `start`,
/// appending any nested `{nested_field}` root names to `roots` and returning the byte index
/// immediately after the outer closing `}`.
fn parse_format_spec_section<'a>(
    message: &'a str,
    start: usize,
    roots: &mut Vec<&'a str>,
) -> Option<usize> {
    let mut cursor = start;
    while cursor < message.len() {
        let rest = &message[cursor..];
        if rest.starts_with('}') {
            return Some(cursor + 1);
        }
        if rest.starts_with('{') {
            let after_open = &message[cursor + 1..];
            let close_offset = after_open.find('}')?;
            let nested_body = &after_open[..close_offset];
            if nested_body.contains('{') {
                return None;
            }
            let nested_field = nested_body
                .split_once('!')
                .map_or(nested_body, |(before, _)| before);
            if let Some(nested_root) = extract_valid_field_root(nested_field) {
                roots.push(nested_root);
            }
            cursor += 1 + close_offset + 1;
        } else {
            cursor += rest.chars().next()?.len_utf8();
        }
    }
    None
}

/// Returns true if `name` is a valid Python identifier (`order_id`, `_item2`, `café`).
fn is_python_identifier(name: &str) -> bool {
    let mut characters = name.chars();
    let Some(first) = characters.next() else {
        return false;
    };
    let valid_start = first == '_' || first.is_alphabetic();
    valid_start
        && characters.all(|character| character == '_' || character.is_alphanumeric())
        && !first.is_ascii_digit()
}

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

    #[rstest::rstest]
    #[case::simple("Order {order_id} filled", Some(&["order_id"][..]))]
    #[case::compound_attribute("Order {order.id} filled", Some(&["order"][..]))]
    #[case::compound_subscript("Order {order[id]} filled", Some(&["order"][..]))]
    #[case::conversion_and_spec("Order {order_id!r} {amount:.2f}", Some(&["order_id", "amount"][..]))]
    #[case::positional_empty_and_numbered("Order {} and {0} and {0.id} and {1[key]}", Some(&[][..]))]
    #[case::escaped_double_braces("Literal {{order_id}} value {}", Some(&[][..]))]
    #[case::triple_braces_captures_inner("Literal {{{order_id}}}", Some(&["order_id"][..]))]
    #[case::nested_format_spec("Value {:>{width}}", Some(&["width"][..]))]
    #[case::non_identifier_braces("Payload {\"order_id\": 1} and {a, b}", Some(&[][..]))]
    #[case::unclosed_opening_brace("Malformed {order_id in input", None)]
    #[case::unmatched_closing_brace("Malformed {order_id} stray }", None)]
    fn test_named_format_field_roots(#[case] message: &str, #[case] expected: Option<&[&str]>) {
        let expected = expected.map(|roots| roots.iter().map(|root| (*root).to_owned()).collect());
        assert_eq!(named_format_field_roots(message), expected);
    }

    /// `(style, preceding_text, literals, [(field, conversion, format_spec)])`.
    type FormatStringSummary = (
        PythonFormatStyle,
        String,
        Vec<String>,
        Vec<(String, Option<String>, Option<String>)>,
    );

    fn summarize_format_strings(python_code: &str) -> Vec<FormatStringSummary> {
        let file = ParsedFile::new(python_code, Language::Python);
        collect_format_strings(&file)
            .into_iter()
            .map(|format_string| {
                let placeholders = format_string
                    .placeholders
                    .into_iter()
                    .map(|placeholder| {
                        (
                            placeholder.field,
                            placeholder.conversion,
                            placeholder.format_spec,
                        )
                    })
                    .collect();
                (
                    format_string.style,
                    format_string.preceding_text,
                    format_string.literals,
                    placeholders,
                )
            })
            .collect()
    }

    fn owned(texts: &[&str]) -> Vec<String> {
        texts.iter().map(|text| (*text).to_owned()).collect()
    }

    fn field(
        name: &str,
        conversion: Option<&str>,
        format_spec: Option<&str>,
    ) -> (String, Option<String>, Option<String>) {
        (
            name.to_owned(),
            conversion.map(str::to_owned),
            format_spec.map(str::to_owned),
        )
    }

    #[test]
    fn test_collect_format_strings_splits_fstrings() {
        assert_eq!(
            summarize_format_strings(r#"message = f"Copied '{ source !r:>8}' {count=}""#),
            vec![(
                PythonFormatStyle::FString,
                String::new(),
                owned(&["Copied '", "' ", ""]),
                vec![
                    field("source", Some("r"), Some(">8")),
                    field("count", None, None)
                ],
            )]
        );
    }

    #[test]
    fn test_collect_format_strings_splits_str_format_templates() {
        assert_eq!(
            summarize_format_strings(
                r#"message = "Copied '{}' to '{target.name!s}' {{raw}}".format(a, target=b)"#
            ),
            vec![(
                PythonFormatStyle::StrFormat,
                String::new(),
                owned(&["Copied '", "' to '", "' {{raw}}"]),
                vec![field("", None, None), field("target.name", Some("s"), None)],
            )]
        );
    }

    #[rstest::rstest]
    #[case::percent_operator(r#"message = "Got %s, %(key)5.2f and 100%%" % values"#)]
    #[case::logger_with_arguments(r#"logger.info("Got %s, %(key)5.2f and 100%%", values)"#)]
    fn test_collect_format_strings_splits_printf_templates(#[case] python_code: &str) {
        assert_eq!(
            summarize_format_strings(python_code),
            vec![(
                PythonFormatStyle::Printf,
                String::new(),
                owned(&["Got ", ", ", " and 100%%"]),
                vec![
                    field("", Some("s"), None),
                    field("key", Some("f"), Some("5.2"))
                ],
            )]
        );
    }

    #[test]
    fn test_collect_format_strings_reads_concatenated_prefix() {
        let summaries = summarize_format_strings("message = (\"Load \" f\"'{name}'\")");
        let (style, preceding_text, ..) = &summaries[0];
        assert_eq!(summaries.len(), 1);
        assert_eq!(*style, PythonFormatStyle::FString);
        assert_eq!(preceding_text.as_str(), "Load ");
    }

    #[test]
    fn test_collect_format_strings_skips_unformatted_raw_and_byte_strings() {
        let source = indoc::indoc! {r#"
            plain = "Invalid '{name}' or '%s'"
            raw = r"Invalid '{name}'".format(name=value)
            byte = b"Invalid '%s'" % value
            no_arguments = logger.info("Literal '%s'")
        "#};
        assert_eq!(summarize_format_strings(source), vec![]);
    }
}