use super::documents::{declare_document, DocTree, TagRule, Vocabulary};
use super::element::{attributes, elements};
use super::{Declared, Family};
pub use super::{fragment, json, El, Json, Value};
elements! {
cam: children;
slice: children;
source: leaf;
design: leaf;
stock: leaf;
part: leaf;
machine: leaf;
fixture: leaf;
operation: leaf;
runs_on = "runsOn": leaf;
spool: leaf;
}
attributes! {
pub trait FabAttrs {
file_id;
name;
material_id;
thickness_mm;
x_mm;
y_mm;
footprint_x_mm;
footprint_y_mm;
at_x_mm;
at_y_mm;
post;
max_spindle_rpm;
max_feed_mm_per_min;
spindle_power_kw;
travel_mm;
w_mm;
d_mm;
z_mm;
id;
op;
profile;
enabled;
params;
tabs;
region;
unit;
channel;
diameter_mm;
device;
}
}
pub(crate) const FAMILY: Family = Family { module: "fab", roots: &["cam", "slice"], declare };
pub const CAM_REF_FIELDS: &[&str] = &["fileId", "name"];
pub const CAM_STOCK_FIELDS: &[&str] = &["materialId", "thicknessMm", "xMm", "yMm"];
pub const CAM_PART_FIELDS: &[&str] = &["footprintXMm", "footprintYMm", "atXMm", "atYMm"];
pub const CAM_MACHINE_FIELDS: &[&str] = &["post", "maxSpindleRpm", "maxFeedMmPerMin", "spindlePowerKw", "travelMm"];
pub const CAM_FIXTURE_FIELDS: &[&str] = &["name", "xMm", "yMm", "wMm", "dMm", "zMm"];
pub const CAM_OPERATION_FIELDS: &[&str] = &["id", "op", "profile", "enabled", "params", "tabs", "region"];
pub const TARGET_FIELDS: &[&str] = &["unit", "channel", "name"];
pub const SPOOL_FIELDS: &[&str] = &["materialId", "diameterMm"];
pub const SLICE_FIELDS: &[&str] = &["profile", "params", "device"];
const CAM_PARENT: &[&str] = &["cam"];
const SLICE_PARENT: &[&str] = &["slice"];
const REF_REQUIRED: &[&str] = &["fileId", "name"];
pub const CAM: Vocabulary = Vocabulary {
format: "cam",
noun: "a machining setup",
root: "cam",
tags: &[
TagRule::new("cam", &[]).attrs(&[]),
TagRule::new("source", CAM_PARENT).single().required(REF_REQUIRED).attrs(CAM_REF_FIELDS),
TagRule::new("design", CAM_PARENT).single().required(REF_REQUIRED).attrs(CAM_REF_FIELDS),
TagRule::new("stock", CAM_PARENT).single().attrs(CAM_STOCK_FIELDS),
TagRule::new("part", CAM_PARENT).single().required(&["footprintXMm", "footprintYMm"]).attrs(CAM_PART_FIELDS),
TagRule::new("machine", CAM_PARENT).single().attrs(CAM_MACHINE_FIELDS),
TagRule::new("fixture", CAM_PARENT).required(CAM_FIXTURE_FIELDS).attrs(CAM_FIXTURE_FIELDS),
TagRule::new("operation", CAM_PARENT).key("id").required(&["id", "op", "profile"]).attrs(CAM_OPERATION_FIELDS),
TagRule::new("runsOn", CAM_PARENT).single().required(TARGET_FIELDS).attrs(TARGET_FIELDS),
],
from_tree: cam_from_tree,
};
pub const SLICE: Vocabulary = Vocabulary {
format: "slice",
noun: "a slicing setup",
root: "slice",
tags: &[
TagRule::new("slice", &[]).required(&["profile"]).attrs(SLICE_FIELDS),
TagRule::new("source", SLICE_PARENT).single().required(REF_REQUIRED).attrs(CAM_REF_FIELDS),
TagRule::new("design", SLICE_PARENT).single().required(REF_REQUIRED).attrs(CAM_REF_FIELDS),
TagRule::new("spool", SLICE_PARENT).single().attrs(SPOOL_FIELDS),
TagRule::new("machine", SLICE_PARENT).single().required(TARGET_FIELDS).attrs(TARGET_FIELDS),
],
from_tree: slice_from_tree,
};
fn cam_from_tree(t: &DocTree) -> Result<Json, String> {
let mut doc = Json::obj().with("operations", Json::Arr(Vec::new()));
for tag in ["source", "design", "stock", "part", "machine"] {
if let Some(c) = t.single(tag) {
doc.set(tag, c.record());
}
}
let fixtures: Vec<Json> = t.children.iter().filter(|c| c.tag == "fixture").map(DocTree::record).collect();
if !fixtures.is_empty() {
doc.set("fixtures", fixtures);
}
doc.set("operations", t.children.iter().filter(|c| c.tag == "operation").map(DocTree::record).collect::<Vec<_>>());
if let Some(target) = t.single("runsOn") {
doc.set("runsOn", target.record());
}
Ok(doc)
}
fn slice_from_tree(t: &DocTree) -> Result<Json, String> {
let mut doc = t.record();
for tag in ["source", "design", "spool", "machine"] {
if let Some(c) = t.single(tag) {
doc.set(tag, c.record());
}
}
Ok(doc)
}
fn declare(root: El) -> Result<Declared, String> {
declare_document(&root, if root.tag == "cam" { &CAM } else { &SLICE })
}