Skip to main content

vitrify_cli/cmd/
questions.rs

1//! `vitrify questions` — every `Q-###` of an SDD's Decision Log, whether it
2//! is open, and the units it blocks. A read verb.
3//!
4//! A question is open unless its entry marks it resolved
5//! ([`vitrify_core::questions`] — the rule lives there and nowhere else).
6//! The units it blocks are those whose entry carries its `OPEN(Q-###)`
7//! marker, and — for an open question a run landed — the unit it was
8//! reported on: that unit's worker stopped at it, and no marker says so,
9//! since a worker may not edit spec text.
10
11use vitrify_core::CoreError;
12use vitrify_core::model::{self, Instance, SddHandle, Shape};
13use vitrify_core::questions::{self, Question};
14
15use crate::out::{J, emit};
16
17/// One question with what it blocks.
18struct Row {
19    sdd: String,
20    q: Question,
21    blocks: Vec<String>,
22}
23
24fn unit_text(inst: &Instance, u: &model::Unit) -> Result<String, CoreError> {
25    match inst.shape {
26        Shape::Bundle => model::read_text(&u.file),
27        Shape::Single => {
28            let text = model::read_text(&inst.file)?;
29            Ok(model::find_unit_span(&text, &u.unit)
30                .map(|(s, e)| text[s..e].to_string())
31                .unwrap_or_default())
32        }
33    }
34}
35
36fn rows_of(name: &str, sdd: &SddHandle) -> Result<Vec<Row>, CoreError> {
37    let inst = Instance::load(sdd.clone())?;
38    let mut marked: Vec<(String, Vec<String>)> = Vec::new();
39    for u in &inst.units {
40        marked.push((u.unit.clone(), questions::markers(&unit_text(&inst, u)?)));
41    }
42    Ok(questions::of(sdd)?
43        .into_iter()
44        .map(|q| {
45            let mut blocks: Vec<String> = marked
46                .iter()
47                .filter(|(_, qs)| qs.contains(&q.id))
48                .map(|(u, _)| u.clone())
49                .collect();
50            if q.open
51                && let Some(unit) = questions::reported_on(&q.text)
52                && !blocks.contains(&unit)
53            {
54                blocks.push(unit);
55                blocks.sort();
56            }
57            Row {
58                sdd: name.to_string(),
59                q,
60                blocks,
61            }
62        })
63        .collect())
64}
65
66/// The open questions of `sdd`, each with the units it blocks.
67pub fn open(sdd: &SddHandle) -> Result<Vec<(String, Vec<String>)>, CoreError> {
68    Ok(rows_of("", sdd)?
69        .into_iter()
70        .filter(|r| r.q.open)
71        .map(|r| (r.q.id, r.blocks))
72        .collect())
73}
74
75/// `text` cut to `max` characters, on one line.
76fn brief(text: &str, max: usize) -> String {
77    if text.chars().count() <= max {
78        return text.to_string();
79    }
80    let cut: String = text.chars().take(max.saturating_sub(1)).collect();
81    format!("{}…", cut.trim_end())
82}
83
84pub fn run(sdd: Option<&str>, all: bool, open_only: bool, json: bool) -> Result<u8, CoreError> {
85    let root = super::repo_root();
86    let sdds = super::targets(&root, sdd, all, &super::cwd())?;
87    let mut rows: Vec<Row> = Vec::new();
88    let code = super::each(&root, &sdds, |sdd| {
89        rows.extend(rows_of(&super::label(sdd, &root), sdd)?);
90        Ok(0)
91    });
92    if !all && code != 0 {
93        return Ok(code); // the one SDD could not be read: already said
94    }
95    let n_open = rows.iter().filter(|r| r.q.open).count();
96    let n_resolved = rows.len() - n_open;
97    if open_only {
98        rows.retain(|r| r.q.open);
99    }
100
101    let status = |q: &Question| if q.open { "open" } else { "resolved" };
102    if json {
103        let obj = J::Obj(vec![
104            ("open".into(), J::Int(n_open as i64)),
105            ("resolved".into(), J::Int(n_resolved as i64)),
106            (
107                "questions".into(),
108                J::Arr(
109                    rows.iter()
110                        .map(|r| {
111                            J::Obj(vec![
112                                ("sdd".into(), J::s(r.sdd.clone())),
113                                ("q".into(), J::s(r.q.id.clone())),
114                                ("status".into(), J::s(status(&r.q))),
115                                (
116                                    "blocks".into(),
117                                    J::Arr(r.blocks.iter().map(|u| J::s(u.clone())).collect()),
118                                ),
119                                ("noted".into(), J::s(r.q.meta.clone())),
120                                ("text".into(), J::s(r.q.text.clone())),
121                            ])
122                        })
123                        .collect(),
124                ),
125            ),
126        ]);
127        emit(&obj.dumps_indent2());
128    } else {
129        let w_id = rows
130            .iter()
131            .map(|r| r.q.id.len() + if all { r.sdd.chars().count() + 1 } else { 0 })
132            .max()
133            .unwrap_or(0);
134        let w_blocks = rows
135            .iter()
136            .map(|r| r.blocks.join(", ").len().max(1))
137            .max()
138            .unwrap_or(1);
139        for r in &rows {
140            let id = if all {
141                format!("{} {}", r.sdd, r.q.id)
142            } else {
143                r.q.id.clone()
144            };
145            let blocks = if r.blocks.is_empty() {
146                "—".to_string()
147            } else {
148                r.blocks.join(", ")
149            };
150            // `—` is one column wide and three bytes long: pad by characters
151            let pad = " ".repeat(w_blocks.saturating_sub(blocks.chars().count()));
152            emit(&format!(
153                "{id:<w_id$}  {:<8}  blocks {blocks}{pad}  {}",
154                status(&r.q),
155                brief(&r.q.text, 100)
156            ));
157        }
158        emit(&format!("{n_open} open, {n_resolved} resolved"));
159    }
160    Ok(code)
161}