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

kicad_monkey_core/pcb/primary/
batch.rs

1//! Invocation-local primary acquisition; no Source view or persistent cache is retained.
2
3use super::*;
4
5/// Independent graphical results in Reference, Value, Description order.
6/// Only the existing unpositioned build-capability errors are eligible for
7/// drawing recovery; source errors and resource limits must remain fatal.
8pub type PcbFootprintPrimaryFieldResults =
9    [(&'static str, Result<Option<PcbFootprintProperty>, Error>); 3];
10
11/// One footprint's strict effective scalars and independently fallible drawings.
12/// This is an operation result over existing Source records, not a saved carrier.
13#[derive(Debug)]
14pub struct PcbFootprintPrimaryBatch {
15    version: i64,
16    values: BTreeMap<String, String>,
17    fields: PcbFootprintPrimaryFieldResults,
18}
19
20impl PcbFootprintPrimaryBatch {
21    /// Source file version acquired with the same reader limits as the fields.
22    pub fn version(&self) -> i64 {
23        self.version
24    }
25
26    /// Strict effective values, distinguishing missing slots from present blanks.
27    pub fn values(&self) -> &BTreeMap<String, String> {
28        &self.values
29    }
30
31    /// Consume the scalar map and independent ordered field results.
32    pub fn into_parts(self) -> (BTreeMap<String, String>, PcbFootprintPrimaryFieldResults) {
33        (self.values, self.fields)
34    }
35}
36
37impl PcbView<'_> {
38    /// Acquire ordered primary history once, then reduce each graphical slot
39    /// independently with the existing reducer. Acquisition validates all
40    /// primary leaves before any drawing capability error can be recovered.
41    /// Original declarations, reader limits and singular operations are unchanged.
42    pub fn effective_footprint_primary_batch(
43        &self,
44        footprint_index: usize,
45    ) -> Result<PcbFootprintPrimaryBatch, Error> {
46        let (properties, texts) = event_indices(self, footprint_index)?;
47        let version = self.metadata()?.version;
48        let events = events_from_indexed(self.source, properties, texts, version, self.limits)?;
49        let footprint =
50            footprint_from_span(self.source, &self.footprints[footprint_index], self.limits)?;
51        let mut values = BTreeMap::new();
52        let mut histories: [Vec<Event>; 3] = std::array::from_fn(|_| Vec::new());
53        for event in events {
54            let index = PRIMARY
55                .iter()
56                .position(|name| *name == event.row.name)
57                .expect("acquired primary events have an existing standard role");
58            values.insert(event.row.name.clone(), event.row.value.clone());
59            histories[index].push(event);
60        }
61        let fields = std::array::from_fn(|index| {
62            let name = PRIMARY[index];
63            (
64                name,
65                reduce(
66                    self.source,
67                    std::mem::take(&mut histories[index]),
68                    &footprint,
69                    Some(version),
70                    name,
71                    self.limits,
72                ),
73            )
74        });
75        Ok(PcbFootprintPrimaryBatch {
76            version,
77            values,
78            fields,
79        })
80    }
81}