use crate::query;
use crate::query::why::Why;
use super::{j_bus_row, j_table, push_heuristic, J};
pub(crate) fn tests_model_to_json(model: &query::TestsModel) -> String {
let mut fields: Vec<(&'static str, J)> = vec![
("schema_version", J::UInt(query::SCHEMA_VERSION)),
("status", J::Str("resolved".to_string())),
("query", J::Str(model.query.clone())),
("symbol", J::Str(model.symbol.clone())),
(
"defFiles",
J::Arr(model.def_files.iter().map(|f| J::Str(f.clone())).collect()),
),
(
"rows",
J::Arr(
model
.rows
.iter()
.map(|r| {
let mut fields = vec![
("file", J::Str(r.file.clone())),
(
"testDefs",
J::Arr(r.test_defs.iter().map(|d| J::Str(d.clone())).collect()),
),
(
"lines",
J::Arr(r.lines.iter().map(|l| J::UInt(*l as u64)).collect()),
),
("refCount", J::UInt(r.ref_count as u64)),
];
push_heuristic(&mut fields, r.heuristic, r.tier);
let why = if r.via == query::TestVia::Project {
fields.push(("via", J::Str("project".to_string())));
Why::TestProject
} else {
Why::TestAttribute
};
fields.push(("why", J::Str(why.as_str().to_string())));
J::Obj(fields)
})
.collect(),
),
),
("testFileCount", J::UInt(model.test_file_count as u64)),
("refCount", J::UInt(model.ref_count as u64)),
(
"heuristicFileCount",
J::UInt(model.heuristic_file_count as u64),
),
(
"heuristicRefCount",
J::UInt(model.heuristic_ref_count as u64),
),
];
if model.bus.total != 0 {
fields.push(("bus-hop", j_table(&model.bus, j_bus_row)));
}
fields.push(("outcome", J::Str(query::Outcome::Hit.as_str().to_string())));
J::Obj(fields).to_json_string()
}