ralph-workflow 0.7.18

PROMPT-driven multi-agent orchestrator for git repos
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
// Core XSD validation implementation.
// Contains the main validation logic and parsed commit message types.

mod types;
mod file_list_parsers;

pub use types::CommitMessageElements;

use crate::files::llm_output_extraction::xml_helpers::check_for_illegal_xml_characters;
use crate::reducer::state::pipeline::ExcludedFile;
use quick_xml::Reader;

use self::file_list_parsers::{parse_excluded_files_section, parse_files_section};

/// Example of a valid commit message XML for error messages.
const EXAMPLE_COMMIT_XML: &str = r"<ralph-commit>
<ralph-subject>feat(api): add user authentication</ralph-subject>
<ralph-body>Implements JWT-based authentication for the API.</ralph-body>
</ralph-commit>";

/// Result type for text-reading functions that return the reader state.
type TextReadResult<'a> = Result<(String, Reader<&'a [u8]>), XsdValidationError>;

/// Result type for commit element parsing functions.
type CommitParseResult<'a> = Result<(Reader<&'a [u8]>, ValidatorState), XsdValidationError>;

fn configure_validation_reader(mut reader: Reader<&[u8]>) -> Reader<&[u8]> {
    reader.config_mut().trim_text(false);
    reader
}

#[derive(Default)]
struct ValidatorState {
    subject: Option<String>,
    body: Option<String>,
    body_summary: Option<String>,
    body_details: Option<String>,
    body_footer: Option<String>,
    skip_reason: Option<String>,
    files: Vec<String>,
    files_seen: bool,
    excluded_files: Vec<ExcludedFile>,
    excluded_files_seen: bool,
}

impl ValidatorState {
    fn with_subject(mut self, subject: String) -> Self {
        self.subject = Some(subject);
        self
    }

    fn with_body(mut self, body: String) -> Self {
        self.body = Some(body);
        self
    }

    fn with_body_summary(mut self, summary: String) -> Self {
        self.body_summary = Some(summary);
        self
    }

    fn with_body_details(mut self, details: String) -> Self {
        self.body_details = Some(details);
        self
    }

    fn with_body_footer(mut self, footer: String) -> Self {
        self.body_footer = Some(footer);
        self
    }

    fn with_skip_reason(mut self, reason: String) -> Self {
        self.skip_reason = Some(reason);
        self
    }

    fn with_files(mut self, files: Vec<String>) -> Self {
        self.files = files;
        self.files_seen = true;
        self
    }

    fn with_excluded_files(mut self, excluded_files: Vec<ExcludedFile>) -> Self {
        self.excluded_files = excluded_files;
        self.excluded_files_seen = true;
        self
    }
}

