1use serde_json::Value;
14
15pub mod edit;
16pub mod grid;
17pub mod index;
18pub mod layout;
19pub mod schematic;
20pub mod single_line;
21pub mod state;
22pub mod text;
23pub mod topology;
24pub mod tree;
25
26pub trait Locations {
29 fn location(&self, id: &str) -> Value;
32 fn files(&self) -> Vec<Value>;
34}
35
36pub fn mrid_of(value: &str) -> &str {
38 value.rsplit_once('#').map_or(value, |(_, f)| f)
39}
40
41#[cfg(test)]
43pub(crate) mod testing {
44 use std::collections::HashMap;
45 use std::path::{Path, PathBuf};
46 use std::sync::Arc;
47
48 use cimmodel::CimDataset;
49 use serde_json::{Value, json};
50
51 use crate::Locations;
52 use crate::grid::{self, Grid, Model};
53 use crate::tree::{self, Tree};
54
55 pub fn collect(dir: &Path) -> Vec<(PathBuf, Arc<str>)> {
57 let mut paths: Vec<PathBuf> = std::fs::read_dir(dir)
58 .map(|d| d.flatten().map(|e| e.path()).collect())
59 .unwrap_or_default();
60 paths.sort();
61 paths
62 .into_iter()
63 .filter_map(|p| {
64 let text = std::fs::read_to_string(&p).ok()?;
65 cimmodel::decode::is_cim(&text).then(|| (p, Arc::from(text)))
66 })
67 .collect()
68 }
69
70 pub struct Set {
71 pub grid: Grid,
72 pub tree: Tree,
73 files: Vec<String>,
74 defs: HashMap<String, (usize, bool)>,
76 }
77
78 impl Locations for Set {
79 fn location(&self, id: &str) -> Value {
80 match self.defs.get(id) {
81 Some(&(f, _)) => json!({ "uri": self.files[f] }),
82 None => Value::Null,
83 }
84 }
85 fn files(&self) -> Vec<Value> {
86 self.files.iter().map(|f| json!(f)).collect()
87 }
88 }
89
90 pub fn validate(files: &[(PathBuf, Arc<str>)]) -> Set {
93 let datasets: Vec<CimDataset> = files
94 .iter()
95 .map(|(_, t)| CimDataset::decode_str(t).unwrap())
96 .collect();
97 let mut defs: HashMap<String, (usize, bool)> = HashMap::new();
98 for (f, ds) in datasets.iter().enumerate() {
99 for (id, el) in &ds.entries {
100 let slot = defs.entry(id.to_string()).or_insert((f, el.defined()));
101 if el.defined() && !slot.1 {
102 *slot = (f, true);
103 }
104 }
105 }
106 let cfg = cimvalidation::combined_config(&datasets, None, None, true, true, Vec::new());
107 let model = Model::new(&datasets);
108 let (mut grid, mut tree) = (grid::build(&model), tree::build(&model));
109 let found = cimvalidation::validate_files_traced(datasets, &cfg);
110 grid.add_findings(&found);
111 tree.add_findings(&found);
112 let files = files
113 .iter()
114 .map(|(p, _)| p.to_string_lossy().into_owned())
115 .collect();
116 Set {
117 grid,
118 tree,
119 files,
120 defs,
121 }
122 }
123}