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

kicad_monkey_contracts/
compiled_graph_contract.rs

1//! Strict promoted boundary for the generated compiled-graph transport DTO.
2
3use crate::ValidationError;
4use crate::generated::compiled_schematic_graph::CompiledSchematicGraphA0;
5use crate::validation_error;
6use std::fmt;
7
8const SCHEMA: &str = "kicad_monkey.compiled_schematic_graph.a0";
9const DOCUMENT_TYPE: &str = "sch.compiled_schematic_graph";
10const IDENTITY_NAMESPACE: &str = "sch.compiled_schematic_graph.a0";
11
12/// Decode JSON and enforce every TypeSpec literal that Typify represents as a string.
13pub fn decode_compiled_schematic_graph_a0(
14    source: &[u8],
15) -> Result<CompiledSchematicGraphA0, CompiledGraphDecodeError> {
16    let document = serde_json::from_slice(source).map_err(CompiledGraphDecodeError::Transport)?;
17    validate_compiled_schematic_graph_contract(&document)
18        .map_err(CompiledGraphDecodeError::Validation)?;
19    Ok(document)
20}
21
22/// Enforce the a0 envelope and row-family discriminators after structural decoding.
23pub fn validate_compiled_schematic_graph_contract(
24    document: &CompiledSchematicGraphA0,
25) -> Result<(), ValidationError> {
26    require_literal(&document.schema, SCHEMA, "$.schema")?;
27    require_literal(&document.type_, DOCUMENT_TYPE, "$.type")?;
28    require_literal(
29        &document.identity_namespace,
30        IDENTITY_NAMESPACE,
31        "$.identity_namespace",
32    )?;
33
34    validate_row_types(
35        &document.unit_definitions,
36        "unit_definitions",
37        "sch.unit_definition",
38        |row| &row.type_,
39    )?;
40    validate_row_types(
41        &document.page_definitions,
42        "page_definitions",
43        "sch.page_definition",
44        |row| &row.type_,
45    )?;
46    validate_row_types(
47        &document.unit_occurrences,
48        "unit_occurrences",
49        "sch.unit_occurrence",
50        |row| &row.type_,
51    )?;
52    validate_row_types(
53        &document.page_occurrences,
54        "page_occurrences",
55        "sch.page_occurrence",
56        |row| &row.type_,
57    )?;
58    validate_row_types(
59        &document.hierarchy_occurrences,
60        "hierarchy_occurrences",
61        "sch.hierarchy_occurrence",
62        |row| &row.type_,
63    )?;
64    validate_row_types(
65        &document.component_occurrences,
66        "component_occurrences",
67        "sch.component_occurrence",
68        |row| &row.type_,
69    )?;
70    validate_row_types(
71        &document.local_net_occurrences,
72        "local_net_occurrences",
73        "sch.local_net_occurrence",
74        |row| &row.type_,
75    )?;
76    validate_row_types(
77        &document.terminal_occurrences,
78        "terminal_occurrences",
79        "sch.terminal_occurrence",
80        |row| &row.type_,
81    )?;
82    validate_row_types(
83        &document.hierarchy_terminal_bindings,
84        "hierarchy_terminal_bindings",
85        "sch.hierarchy_terminal_binding",
86        |row| &row.type_,
87    )?;
88    validate_row_types(
89        &document.graphical_artifact_links,
90        "graphical_artifact_links",
91        "sch.graphical_artifact_link",
92        |row| &row.type_,
93    )?;
94    for (index, link) in document.graphical_artifact_links.iter().enumerate() {
95        require_literal(
96            &link.artifact_key,
97            "sch.dwg_scene",
98            format!("$.graphical_artifact_links[{index}].artifact_key"),
99        )?;
100    }
101    Ok(())
102}
103
104fn validate_row_types<T>(
105    rows: &[T],
106    collection: &str,
107    expected: &'static str,
108    row_type: impl Fn(&T) -> &str,
109) -> Result<(), ValidationError> {
110    for (index, row) in rows.iter().enumerate() {
111        require_literal(
112            row_type(row),
113            expected,
114            format!("$.{collection}[{index}].type"),
115        )?;
116    }
117    Ok(())
118}
119
120fn require_literal(
121    actual: &str,
122    expected: &'static str,
123    path: impl Into<String>,
124) -> Result<(), ValidationError> {
125    if actual == expected {
126        Ok(())
127    } else {
128        Err(validation_error(
129            "unsupported_contract",
130            path,
131            "value does not match the registered a0 contract literal",
132        ))
133    }
134}
135
136/// Failure from either strict JSON transport decoding or literal validation.
137#[derive(Debug)]
138pub enum CompiledGraphDecodeError {
139    Transport(serde_json::Error),
140    Validation(ValidationError),
141}
142
143impl fmt::Display for CompiledGraphDecodeError {
144    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
145        match self {
146            Self::Transport(error) => write!(formatter, "compiled graph transport: {error}"),
147            Self::Validation(error) => write!(formatter, "compiled graph validation: {error}"),
148        }
149    }
150}
151
152impl std::error::Error for CompiledGraphDecodeError {
153    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
154        match self {
155            Self::Transport(error) => Some(error),
156            Self::Validation(error) => Some(error),
157        }
158    }
159}