/// Validate XML content against the XSD schema.
///
/// This function validates that the XML content conforms to the expected
/// commit message format defined in `commit_message.xsd`:
///
/// ```xml
/// <ralph-commit>
///   <ralph-subject>type(scope): description</ralph-subject>
///   <ralph-body>Optional body text</ralph-body>
///   <ralph-body-summary>Optional summary</ralph-body-summary>
///   <ralph-body-details>Optional details</ralph-body-details>
///   <ralph-body-footer>Optional footer</ralph-body-footer>
/// </ralph-commit>
/// ```
///
/// # Arguments
///
/// * `xml_content` - The XML content to validate
///
/// # Returns
///
/// * `Ok(CommitMessageElements)` if the XML is valid and contains all required elements
/// * `Err(XsdValidationError)` if the XML is invalid or doesn't conform to the schema
///
/// # Errors
///
/// Returns an `XsdValidationError` if the XML is malformed, contains illegal characters,
/// is missing the root `<ralph-commit>` element, is missing the required `<ralph-subject>` element,
/// or if the `<ralph-subject>` does not conform to the conventional commit format.
///
/// # Panics
///
/// Panics if an internal invariant is violated: `subject` is `None` when `skip_reason` is also
/// `None` after parsing. This should never happen in practice.
///
/// # Examples
///
/// ```ignore
/// use ralph_workflow::files::llm_output_extraction::xsd_validation::validate_xml_against_xsd;
///
/// let xml = r#"<ralph-commit>
/// <ralph-subject>feat: add new feature</ralph-subject>
/// </ralph-commit>"#;
/// let result = validate_xml_against_xsd(xml);
/// assert!(result.is_ok());
/// ```
pub fn validate_xml_against_xsd(
    xml_content: &str,
) -> Result<CommitMessageElements, XsdValidationError> {
    let content = xml_content.trim();

    // Check for illegal XML characters BEFORE parsing
    check_for_illegal_xml_characters(content)?;

    let reader = configure_validation_reader(create_reader(content));

    let reader = find_commit_root(reader, content)?;

    let (reader, state) = parse_commit_elements(reader, ValidatorState::default())?;

    ensure_no_trailing_root_content(reader)?;

    // Validate that either skip_reason OR subject is present (but not both)
    if state.skip_reason.is_none() && state.subject.is_none() {
        return Err(XsdValidationError {
            error_type: XsdErrorType::MissingRequiredElement,
            element_path: "ralph-commit".to_string(),
            expected: "either <ralph-subject> or <ralph-skip>".to_string(),
            found: "neither commit message nor skip directive".to_string(),
            suggestion: "Provide either a commit message or skip directive.".to_string(),
            example: Some(EXAMPLE_COMMIT_XML.into()),
        });
    }

    if let Some(skip_reason) = state.skip_reason {
        let skip_trimmed = skip_reason.trim();
        if skip_trimmed.is_empty() {
            return Err(XsdValidationError {
                error_type: XsdErrorType::InvalidContent,
                element_path: "ralph-skip".to_string(),
                expected: "non-empty skip reason".to_string(),
                found: "empty skip reason".to_string(),
                suggestion: "The <ralph-skip> must contain a reason why no commit is needed.".to_string(),
                example: Some("<ralph-commit><ralph-skip>No staged changes found via git status</ralph-skip></ralph-commit>".into()),
            });
        }
        return Ok(CommitMessageElements {
            subject: String::new(),
            body: None,
            body_summary: None,
            body_details: None,
            body_footer: None,
            skip_reason: Some(skip_trimmed.to_string()),
            files: Vec::new(),
            excluded_files: Vec::new(),
        });
    }

    let subject = state
        .subject
        .expect("subject must be Some if skip_reason is None");
    let subject = subject.trim();
    if subject.is_empty() {
        return Err(XsdValidationError {
            error_type: XsdErrorType::InvalidContent,
            element_path: "ralph-subject".to_string(),
            expected: "non-empty subject line".to_string(),
            found: "empty subject".to_string(),
            suggestion: "The <ralph-subject> must contain a non-empty commit subject like 'feat: add feature'.".to_string(),
            example: Some(EXAMPLE_COMMIT_XML.into()),
        });
    }

    if !is_conventional_commit_subject(subject) {
        return Err(XsdValidationError {
            error_type: XsdErrorType::InvalidContent,
            element_path: "ralph-subject".to_string(),
            expected: "conventional commit format (type: description or type(scope): description)".to_string(),
            found: subject.to_string(),
            suggestion: "Use conventional commit format: type(scope): description. Valid types: feat, fix, docs, style, refactor, perf, test, build, ci, chore.".to_string(),
            example: Some(EXAMPLE_COMMIT_XML.into()),
        });
    }

    Ok(CommitMessageElements {
        subject: subject.to_string(),
        body: state.body.filter(|s| !s.is_empty()),
        body_summary: state.body_summary.filter(|s| !s.is_empty()),
        body_details: state.body_details.filter(|s| !s.is_empty()),
        body_footer: state.body_footer.filter(|s| !s.is_empty()),
        skip_reason: None,
        files: state.files,
        excluded_files: state.excluded_files,
    })
}

