Skip to main content

vitrify_cli/cmd/
readset.rs

1//! `vitrify readset` — the ordered minimal reading set for one unit, with
2//! the byte-budget check (byte-denominated by design). JSON output held by
3//! the regression goldens (`fixtures/golden/calc/readset-*.json`).
4
5use vitrify_core::CoreError;
6use vitrify_core::config::Overrides;
7use vitrify_core::model::Instance;
8use vitrify_core::readset;
9
10use crate::out::{J, emit};
11
12pub fn run(
13    unit: &str,
14    sdd: Option<&str>,
15    json: bool,
16    budget: Option<Option<i64>>,
17) -> Result<u8, CoreError> {
18    let root = super::repo_root();
19    let cwd = super::cwd();
20    let inst = Instance::load(super::resolve_sdd(&root, sdd, &cwd)?)?;
21    let items = readset::resolve(&inst, unit, &super::peers(&root))?;
22
23    if let Some(flag) = budget {
24        let limit = match flag {
25            Some(bytes) => bytes,
26            None => {
27                super::load_config(&Overrides::default())?
28                    .lint
29                    .readset_budget_bytes
30            }
31        };
32        let total: i64 = items.iter().map(|i| i.bytes as i64).sum();
33        if total > limit {
34            return Err(CoreError::refusal(
35                2,
36                format!("error: reading set for {unit} totals {total} bytes, over budget {limit}"),
37            ));
38        }
39    }
40
41    let consumes = inst
42        .unit(unit)
43        .map(|u| u.meta.get_list("consumes"))
44        .unwrap_or_default();
45
46    if json {
47        let readings: Vec<J> = items
48            .iter()
49            .map(|item| {
50                J::Obj(vec![
51                    ("path".into(), J::s(item.path.display().to_string())),
52                    ("region".into(), J::opt_str(item.region.clone())),
53                ])
54            })
55            .collect();
56        let obj = J::Obj(vec![
57            ("unit".into(), J::s(unit)),
58            ("sdd".into(), J::s(inst.root.display().to_string())),
59            ("shape".into(), J::s(inst.shape.as_str())),
60            (
61                "consumes".into(),
62                J::Arr(consumes.into_iter().map(J::Str).collect()),
63            ),
64            ("readings".into(), J::Arr(readings)),
65        ]);
66        emit(&obj.dumps_indent2());
67    } else {
68        for item in &items {
69            match &item.region {
70                Some(r) => emit(&format!("{}  ::  {}", item.path.display(), r)),
71                None => emit(&item.path.display().to_string()),
72            }
73        }
74    }
75    Ok(0)
76}