use pathbuf in spec; move output to render

This commit is contained in:
2026-02-17 03:59:16 -07:00
parent a813c4cb53
commit ae5bf37df1
3 changed files with 141 additions and 134 deletions

View File

@@ -2,28 +2,27 @@ use crate::{LogLevel, info, verb};
use regex::Regex;
use serde_json::{Map, Value, json};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::{env, fmt, fs};
use walkdir::WalkDir;
use yaml_serde;
#[derive(Debug)]
pub struct Specification {
pub partitional: String,
pub regional: String,
pub common: String,
pub zonal: String,
pub device: String,
pub partitional: PathBuf,
pub regional: PathBuf,
pub common: PathBuf,
pub zonal: PathBuf,
pub device: PathBuf,
pub compiled: Value,
}
impl Specification {
pub fn build(
partitional: String,
regional: String,
common: String,
zonal: String,
device: String,
partitional: PathBuf,
regional: PathBuf,
common: PathBuf,
zonal: PathBuf,
device: PathBuf,
) -> Result<Self, Box<dyn std::error::Error>> {
let json_values: Vec<Value> = [&partitional, &regional, &common, &zonal, &device]
.iter()
@@ -31,7 +30,6 @@ impl Specification {
.map(|content| yaml_serde::from_str::<Value>(&content))
.collect::<Result<Vec<Value>, _>>()?;
// Merge all objects under a single key
let merged_map: Map<String, Value> = json_values
.into_iter()
.filter_map(|v| v.as_object().cloned())
@@ -39,21 +37,20 @@ impl Specification {
.flatten()
.collect();
// Create the final merged object
let compiled = json!({"data": merged_map});
Ok(Self {
partitional: String::from(partitional),
regional: String::from(regional),
common: String::from(common),
zonal: String::from(zonal),
device: String::from(device),
partitional,
regional,
common,
zonal,
device,
compiled,
})
}
pub fn get_layer(&self) -> String {
PathBuf::from(&self.device)
self.device
.parent()
.unwrap()
.file_name()
@@ -70,13 +67,18 @@ impl fmt::Display for Specification {
write!(
f,
"Specification:\nPartitional: {}\nRegional: {}\nCommon: {}\nZonal: {}\nDevice: {}\nCompiled:\n{}",
self.partitional, self.regional, self.common, self.zonal, self.device, self.compiled
self.partitional.display(),
self.regional.display(),
self.common.display(),
self.zonal.display(),
self.device.display(),
self.compiled
)
}
}
pub fn compile(pattern: &Regex, spec_path: &String, dbg: LogLevel) -> Vec<Specification> {
let spec_list: Vec<String> = filter_specs(pattern, get_specs(spec_path, dbg));
let spec_list: Vec<PathBuf> = filter_specs(pattern, get_specs(spec_path, dbg));
info!(
dbg,
"Matched {} devices against '{}'",
@@ -84,14 +86,14 @@ pub fn compile(pattern: &Regex, spec_path: &String, dbg: LogLevel) -> Vec<Specif
&pattern
);
for spec in &spec_list {
verb!(dbg, " | {}", spec);
verb!(dbg, " | {}", spec.display());
}
let mut specifications: Vec<Specification> = Vec::new();
for spec in spec_list {
let zonal: String = get_zonal(&spec);
let common: String = get_common(&spec);
let regional: String = get_regional(&spec);
let partitional: String = get_partional(&common, &regional);
let zonal = get_zonal(&spec);
let common = get_common(&spec);
let regional = get_regional(&spec);
let partitional = get_partitional(&common, &regional);
specifications.push(
match Specification::build(partitional, regional, common, zonal, spec) {
Ok(compiled_spec) => compiled_spec,
@@ -102,25 +104,25 @@ pub fn compile(pattern: &Regex, spec_path: &String, dbg: LogLevel) -> Vec<Specif
specifications
}
fn get_partional(common: &str, regional: &str) -> String {
fn get_partitional(common: &PathBuf, regional: &PathBuf) -> PathBuf {
let re = Regex::new(r"partition: ([^\n\r$]+)").expect("failed");
[common, regional]
.iter()
.find_map(|&path| {
.find_map(|path| {
fs::read_to_string(path).ok().and_then(|contents| {
re.captures(&contents).and_then(|captures| {
captures
.get(1)
.map(|matched| format!("./spec/common/{}.yaml", matched.as_str().trim()))
captures.get(1).map(|matched| {
PathBuf::from(format!("./spec/common/{}.yaml", matched.as_str().trim()))
})
})
})
})
.unwrap_or_else(|| format!("./spec/common/default.yaml"))
.unwrap_or_else(|| PathBuf::from("./spec/common/default.yaml"))
}
fn get_regional(path: &str) -> String {
let dir_name = PathBuf::from(path)
fn get_regional(path: &PathBuf) -> PathBuf {
let dir_name = path
.parent()
.unwrap()
.file_name()
@@ -128,59 +130,48 @@ fn get_regional(path: &str) -> String {
.to_string_lossy()
.into_owned();
let new_path = PathBuf::from(path)
.parent()
path.parent()
.unwrap()
.parent()
.unwrap()
.join("common")
.join(format!("{}.yaml", &dir_name[0..2]));
String::from(new_path.to_str().unwrap())
.join(format!("{}.yaml", &dir_name[0..2]))
}
fn get_common(spec: &str) -> String {
let path: PathBuf = PathBuf::from_str(&spec).expect("Path was invalid");
String::from(path.with_file_name("common.yaml").to_str().unwrap())
fn get_common(spec: &PathBuf) -> PathBuf {
spec.with_file_name("common.yaml")
}
fn get_zonal(spec: &str) -> String {
let path: PathBuf = PathBuf::from_str(&spec).expect("Path was invalid");
fn get_zonal(spec: &PathBuf) -> PathBuf {
let zone = spec
.file_name()
.and_then(|f| f.to_str())
.and_then(|f| f.split_once('-'))
.map(|(zone, _)| spec.with_file_name(zone))
.unwrap_or_else(|| spec.with_file_name("common.yaml"));
String::from(format!(
"{}.common.yaml",
path.file_name()
.and_then(|filename| filename.to_str())
.and_then(|filename| filename.split_once('-'))
.map(|(zone, _)| path.with_file_name(zone))
.unwrap_or_else(|| path.with_file_name("common.yaml"))
.to_str()
.unwrap()
.to_string(),
))
PathBuf::from(format!("{}.common.yaml", zone.to_str().unwrap()))
}
fn filter_specs(pattern: &Regex, spec_list: Vec<String>) -> Vec<String> {
fn filter_specs(pattern: &Regex, spec_list: Vec<PathBuf>) -> Vec<PathBuf> {
spec_list
.into_iter()
.filter(|spec| {
Path::new(spec)
.file_name()
spec.file_name()
.and_then(|name| name.to_str())
.map_or(false, |name| pattern.is_match(name))
})
.collect()
}
fn get_specs(spec_path: &String, dbg: LogLevel) -> Vec<String> {
fn get_specs(spec_path: &String, dbg: LogLevel) -> Vec<PathBuf> {
let mut result = Vec::new();
let root = Path::new(&spec_path);
for entry in WalkDir::new(root).into_iter().filter_map(|e| e.ok()) {
if let Ok(path) = entry.path().strip_prefix(root) {
let path_str = path.to_string_lossy().to_string();
if !path_str.contains("common") && path_str.ends_with("yaml") {
result.push(format!("{}/{}", spec_path, path_str));
result.push(root.join(path));
}
}
}