fn parse_commit_elements<'a>(
    mut reader: Reader<&'a [u8]>,
    state: ValidatorState,
) -> CommitParseResult<'a> {
    match reader.read_event_into(&mut Vec::new()) {
        Ok(Event::Start(e)) => match e.name().as_ref() {
            b"ralph-subject" => {
                if state.skip_reason.is_some() {
                    return Err(XsdValidationError {
                        error_type: XsdErrorType::UnexpectedElement,
                        element_path: "ralph-commit/ralph-subject".to_string(),
                        expected: "either commit message elements OR ralph-skip, not both".to_string(),
                        found: "mixed commit and skip elements".to_string(),
                        suggestion: "Use ralph-skip alone when no commit is needed.".to_string(),
                        example: Some("<ralph-commit><ralph-skip>No changes found</ralph-skip></ralph-commit>".into()),
                    });
                }
                if state.subject.is_some() {
                    return Err(duplicate_element_error("ralph-subject", "ralph-commit"));
                }
                let (text, reader) = read_text_with_inline_code_until_end(
                    reader,
                    b"ralph-subject",
                    "ralph-commit/ralph-subject",
                )?;
                parse_commit_elements(reader, state.with_subject(text))
            }
            b"ralph-body" => {
                if state.skip_reason.is_some() {
                    return Err(XsdValidationError {
                        error_type: XsdErrorType::UnexpectedElement,
                        element_path: "ralph-commit/ralph-body".to_string(),
                        expected: "either commit message elements OR ralph-skip, not both".to_string(),
                        found: "mixed commit and skip elements".to_string(),
                        suggestion: "Use ralph-skip alone when no commit is needed.".to_string(),
                        example: Some("<ralph-commit><ralph-skip>No changes found</ralph-skip></ralph-commit>".into()),
                    });
                }
                if state.body_summary.is_some() || state.body_details.is_some() || state.body_footer.is_some() {
                    return Err(XsdValidationError {
                        error_type: XsdErrorType::UnexpectedElement,
                        element_path: "ralph-commit/ralph-body".to_string(),
                        expected: "either <ralph-body> OR detailed tags, not both".to_string(),
                        found: "mixed simple and detailed body elements".to_string(),
                        suggestion: "Use <ralph-body> for simple body OR <ralph-body-summary>, <ralph-body-details>, <ralph-body-footer> for detailed format.".to_string(),
                        example: Some(EXAMPLE_COMMIT_XML.into()),
                    });
                }
                if state.body.is_some() {
                    return Err(duplicate_element_error("ralph-body", "ralph-commit"));
                }
                let (text, reader) = read_text_with_inline_code_until_end(
                    reader,
                    b"ralph-body",
                    "ralph-commit/ralph-body",
                )?;
                parse_commit_elements(reader, state.with_body(text))
            }
            b"ralph-body-summary" => {
                if state.skip_reason.is_some() {
                    return Err(XsdValidationError {
                        error_type: XsdErrorType::UnexpectedElement,
                        element_path: "ralph-commit/ralph-body-summary".to_string(),
                        expected: "either commit message elements OR ralph-skip, not both".to_string(),
                        found: "mixed commit and skip elements".to_string(),
                        suggestion: "Use ralph-skip alone when no commit is needed.".to_string(),
                        example: Some("<ralph-commit><ralph-skip>No changes found</ralph-skip></ralph-commit>".into()),
                    });
                }
                if state.body.is_some() {
                    return Err(XsdValidationError {
                        error_type: XsdErrorType::UnexpectedElement,
                        element_path: "ralph-commit/ralph-body-summary".to_string(),
                        expected: "either <ralph-body> OR detailed tags, not both".to_string(),
                        found: "mixed simple and detailed body elements".to_string(),
                        suggestion: "Use <ralph-body> for simple body OR <ralph-body-summary>, <ralph-body-details>, <ralph-body-footer> for detailed format.".to_string(),
                        example: Some(EXAMPLE_COMMIT_XML.into()),
                    });
                }
                if state.body_summary.is_some() {
                    return Err(duplicate_element_error("ralph-body-summary", "ralph-commit"));
                }
                let (text, reader) = read_text_with_inline_code_until_end(
                    reader,
                    b"ralph-body-summary",
                    "ralph-commit/ralph-body-summary",
                )?;
                parse_commit_elements(reader, state.with_body_summary(text))
            }
            b"ralph-body-details" => {
                if state.skip_reason.is_some() {
                    return Err(XsdValidationError {
                        error_type: XsdErrorType::UnexpectedElement,
                        element_path: "ralph-commit/ralph-body-details".to_string(),
                        expected: "either commit message elements OR ralph-skip, not both".to_string(),
                        found: "mixed commit and skip elements".to_string(),
                        suggestion: "Use ralph-skip alone when no commit is needed.".to_string(),
                        example: Some("<ralph-commit><ralph-skip>No changes found</ralph-skip></ralph-commit>".into()),
                    });
                }
                if state.body.is_some() {
                    return Err(XsdValidationError {
                        error_type: XsdErrorType::UnexpectedElement,
                        element_path: "ralph-commit/ralph-body-details".to_string(),
                        expected: "either <ralph-body> OR detailed tags, not both".to_string(),
                        found: "mixed simple and detailed body elements".to_string(),
                        suggestion: "Use <ralph-body> for simple body OR <ralph-body-summary>, <ralph-body-details>, <ralph-body-footer> for detailed format.".to_string(),
                        example: Some(EXAMPLE_COMMIT_XML.into()),
                    });
                }
                if state.body_details.is_some() {
                    return Err(duplicate_element_error("ralph-body-details", "ralph-commit"));
                }
                let (text, reader) = read_text_with_inline_code_until_end(
                    reader,
                    b"ralph-body-details",
                    "ralph-commit/ralph-body-details",
                )?;
                parse_commit_elements(reader, state.with_body_details(text))
            }
            b"ralph-body-footer" => {
                if state.skip_reason.is_some() {
                    return Err(XsdValidationError {
                        error_type: XsdErrorType::UnexpectedElement,
                        element_path: "ralph-commit/ralph-body-footer".to_string(),
                        expected: "either commit message elements OR ralph-skip, not both".to_string(),
                        found: "mixed commit and skip elements".to_string(),
                        suggestion: "Use ralph-skip alone when no commit is needed.".to_string(),
                        example: Some("<ralph-commit><ralph-skip>No changes found</ralph-skip></ralph-commit>".into()),
                    });
                }
                if state.body.is_some() {
                    return Err(XsdValidationError {
                        error_type: XsdErrorType::UnexpectedElement,
                        element_path: "ralph-commit/ralph-body-footer".to_string(),
                        expected: "either <ralph-body> OR detailed tags, not both".to_string(),
                        found: "mixed simple and detailed body elements".to_string(),
                        suggestion: "Use <ralph-body> for simple body OR <ralph-body-summary>, <ralph-body-details>, <ralph-body-footer> for detailed format.".to_string(),
                        example: Some(EXAMPLE_COMMIT_XML.into()),
                    });
                }
                if state.body_footer.is_some() {
                    return Err(duplicate_element_error("ralph-body-footer", "ralph-commit"));
                }
                let (text, reader) = read_text_with_inline_code_until_end(
                    reader,
                    b"ralph-body-footer",
                    "ralph-commit/ralph-body-footer",
                )?;
                parse_commit_elements(reader, state.with_body_footer(text))
            }
            b"ralph-skip" => {
                if state.skip_reason.is_some() {
                    return Err(duplicate_element_error("ralph-skip", "ralph-commit"));
                }
                if state.subject.is_some() || state.body.is_some() || state.body_summary.is_some() || state.body_details.is_some() || state.body_footer.is_some() {
                    return Err(XsdValidationError {
                        error_type: XsdErrorType::UnexpectedElement,
                        element_path: "ralph-commit/ralph-skip".to_string(),
                        expected: "either commit message elements OR ralph-skip, not both".to_string(),
                        found: "mixed commit and skip elements".to_string(),
                        suggestion: "Use ralph-skip alone when no commit is needed.".to_string(),
                        example: Some("<ralph-commit><ralph-skip>No changes found</ralph-skip></ralph-commit>".into()),
                    });
                }
                let (text, reader) = read_text_with_inline_code_until_end(
                    reader,
                    b"ralph-skip",
                    "ralph-commit/ralph-skip",
                )?;
                parse_commit_elements(reader, state.with_skip_reason(text))
            }
            b"ralph-files" => {
                if state.files_seen {
                    return Err(duplicate_element_error("ralph-files", "ralph-commit"));
                }
                if state.skip_reason.is_some() {
                    return Err(XsdValidationError {
                        error_type: XsdErrorType::UnexpectedElement,
                        element_path: "ralph-commit/ralph-files".to_string(),
                        expected: "either commit message elements OR ralph-skip, not both".to_string(),
                        found: "ralph-files cannot be used with ralph-skip".to_string(),
                        suggestion: "Remove ralph-files when using ralph-skip.".to_string(),
                        example: Some(EXAMPLE_COMMIT_XML.into()),
                    });
                }
                let files = parse_files_section(&mut reader)?;
                parse_commit_elements(reader, state.with_files(files))
            }
            b"ralph-excluded-files" => {
                if state.excluded_files_seen {
                    return Err(duplicate_element_error("ralph-excluded-files", "ralph-commit"));
                }
                if state.skip_reason.is_some() {
                    return Err(XsdValidationError {
                        error_type: XsdErrorType::UnexpectedElement,
                        element_path: "ralph-commit/ralph-excluded-files".to_string(),
                        expected: "either commit message elements OR ralph-skip, not both".to_string(),
                        found: "ralph-excluded-files cannot be used with ralph-skip".to_string(),
                        suggestion: "Remove ralph-excluded-files when using ralph-skip.".to_string(),
                        example: Some(EXAMPLE_COMMIT_XML.into()),
                    });
                }
                let excluded = parse_excluded_files_section(&mut reader)?;
                parse_commit_elements(reader, state.with_excluded_files(excluded))
            }
            other => {
                let _ = skip_to_end(&mut reader, other);
                parse_commit_elements(reader, state)
            }
        },
        Ok(Event::Empty(e)) => match e.name().as_ref() {
            b"ralph-subject" => {
                Err(XsdValidationError {
                    error_type: XsdErrorType::InvalidContent,
                    element_path: "ralph-subject".to_string(),
                    expected: "non-empty subject line".to_string(),
                    found: "<ralph-subject/> (empty subject)".to_string(),
                    suggestion: "Provide a conventional commit subject inside <ralph-subject> like 'feat: add feature'.".to_string(),
                    example: Some(EXAMPLE_COMMIT_XML.into()),
                })
            }
            b"ralph-skip" => {
                Err(XsdValidationError {
                    error_type: XsdErrorType::InvalidContent,
                    element_path: "ralph-skip".to_string(),
                    expected: "non-empty skip reason".to_string(),
                    found: "<ralph-skip/> (empty skip reason)".to_string(),
                    suggestion: "Provide a reason inside <ralph-skip> explaining why no commit is needed.".to_string(),
                    example: Some("<ralph-commit><ralph-skip>No changes found</ralph-skip></ralph-commit>".into()),
                })
            }
            b"ralph-files" => {
                Err(XsdValidationError {
                    error_type: XsdErrorType::InvalidContent,
                    element_path: "ralph-commit/ralph-files".to_string(),
                    expected: "at least one ralph-file child element".to_string(),
                    found: "<ralph-files/>".to_string(),
                    suggestion: "Either add one or more <ralph-file>path</ralph-file> entries or omit <ralph-files> entirely.".to_string(),
                    example: Some(EXAMPLE_COMMIT_XML.into()),
                })
            }
            b"ralph-excluded-files" => {
                Err(XsdValidationError {
                    error_type: XsdErrorType::InvalidContent,
                    element_path: "ralph-commit/ralph-excluded-files".to_string(),
                    expected: "at least one ralph-excluded-file child element".to_string(),
                    found: "<ralph-excluded-files/>".to_string(),
                    suggestion: r#"Either add one or more <ralph-excluded-file reason="...">path</ralph-excluded-file> entries or omit <ralph-excluded-files> entirely."#.to_string(),
                    example: Some(EXAMPLE_COMMIT_XML.into()),
                })
            }
            _ => {
                parse_commit_elements(reader, state)
            }
        },
        Ok(Event::End(e)) if e.name().as_ref() == b"ralph-commit" => {
            Ok((reader, state))
        }
        Ok(Event::Eof) => Err(XsdValidationError {
            error_type: XsdErrorType::MalformedXml,
            element_path: "ralph-commit".to_string(),
            expected: "closing </ralph-commit> tag".to_string(),
            found: "end of content without closing tag".to_string(),
            suggestion: "Add </ralph-commit> at the end of your commit message.".to_string(),
            example: Some(EXAMPLE_COMMIT_XML.into()),
        }),
        Ok(Event::Text(_) | _) => {
            parse_commit_elements(reader, state)
        }
        Err(e) => Err(malformed_xml_error(&e)),
    }
}

