use serde_json::Value;
pub mod edit;
pub mod grid;
pub mod index;
pub mod layout;
pub mod refresh;
pub mod schematic;
pub mod single_line;
pub mod state;
pub mod text;
pub mod topology;
pub mod tree;
pub trait Locations {
fn location(&self, id: &str) -> Value;
fn files(&self) -> Vec<Value>;
}
pub fn mrid_of(value: &str) -> &str {
value.rsplit_once('#').map_or(value, |(_, f)| f)
}
#[cfg(test)]
pub(crate) mod testing {
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use cimmodel::CimDataset;
use serde_json::{Value, json};
use crate::Locations;
use crate::grid::{self, Grid, Model};
use crate::tree::{self, Tree};
pub fn collect(dir: &Path) -> Vec<(PathBuf, Arc<str>)> {
let mut paths: Vec<PathBuf> = std::fs::read_dir(dir)
.map(|d| d.flatten().map(|e| e.path()).collect())
.unwrap_or_default();
paths.sort();
paths
.into_iter()
.filter_map(|p| {
let text = std::fs::read_to_string(&p).ok()?;
cimmodel::decode::is_cim(&text).then(|| (p, Arc::from(text)))
})
.collect()
}
pub struct Set {
pub grid: Grid,
pub tree: Tree,
files: Vec<String>,
defs: HashMap<String, (usize, bool)>,
}
impl Locations for Set {
fn location(&self, id: &str) -> Value {
match self.defs.get(id) {
Some(&(f, _)) => json!({ "uri": self.files[f] }),
None => Value::Null,
}
}
fn files(&self) -> Vec<Value> {
self.files.iter().map(|f| json!(f)).collect()
}
}
pub fn validate(files: &[(PathBuf, Arc<str>)]) -> Set {
let datasets: Vec<CimDataset> = files
.iter()
.map(|(_, t)| CimDataset::decode_str(t).unwrap())
.collect();
let mut defs: HashMap<String, (usize, bool)> = HashMap::new();
for (f, ds) in datasets.iter().enumerate() {
for (id, el) in &ds.entries {
let slot = defs.entry(id.to_string()).or_insert((f, el.defined()));
if el.defined() && !slot.1 {
*slot = (f, true);
}
}
}
let cfg = cimvalidation::combined_config(&datasets, None, None, true, true, Vec::new());
let model = Model::new(&datasets);
let (mut grid, mut tree) = (grid::build(&model), tree::build(&model));
let found = cimvalidation::validate_files_traced(datasets, &cfg);
grid.add_findings(&found);
tree.add_findings(&found);
let files = files
.iter()
.map(|(p, _)| p.to_string_lossy().into_owned())
.collect();
Set {
grid,
tree,
files,
defs,
}
}
}