1#[path = "manufacturing_output_observations_agreement.rs"]
3mod agreement;
4#[path = "manufacturing_output_observations_gerber.rs"]
5mod gerber;
6pub use agreement::{ManufacturingFunctionAgreement, compare_job_and_layer_file_function};
7#[path = "manufacturing_output_observations_job.rs"]
8mod job;
9#[path = "manufacturing_output_observations_types.rs"]
10mod types;
11pub use types::*;
12
13use sha2::{Digest, Sha256};
14
15pub fn observe_manufacturing_output(
17 bytes: &[u8],
18 format: ManufacturingOutputFormat,
19 limits: ManufacturingObservationLimits,
20) -> Result<ManufacturingOutputObservations, String> {
21 limits.require_valid()?;
22 if bytes.len() > limits.source_bytes {
23 return Err("Manufacturing observation source exceeds its byte budget".into());
24 }
25 let mut result = ManufacturingOutputObservations {
26 sha256: Sha256::digest(bytes)
27 .iter()
28 .map(|byte| format!("{byte:02x}"))
29 .collect(),
30 format,
31 files: Vec::new(),
32 diagnostics: Vec::new(),
33 };
34 let source = match std::str::from_utf8(bytes) {
35 Ok(source) => source,
36 Err(error) => {
37 result.diagnose(0..bytes.len(), format!("Source is not UTF-8: {error}"));
38 return Ok(result);
39 }
40 };
41 match format {
42 ManufacturingOutputFormat::GerberLayer => gerber::observe(source, limits, &mut result),
43 ManufacturingOutputFormat::GerberJob => job::observe(source, limits, &mut result),
44 }
45 for file in &mut result.files {
47 for attribute in &mut file.attributes {
48 attribute.qualified_context &= file.coherent;
49 }
50 }
51 Ok(result)
52}
53
54impl ManufacturingOutputObservations {
55 pub(super) fn diagnose(&mut self, range: std::ops::Range<usize>, message: String) {
56 self.diagnostics
57 .push(ManufacturingObservationDiagnostic { range, message });
58 }
59}
60
61pub(super) fn attribute(
62 name: &str,
63 raw: String,
64 range: std::ops::Range<usize>,
65 origin: ManufacturingObservationOrigin,
66 in_header: bool,
67) -> ManufacturingAttributeObservation {
68 let value = if in_header {
69 match name {
70 "FilePolarity" => match raw.as_str() {
71 "Positive" => Some(ManufacturingAttributeValue::Polarity(
72 ManufacturingPolarity::Positive,
73 )),
74 "Negative" => Some(ManufacturingAttributeValue::Polarity(
75 ManufacturingPolarity::Negative,
76 )),
77 _ => None,
78 },
79 "FileFunction" => function(&raw, origin).map(ManufacturingAttributeValue::Function),
80 _ => None,
81 }
82 } else {
83 None
84 };
85 ManufacturingAttributeObservation {
86 name: name.into(),
87 raw,
88 range,
89 origin,
90 value,
91 qualified_context: in_header,
92 }
93}
94
95fn function(
96 raw: &str,
97 origin: ManufacturingObservationOrigin,
98) -> Option<ManufacturingFileFunction> {
99 let fields: Vec<_> = raw.split(',').collect();
100 match fields.as_slice() {
101 ["Copper", layer, side] | ["Copper", layer, side, "Plane" | "Signal" | "Mixed"] => {
102 let layer = positive(layer.strip_prefix('L')?)?;
103 let side = match *side {
104 "Top" => ManufacturingCopperSide::Top,
105 "Bot" => ManufacturingCopperSide::Bottom,
106 "Inr" => ManufacturingCopperSide::Inner,
107 _ => return None,
108 };
109 Some(ManufacturingFileFunction::Copper { layer, side })
110 }
111 [
112 kind @ ("Plated" | "NonPlated" | "XPlated"),
113 from,
114 to,
115 process,
116 ] => drill(kind, from, to, process, None, origin),
117 [
118 kind @ ("Plated" | "NonPlated"),
119 from,
120 to,
121 process,
122 operation @ ("Drill" | "Rout" | "Mixed"),
123 ] => drill(kind, from, to, process, Some(operation), origin),
124 _ => None,
125 }
126}
127
128fn positive(raw: &str) -> Option<u32> {
129 if raw.is_empty() || !raw.bytes().all(|byte| byte.is_ascii_digit()) {
130 return None;
131 }
132 raw.parse::<u32>().ok().filter(|value| *value > 0)
133}
134
135fn drill(
136 kind: &str,
137 from: &str,
138 to: &str,
139 process: &str,
140 operation: Option<&str>,
141 origin: ManufacturingObservationOrigin,
142) -> Option<ManufacturingFileFunction> {
143 let allowed = matches!(
144 (kind, process),
145 ("Plated", "PTH" | "Blind" | "Buried") | ("NonPlated", "NPTH")
146 ) || (kind == "XPlated"
147 && process == "XTH"
148 && origin == ManufacturingObservationOrigin::GerberJob);
149 if !allowed {
150 return None;
151 }
152 let (from, to) = (positive(from)?, positive(to)?);
153 if from > to {
154 return None;
155 }
156 Some(ManufacturingFileFunction::Drill {
157 from,
158 to,
159 plating: kind.into(),
160 process: process.into(),
161 operation: operation.map(str::to_owned),
162 })
163}
164
165#[cfg(test)]
166#[path = "manufacturing_output_observations_tests.rs"]
167mod tests;