fn find_commit_root<'a>(
    mut reader: Reader<&'a [u8]>,
    _content: &str,
) -> Result<Reader<&'a [u8]>, XsdValidationError> {
    match reader.read_event_into(&mut Vec::new()) {
        Ok(Event::Start(e)) if e.name().as_ref() == b"ralph-commit" => {
            Ok(reader)
        }
        Ok(Event::Empty(e)) if e.name().as_ref() == b"ralph-commit" => Err(XsdValidationError {
            error_type: XsdErrorType::MissingRequiredElement,
            element_path: "ralph-commit".to_string(),
            expected: "either <ralph-subject> or <ralph-skip>".to_string(),
            found: "<ralph-commit/> (empty root element)".to_string(),
            suggestion: "Use <ralph-commit>...</ralph-commit> and include either <ralph-subject> or <ralph-skip>.".to_string(),
            example: Some(EXAMPLE_COMMIT_XML.into()),
        }),
        Ok(Event::Start(e)) => {
            let tag_name = String::from_utf8_lossy(e.name().as_ref()).to_string();
            Err(XsdValidationError {
                error_type: XsdErrorType::MissingRequiredElement,
                element_path: "ralph-commit".to_string(),
                expected: "<ralph-commit> as root element".to_string(),
                found: format!("<{tag_name}> (wrong root element)"),
                suggestion: "Use <ralph-commit> as the root element.".to_string(),
                example: Some(EXAMPLE_COMMIT_XML.into()),
            })
        }
        Ok(Event::Empty(e)) => {
            let tag_name = String::from_utf8_lossy(e.name().as_ref()).to_string();
            Err(XsdValidationError {
                error_type: XsdErrorType::MissingRequiredElement,
                element_path: "ralph-commit".to_string(),
                expected: "<ralph-commit> as root element".to_string(),
                found: format!("<{tag_name}/> (wrong root element)"),
                suggestion: "Use <ralph-commit> as the root element.".to_string(),
                example: Some(EXAMPLE_COMMIT_XML.into()),
            })
        }
        Ok(Event::Eof) => Err(XsdValidationError {
            error_type: XsdErrorType::MissingRequiredElement,
            element_path: "ralph-commit".to_string(),
            expected: "<ralph-commit> as root element".to_string(),
            found: format_content_preview(_content),
            suggestion: "Wrap your commit message in <ralph-commit>...</ralph-commit> tags.".to_string(),
            example: Some(EXAMPLE_COMMIT_XML.into()),
        }),
        Ok(Event::Text(e)) => {
            reject_non_whitespace_text(&unescape_text(&e, "ralph-commit")?)?;
            find_commit_root(reader, _content)
        }
        Ok(Event::CData(e)) => {
            reject_non_whitespace_text(&String::from_utf8_lossy(e.as_ref()))?;
            find_commit_root(reader, _content)
        }
        Ok(_) => {
            find_commit_root(reader, _content)
        }
        Err(e) => Err(malformed_xml_error(&e)),
    }
}

