Files
stimmwerk/src/roadmap.rs
wlkns-dev 3c9ff67b0c Entwickler-Reiter „Roadmap“ (#1035): ROADMAP.md lesen, Kennzahlen, Filter, Sprung per ID
Nur in Debug-Builds erreichbar (--tab roadmap). Parser mit Tests, auch gegen die echte ROADMAP.md.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-02 09:28:06 +01:00

360 lines
10 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! Liest `ROADMAP.md` für den Entwickler-Reiter „Roadmap“.
//!
//! Gleiche Regeln wie `tools/check-roadmap.py`: Eine ID `#NNNN` steht am Zeilenanfang, als Tabellenzeile
//! (`| #1001 | Titel | ✓ … | Notiz |`) oder als Aufzählung (`- [#1031] **Titel.** Text`).
/// Abschnitt, zu dem ein Eintrag gehört.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Section {
Stand,
Todo,
Erledigt,
Entscheidung,
Risiko,
Auftrag,
}
impl Section {
pub const ALL: [Section; 6] = [
Section::Stand,
Section::Todo,
Section::Erledigt,
Section::Entscheidung,
Section::Risiko,
Section::Auftrag,
];
pub fn label(self) -> &'static str {
match self {
Section::Stand => "Stand",
Section::Todo => "Todo",
Section::Erledigt => "Erledigt",
Section::Entscheidung => "Entscheidungen",
Section::Risiko => "Risiken",
Section::Auftrag => "Aufträge",
}
}
}
#[derive(Clone, Debug)]
pub struct Item {
pub id: u32,
pub section: Section,
/// Gruppe im Abschnitt, z. B. „P1 – Release v0.1.0“ (leer, wenn der Abschnitt keine hat).
pub group: String,
pub title: String,
/// Status einer Stand-Zeile: `✓`, `◐`, `✗` oder `?`.
pub status: Option<char>,
pub detail: String,
/// Andere IDs, die Titel oder Text nennen (ohne die eigene).
pub refs: Vec<u32>,
}
#[derive(Clone, Debug, Default)]
pub struct Roadmap {
/// Kopfzeile „Stand: …“ ohne das Wort „Stand:“.
pub stand: String,
pub next_id: Option<u32>,
pub items: Vec<Item>,
}
impl Roadmap {
pub fn parse(text: &str) -> Roadmap {
let mut out = Roadmap::default();
let mut section: Option<Section> = None;
let mut group = String::new();
for line in text.lines() {
if let Some(rest) = line.strip_prefix("Stand:") {
out.stand = rest.trim().to_string();
out.next_id = parse_next_id(rest);
continue;
}
if let Some(h) = line.strip_prefix("## ") {
section = section_of(h);
group.clear();
continue;
}
let Some(sec) = section else { continue };
if let Some(g) = bold_line(line) {
// „**Erledigt (einzeln)**“ eröffnet den Block erledigter Einträge im Todo-Abschnitt;
// „**Erledigt am …:**“ ist eine Fließtext-Zeile ohne ID.
if sec == Section::Todo && g.starts_with("Erledigt (einzeln)") {
section = Some(Section::Erledigt);
group = "Erledigt".to_string();
} else {
group = g.to_string();
}
continue;
}
if let Some(item) = parse_item(line, sec, &group) {
out.items.push(item);
}
}
out
}
pub fn count(&self, s: Section) -> usize {
self.items.iter().filter(|i| i.section == s).count()
}
/// Anzahl der Stand-Zeilen mit dem gegebenen Status.
pub fn count_status(&self, status: char) -> usize {
self.items
.iter()
.filter(|i| i.status == Some(status))
.count()
}
pub fn find(&self, id: u32) -> Option<&Item> {
self.items.iter().find(|i| i.id == id)
}
}
fn parse_next_id(s: &str) -> Option<u32> {
let rest = s.split("Nächste freie ID:").nth(1)?.trim_start();
digits_after_hash(rest)
}
fn digits_after_hash(s: &str) -> Option<u32> {
let d: String = s
.strip_prefix('#')?
.chars()
.take_while(char::is_ascii_digit)
.collect();
d.parse().ok()
}
fn section_of(heading: &str) -> Option<Section> {
if heading.contains("Stand") {
Some(Section::Stand)
} else if heading.contains("Todo") {
Some(Section::Todo)
} else if heading.contains("Entscheidungen") {
Some(Section::Entscheidung)
} else if heading.contains("Risiken") {
Some(Section::Risiko)
} else if heading.contains("Aufträge") {
Some(Section::Auftrag)
} else {
None
}
}
/// Zeile der Form `**Text**` (nur fett, sonst nichts).
fn bold_line(line: &str) -> Option<&str> {
let t = line.trim();
let inner = t.strip_prefix("**")?.strip_suffix("**")?;
(!inner.contains("**")).then_some(inner)
}
fn parse_item(line: &str, section: Section, group: &str) -> Option<Item> {
if let Some(rest) = line.strip_prefix("| #") {
let cells: Vec<&str> = line
.trim()
.trim_matches('|')
.split('|')
.map(str::trim)
.collect();
let id = digits_after_hash(&format!("#{rest}"))?;
let title = cells.get(1).copied().unwrap_or_default().to_string();
let status = cells.get(2).and_then(|c| c.chars().next());
let detail = cells
.iter()
.skip(3)
.copied()
.collect::<Vec<_>>()
.join(" | ");
return Some(make_item(id, section, group, title, status, detail));
}
let rest = line.strip_prefix("- [#")?;
let id: u32 = rest.split(']').next()?.parse().ok()?;
let body = rest.split_once(']')?.1.trim();
let (title, detail) = split_title(body);
Some(make_item(id, section, group, title, None, detail))
}
/// Titel einer Aufzählung: der fette Anfang `**Titel.**`, sonst der erste Satz.
fn split_title(body: &str) -> (String, String) {
if let Some(r) = body.strip_prefix("**")
&& let Some((t, rest)) = r.split_once("**")
{
return (
t.trim().trim_end_matches('.').to_string(),
rest.trim().to_string(),
);
}
match body.split_once(". ") {
Some((t, rest)) => (t.to_string(), rest.to_string()),
None => (body.trim_end_matches('.').to_string(), String::new()),
}
}
fn make_item(
id: u32,
section: Section,
group: &str,
title: String,
status: Option<char>,
detail: String,
) -> Item {
let mut refs = Vec::new();
for text in [&title, &detail] {
let mut rest = text.as_str();
while let Some(p) = rest.find('#') {
let after = &rest[p..];
let d: String = after[1..]
.chars()
.take_while(char::is_ascii_digit)
.collect();
if d.len() == 4
&& let Ok(n) = d.parse::<u32>()
&& n != id
&& !refs.contains(&n)
{
refs.push(n);
}
rest = &rest[p + 1..];
}
}
Item {
id,
section,
group: group.to_string(),
title,
status,
detail,
refs,
}
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE: &str = "\
# Roadmap
Stand: 2026-11-02, `main` @ abc. Nächste freie ID: #1010
## 1. Leitplanken
Text ohne ID, #1001 hier zählt nicht.
## 2. Stand
| ID | Was | Status | Notiz |
|---|---|---|---|
| #1001 | Audio | ✓ geprüft | Test grün → #1002 |
| #1002 | Anzeige | ◐ gebaut | Nur Simulation |
## 3. Todo
**P1 – Release**
- [#1003] **Paket installieren.** Nutzer: pacman. → #1001
- [#1004] **Kurz.**
**Erledigt (einzeln)**
- [#1005] **Probe-Tag (2026-11-01).** Lauf grün.
**Erledigt am 2026-11-01:** Fließtext ohne ID.
## 4. Offene Entscheidungen
- [#1006] Lizenz des Codes: MIT, entschieden. Offen ist nichts.
## 5. Risiken
- [#1007] Kein echtes Mikrofon.
## 7. Aufträge an den Analyse-Agenten
- [#1008] **Format stabil halten.** Siehe #1003.
";
#[test]
fn reads_head() {
let r = Roadmap::parse(SAMPLE);
assert_eq!(r.next_id, Some(1010));
assert!(r.stand.starts_with("2026-11-02"));
}
#[test]
fn reads_table_rows_with_status() {
let r = Roadmap::parse(SAMPLE);
let a = r.find(1001).unwrap();
assert_eq!(a.section, Section::Stand);
assert_eq!(a.title, "Audio");
assert_eq!(a.status, Some('✓'));
assert_eq!(a.detail, "Test grün → #1002");
assert_eq!(a.refs, vec![1002]);
assert_eq!(r.find(1002).unwrap().status, Some('◐'));
}
#[test]
fn reads_todo_with_group_and_bold_title() {
let r = Roadmap::parse(SAMPLE);
let t = r.find(1003).unwrap();
assert_eq!(
(t.section, t.group.as_str()),
(Section::Todo, "P1 – Release")
);
assert_eq!(t.title, "Paket installieren");
assert_eq!(t.refs, vec![1001]);
assert_eq!(r.find(1004).unwrap().title, "Kurz");
}
#[test]
fn done_block_and_prose_lines() {
let r = Roadmap::parse(SAMPLE);
assert_eq!(r.find(1005).unwrap().section, Section::Erledigt);
assert_eq!(r.count(Section::Erledigt), 1);
}
#[test]
fn decisions_risks_orders_and_sentence_title() {
let r = Roadmap::parse(SAMPLE);
let d = r.find(1006).unwrap();
assert_eq!(
(d.section, d.title.as_str()),
(Section::Entscheidung, "Lizenz des Codes: MIT, entschieden")
);
assert_eq!(r.find(1007).unwrap().section, Section::Risiko);
assert_eq!(r.find(1008).unwrap().section, Section::Auftrag);
assert_eq!(r.find(1008).unwrap().refs, vec![1003]);
}
#[test]
fn ignores_ids_outside_definitions() {
let r = Roadmap::parse(SAMPLE);
assert!(r.find(1010).is_none());
assert_eq!(r.items.len(), 8);
}
/// Die echte `ROADMAP.md` muss vollständig lesbar sein: so viele Einträge, wie `check-roadmap.py` Definitionen zählt.
#[test]
fn real_roadmap_matches_definition_count() {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("ROADMAP.md");
let text = std::fs::read_to_string(path).unwrap();
let defs = text
.lines()
.filter(|l| {
let digits = |s: &str| s.chars().take_while(char::is_ascii_digit).count() > 0;
l.strip_prefix("| #").is_some_and(digits)
|| l.strip_prefix("- [#").is_some_and(digits)
})
.count();
let r = Roadmap::parse(&text);
assert_eq!(r.items.len(), defs);
assert!(r.next_id.is_some_and(|n| r.items.iter().all(|i| i.id < n)));
let mut ids: Vec<u32> = r.items.iter().map(|i| i.id).collect();
ids.sort_unstable();
ids.dedup();
assert_eq!(ids.len(), r.items.len(), "doppelte ID");
assert!(
r.items
.iter()
.filter(|i| i.section == Section::Stand)
.all(|i| i.status.is_some())
);
}
}