use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;
pub use brep_kernel::family::{
effective_row, history_hash, is_identifier, rewrite_expressions, row_hash, FamilyColumn, FamilyRow, FamilyTable,
DERIVED_FROM_KEY, DOCUMENT_CLASS_KEY, FAMILY_SOURCE_KEY, FAMILY_TABLE_KEY, TEMPLATE_SOURCE_KEY,
};
pub const TEMPLATE_INPUTS_KEY: &str = "templateInputs";
pub const PART_ATTRIBUTES_KEY: &str = "partAttributes";
pub fn read_table(document: &Value) -> FamilyTable {
document
.get(FAMILY_TABLE_KEY)
.and_then(|block| serde_json::from_value(block.clone()).ok())
.unwrap_or_default()
}
pub fn write_table(document: &mut Value, table: &FamilyTable) -> Result<(), String> {
let object = document
.as_object_mut()
.ok_or("the family document is not a JSON object")?;
let block = serde_json::to_value(table).map_err(|e| e.to_string())?;
object.insert(FAMILY_TABLE_KEY.into(), block);
Ok(())
}
pub fn document_class(document: &Value) -> Option<&str> {
match document.get(DOCUMENT_CLASS_KEY).and_then(Value::as_str) {
Some(class @ ("family" | "template")) => Some(class),
_ => None,
}
}
pub fn assignments(expressions: &str) -> Vec<(String, String)> {
let mut cleaned = String::with_capacity(expressions.len());
for line in expressions.lines() {
cleaned.push_str(line.split("//").next().unwrap_or(""));
cleaned.push('\n');
}
cleaned
.split(';')
.filter_map(|statement| {
let (name, rhs) = statement.split_once('=')?;
let (name, rhs) = (name.trim(), rhs.trim());
(is_identifier(name) && !rhs.is_empty()).then(|| (name.to_string(), rhs.to_string()))
})
.collect()
}
fn expressions_of(document: &Value) -> &str {
document.get("expressions").and_then(Value::as_str).unwrap_or_default()
}
pub use brep_kernel::stable_json_hash;
pub fn template_hash(template: &Value) -> String {
let mut content = template.clone();
if let Some(object) = content.as_object_mut() {
object.remove("workbench");
}
format!("{:016x}", stable_json_hash(&content))
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FamilySource {
pub family: String,
pub part_number: String,
pub values_hash: String,
pub history_hash: String,
}
pub fn member_document(family: &Value, family_file: &str, row: &FamilyRow) -> Value {
let expressions = expressions_of(family);
let mut member = family.clone();
if let Some(object) = member.as_object_mut() {
object.remove(FAMILY_TABLE_KEY);
object.remove(DOCUMENT_CLASS_KEY);
object.remove(TEMPLATE_INPUTS_KEY);
object.remove(TEMPLATE_SOURCE_KEY);
object.remove(DERIVED_FROM_KEY);
object.insert(
"expressions".into(),
Value::String(rewrite_expressions(expressions, &row.values)),
);
let attributes = object
.entry(PART_ATTRIBUTES_KEY)
.or_insert_with(|| serde_json::json!({}));
if !attributes.is_object() {
*attributes = serde_json::json!({});
}
attributes["Part_Number"] = Value::String(row.part_number.trim().into());
if !row.description.trim().is_empty() {
attributes["Description"] = Value::String(row.description.trim().into());
}
let stamp = FamilySource {
family: family_file.to_string(),
part_number: row.part_number.trim().to_string(),
values_hash: row_hash(row),
history_hash: history_hash(family),
};
object.insert(
FAMILY_SOURCE_KEY.into(),
serde_json::to_value(stamp).unwrap_or(Value::Null),
);
}
member
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TemplateInput {
pub name: String,
#[serde(default)]
pub label: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub min: Option<f64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max: Option<f64>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub choices: Vec<String>,
}
impl TemplateInput {
pub fn shown(&self) -> &str {
if self.label.trim().is_empty() {
&self.name
} else {
&self.label
}
}
}
pub fn inputs_of(document: &Value) -> Vec<TemplateInput> {
document
.get(TEMPLATE_INPUTS_KEY)
.and_then(|block| serde_json::from_value(block.clone()).ok())
.unwrap_or_default()
}
pub fn default_value(template: &Value, input: &TemplateInput) -> String {
let own = assignments(expressions_of(template))
.into_iter()
.find(|(name, _)| *name == input.name)
.map(|(_, rhs)| rhs);
if let Some(first) = input.choices.first() {
return own
.filter(|rhs| input.choices.iter().any(|choice| choice.trim() == rhs))
.unwrap_or_else(|| first.clone());
}
own.unwrap_or_default()
}
pub fn value_problem(input: &TemplateInput, text: &str) -> Option<String> {
let text = text.trim();
let shown = input.shown();
if text.is_empty() {
return Some(format!("{shown}: enter a value"));
}
if !input.choices.is_empty() && !input.choices.iter().any(|choice| choice.trim() == text) {
return Some(format!("{shown}: choose one of {}", input.choices.join(", ")));
}
if let Ok(value) = text.parse::<f64>() {
if !value.is_finite() {
return Some(format!("{shown}: {text} is not a finite number"));
}
if let Some(min) = input.min.filter(|min| value < *min) {
return Some(format!("{shown}: {value} is below the minimum {min}"));
}
if let Some(max) = input.max.filter(|max| value > *max) {
return Some(format!("{shown}: {value} is above the maximum {max}"));
}
}
None
}
pub fn spin_out(
template: &Value,
template_file: &str,
values: &BTreeMap<String, String>,
) -> Result<(Value, BTreeMap<String, String>), String> {
let inputs = inputs_of(template);
if let Some(stray) = values.keys().find(|name| !inputs.iter().any(|i| &i.name == *name)) {
return Err(format!("'{stray}' is not an input of this template"));
}
let mut used = BTreeMap::new();
for input in &inputs {
let given = values.get(&input.name).map(|v| v.trim()).unwrap_or("");
let text = if given.is_empty() { default_value(template, input) } else { given.to_string() };
if let Some(problem) = value_problem(input, &text) {
return Err(problem);
}
used.insert(input.name.clone(), text.trim().to_string());
}
let mut copy = template.clone();
let object = copy.as_object_mut().ok_or("the template is not a JSON object")?;
object.insert(
"expressions".into(),
Value::String(rewrite_expressions(expressions_of(template), &used)),
);
object.remove(TEMPLATE_INPUTS_KEY);
object.remove(DOCUMENT_CLASS_KEY);
object.remove(FAMILY_TABLE_KEY);
object.remove(FAMILY_SOURCE_KEY);
object.remove(DERIVED_FROM_KEY);
object.insert(
TEMPLATE_SOURCE_KEY.into(),
serde_json::json!({ "template": template_file, "contentHash": template_hash(template) }),
);
Ok((copy, used))
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct CsvRows {
pub columns: Vec<String>,
pub has_revision: bool,
pub has_description: bool,
pub rows: Vec<FamilyRow>,
}
pub fn parse_csv(text: &str) -> Result<CsvRows, String> {
use brep_kernel::text::delimited;
let text = delimited::strip_bom(text);
let records = delimited::parse(text, delimited::sniff_separator(text), delimited::Strictness::Strict)?;
let mut records = records
.into_iter()
.filter(|record| !record.is_blank())
.map(|record| (record.line, record.fields));
let Some((_, header)) = records.next() else {
return Err("the CSV is empty — its first line names the columns".into());
};
enum Col {
Number,
Revision,
Description,
Value(String),
}
let mut columns = Vec::new();
let mut out = CsvRows::default();
for raw in &header {
let name = raw.trim();
let key = name.to_ascii_lowercase().replace(['_', ' '], "");
let col = match key.as_str() {
"partnumber" | "number" => Col::Number,
"revision" | "rev" => Col::Revision,
"description" => Col::Description,
_ if is_identifier(name) => Col::Value(name.to_string()),
_ => return Err(format!("column '{name}' is not an expression name (letters, digits, _ and $, not starting with a digit)")),
};
let duplicate = match &col {
Col::Number => columns.iter().any(|c| matches!(c, Col::Number)),
Col::Revision => out.has_revision,
Col::Description => out.has_description,
Col::Value(n) => out.columns.contains(n),
};
if duplicate {
return Err(format!("column '{name}' appears twice"));
}
match &col {
Col::Revision => out.has_revision = true,
Col::Description => out.has_description = true,
Col::Value(n) => out.columns.push(n.clone()),
Col::Number => {}
}
columns.push(col);
}
if !columns.iter().any(|c| matches!(c, Col::Number)) {
return Err("the CSV needs a 'part number' column".into());
}
let mut seen: BTreeMap<String, usize> = BTreeMap::new();
for (line, fields) in records {
if fields.len() > columns.len() && fields[columns.len()..].iter().any(|f| !f.trim().is_empty()) {
return Err(format!("line {line} has more fields than the header has columns"));
}
let mut row = FamilyRow::default();
for (col, field) in columns.iter().zip(fields.iter().chain(std::iter::repeat(&String::new()))) {
let field = field.trim();
match col {
Col::Number => row.part_number = field.to_string(),
Col::Revision => row.revision = field.to_string(),
Col::Description => row.description = field.to_string(),
Col::Value(name) => {
if !field.is_empty() {
row.values.insert(name.clone(), field.to_string());
}
}
}
}
if row.part_number.is_empty() {
return Err(format!("line {line} has no part number"));
}
let key = row.part_number.to_lowercase();
if let Some(first) = seen.get(&key) {
return Err(format!("line {line} repeats part number {} from line {first}", row.part_number));
}
seen.insert(key, line);
out.rows.push(row);
}
Ok(out)
}