fn ensure_no_trailing_root_content(
    mut reader: Reader<&[u8]>,
) -> Result<(), XsdValidationError> {
    match reader.read_event_into(&mut Vec::new()) {
        Ok(Event::Eof) => Ok(()),
        Ok(Event::Text(e)) => {
            reject_non_whitespace_text(&unescape_text(&e, "ralph-commit")?)?;
            ensure_no_trailing_root_content(reader)
        }
        Ok(Event::CData(e)) => {
            reject_non_whitespace_text(&String::from_utf8_lossy(e.as_ref()))?;
            ensure_no_trailing_root_content(reader)
        }
        Ok(_) => {
            ensure_no_trailing_root_content(reader)
        }
        Err(e) => Err(malformed_xml_error(&e)),
    }
}

fn reject_non_whitespace_text(text: &str) -> Result<(), XsdValidationError> {
    let trimmed = text.trim();
    if trimmed.is_empty() {
        Ok(())
    } else {
        Err(text_outside_tags_error(trimmed, "ralph-commit"))
    }
}

fn unescape_text(
    text: &quick_xml::events::BytesText<'_>,
    element_path: &str,
) -> Result<String, XsdValidationError> {
    text.unescape()
        .map(|t| t.to_string())
        .map_err(|e| XsdValidationError {
            error_type: XsdErrorType::MalformedXml,
            element_path: element_path.to_string(),
            expected: "valid XML text content".to_string(),
            found: format!("unescape error: {e}"),
            suggestion:
                "Ensure text content uses valid XML escaping (e.g., &amp; for '&', &lt; for '<')."
                    .to_string(),
            example: Some(EXAMPLE_COMMIT_XML.into()),
        })
}

