Skip to main content
← Back to kicad-monkey Rust Docs kicad-monkey Rust Crate API Reference

kicad_monkey_core/
footprint_plot_contract.rs

1//! Typed standalone-footprint projection into the frozen a0 contract.
2
3use crate::plotter_contract::contract_footprint_plotter_operation;
4use crate::{
5    FootprintPlotDocument, PlotDocumentMetadata, PlotProjectionError, PlotProjectionErrorKind,
6};
7use kicad_monkey_contracts::JavaScriptSafeInteger;
8use kicad_monkey_contracts::generated::footprint_plot_document::{
9    FootprintPlotDocumentA0, FootprintPlotRecord, PlotterCoordinateSpace,
10};
11use kicad_monkey_contracts::validate_footprint_plot_document;
12
13pub fn project_footprint_plot_document_a0(
14    document: FootprintPlotDocument,
15    metadata: PlotDocumentMetadata,
16) -> Result<FootprintPlotDocumentA0, PlotProjectionError> {
17    let total_operations = u32::try_from(document.operations.len()).map_err(|_| {
18        PlotProjectionError::new(
19            PlotProjectionErrorKind::NumericRange,
20            "footprint operation count exceeds uint32",
21        )
22    })?;
23    let operations = document
24        .operations
25        .into_iter()
26        .enumerate()
27        .map(|(index, operation)| contract_footprint_plotter_operation(index, operation))
28        .collect::<Result<Vec<_>, _>>()?;
29    let contract = FootprintPlotDocumentA0 {
30        coordinate_space: PlotterCoordinateSpace {
31            unit: "nm".to_owned(),
32            y_axis: "down".to_owned(),
33        },
34        document_id: metadata.document_id,
35        generator: document.generator,
36        generator_version: document.generator_version,
37        records: vec![FootprintPlotRecord {
38            attr: document.attr,
39            descr: document.descr,
40            kind: "footprint".to_owned(),
41            layer: document.layer,
42            locked: document.locked,
43            name: document.name.clone(),
44            object_id: document.name,
45            operation_count: total_operations,
46            operations,
47            placed: document.placed,
48            tags: document.tags,
49            uuid: document.uuid,
50        }],
51        schema: "kicad.plotter_ir.a0".to_owned(),
52        source_kind: "MOD".to_owned(),
53        source_path: metadata.source_path,
54        total_operations,
55        version: JavaScriptSafeInteger::try_from(document.version).map_err(|error| {
56            PlotProjectionError::new(PlotProjectionErrorKind::NumericRange, error.to_string())
57        })?,
58    };
59    validate_footprint_plot_document(&contract).map_err(|error| {
60        PlotProjectionError::new(
61            PlotProjectionErrorKind::ContractValidation,
62            error.to_string(),
63        )
64    })?;
65    assert_exhaustive_operation_union(&contract.records[0].operations);
66    Ok(contract)
67}
68
69fn assert_exhaustive_operation_union(
70    operations: &[kicad_monkey_contracts::generated::footprint_plot_document::PlotterOperation],
71) {
72    use kicad_monkey_contracts::generated::footprint_plot_document::PlotterOperation;
73    for operation in operations {
74        match operation {
75            PlotterOperation::ThickSegmentOperation(_)
76            | PlotterOperation::ArcThreePointOperation(_)
77            | PlotterOperation::CircleOperation(_)
78            | PlotterOperation::RectOperation(_)
79            | PlotterOperation::PlotPolyOperation(_)
80            | PlotterOperation::BezierCurveOperation(_)
81            | PlotterOperation::TextOperation(_)
82            | PlotterOperation::PlotImageOperation(_)
83            | PlotterOperation::FlashPadCircleOperation(_)
84            | PlotterOperation::FlashPadOvalOperation(_)
85            | PlotterOperation::FlashPadRectOperation(_)
86            | PlotterOperation::FlashPadRoundRectOperation(_)
87            | PlotterOperation::FlashPadCustomOperation(_)
88            | PlotterOperation::FlashPadTrapezOperation(_) => {}
89        }
90    }
91}