1use serde_json::Value;
10
11pub mod grid;
12pub mod layout;
13pub mod tree;
14
15pub trait Locations {
18 fn location(&self, id: &str) -> Value;
21 fn files(&self) -> Vec<Value>;
23}
24
25pub fn mrid_of(value: &str) -> &str {
27 value.rsplit_once('#').map_or(value, |(_, f)| f)
28}
29
30#[cfg(test)]
32pub(crate) mod testing {
33 use std::collections::HashMap;
34 use std::path::{Path, PathBuf};
35 use std::sync::Arc;
36
37 use cimmodel::CimDataset;
38 use serde_json::{Value, json};
39
40 use crate::Locations;
41 use crate::grid::{self, Grid, Model};
42 use crate::tree::{self, Tree};
43
44 pub fn collect(dir: &Path) -> Vec<(PathBuf, Arc<str>)> {
46 let mut paths: Vec<PathBuf> = std::fs::read_dir(dir)
47 .map(|d| d.flatten().map(|e| e.path()).collect())
48 .unwrap_or_default();
49 paths.sort();
50 paths
51 .into_iter()
52 .filter_map(|p| {
53 let text = std::fs::read_to_string(&p).ok()?;
54 cimmodel::decode::is_cim(&text).then(|| (p, Arc::from(text)))
55 })
56 .collect()
57 }
58
59 pub struct Set {
60 pub grid: Grid,
61 pub tree: Tree,
62 files: Vec<String>,
63 defs: HashMap<String, (usize, bool)>,
65 }
66
67 impl Locations for Set {
68 fn location(&self, id: &str) -> Value {
69 match self.defs.get(id) {
70 Some(&(f, _)) => json!({ "uri": self.files[f] }),
71 None => Value::Null,
72 }
73 }
74 fn files(&self) -> Vec<Value> {
75 self.files.iter().map(|f| json!(f)).collect()
76 }
77 }
78
79 pub fn validate(files: &[(PathBuf, Arc<str>)]) -> Set {
82 let datasets: Vec<CimDataset> = files.iter().map(|(_, t)| CimDataset::decode_str(t).unwrap()).collect();
83 let mut defs: HashMap<String, (usize, bool)> = HashMap::new();
84 for (f, ds) in datasets.iter().enumerate() {
85 for (id, el) in &ds.entries {
86 let slot = defs.entry(id.to_string()).or_insert((f, el.defined()));
87 if el.defined() && !slot.1 {
88 *slot = (f, true);
89 }
90 }
91 }
92 let cfg = cimvalidation::combined_config(&datasets, None, None, true, true, Vec::new());
93 let model = Model::new(&datasets);
94 let (mut grid, mut tree) = (grid::build(&model), tree::build(&model));
95 let found = cimvalidation::validate_files_traced(datasets, &cfg);
96 grid.add_findings(&found);
97 tree.add_findings(&found);
98 let files = files.iter().map(|(p, _)| p.to_string_lossy().into_owned()).collect();
99 Set { grid, tree, files, defs }
100 }
101}