fn read_text_with_inline_code_until_end<'a>(
    reader: Reader<&'a [u8]>,
    end_tag: &[u8],
    element_path: &str,
) -> TextReadResult<'a> {
    read_text_with_inline_code_until_end_with_acc(reader, end_tag, element_path, String::new())
}

fn read_text_with_inline_code_until_end_with_acc<'a>(
    mut reader: Reader<&'a [u8]>,
    end_tag: &[u8],
    element_path: &str,
    text: String,
) -> TextReadResult<'a> {
    match reader.read_event_into(&mut Vec::new()) {
        Ok(Event::Text(t)) => {
            let text = text + &unescape_text(&t, element_path)?;
            read_text_with_inline_code_until_end_with_acc(reader, end_tag, element_path, text)
        }
        Ok(Event::CData(c)) => {
            let text = text + &String::from_utf8_lossy(c.as_ref());
            read_text_with_inline_code_until_end_with_acc(reader, end_tag, element_path, text)
        }
        Ok(Event::Start(e)) if e.name().as_ref() == b"code" => {
            let nested_path = format!("{}/code", element_path);
            let (inner, reader) = read_text_with_inline_code_until_end(
                reader,
                b"code",
                &nested_path,
            )?;
            let text = text + &inner;
            read_text_with_inline_code_until_end_with_acc(reader, end_tag, element_path, text)
        }
        Ok(Event::Empty(e)) if e.name().as_ref() == b"code" => Err(XsdValidationError {
            error_type: XsdErrorType::InvalidContent,
            element_path: format!("{}/code", element_path),
            expected: "non-empty inline <code> content".to_string(),
            found: "<code/> (empty inline code element)".to_string(),
            suggestion: "Use <code>text</code> when you need inline code, or remove the empty <code/> element.".to_string(),
            example: Some(EXAMPLE_COMMIT_XML.into()),
        }),
        Ok(Event::Start(e)) => {
            let other = e.name().as_ref().to_vec();
            let other_name = String::from_utf8_lossy(&other);
            let _ = skip_to_end(&mut reader, &other);
            let _ = skip_to_end(&mut reader, &other);
            Err(XsdValidationError {
                error_type: XsdErrorType::UnexpectedElement,
                element_path: format!("{}/{}", element_path, other_name),
                expected: "text content with optional inline <code> elements".to_string(),
                found: format!("<{other_name}>"),
                suggestion: "Use plain text and optional inline <code>...</code> only; remove any other nested tags.".to_string(),
                example: Some(EXAMPLE_COMMIT_XML.into()),
            })
        }
        Ok(Event::Empty(e)) => {
            let other = e.name().as_ref().to_vec();
            let other_name = String::from_utf8_lossy(&other);
            Err(XsdValidationError {
                error_type: XsdErrorType::UnexpectedElement,
                element_path: format!("{}/{}", element_path, other_name),
                expected: "text content with optional inline <code> elements".to_string(),
                found: format!("<{other_name}/>"),
                suggestion: "Use plain text and optional inline <code>...</code> only; remove any other nested tags.".to_string(),
                example: Some(EXAMPLE_COMMIT_XML.into()),
            })
        }
        Ok(Event::End(e)) if e.name().as_ref() == end_tag => {
            Ok((text.trim().to_string(), reader))
        }
        Ok(Event::Eof) => Err(XsdValidationError {
            error_type: XsdErrorType::MalformedXml,
            element_path: element_path.to_string(),
            expected: format!("closing </{}> tag", String::from_utf8_lossy(end_tag)),
            found: "end of content without closing tag".to_string(),
            suggestion: format!("Add </{}> to close the element.", String::from_utf8_lossy(end_tag)),
            example: Some(EXAMPLE_COMMIT_XML.into()),
        }),
        Ok(_) => {
            read_text_with_inline_code_until_end_with_acc(reader, end_tag, element_path, text)
        }
        Err(e) => Err(malformed_xml_error(&e)),
    }
}