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

kicad_monkey_core/pcb/
view_resources.rs

1//! Owner-scoped resource access on the source-backed PCB view.
2use super::*;
3
4impl PcbView<'_> {
5    /// Iterate board-owned embedded resources in source order.
6    ///
7    /// This preserves the pre-existing board-scope behavior. Use
8    /// [`Self::footprint_embedded_files`] or [`Self::all_embedded_files`] for
9    /// nested declarations.
10    pub fn embedded_files(&self) -> impl Iterator<Item = Result<PcbEmbeddedFile, Error>> + '_ {
11        self.embedded_files
12            .iter()
13            .filter(move |indexed| {
14                self.selection.contains(PcbFamily::EmbeddedFiles)
15                    && indexed.owner == PcbEmbeddedFileOwner::Board
16            })
17            .map(|indexed| embedded_file_from_span(self.source, indexed, self.limits))
18    }
19
20    /// Iterate footprint-owned embedded resources in board and child source order.
21    pub fn footprint_embedded_files(
22        &self,
23    ) -> impl Iterator<Item = Result<PcbEmbeddedFile, Error>> + '_ {
24        self.embedded_files
25            .iter()
26            .filter(move |indexed| {
27                self.selection.contains(PcbFamily::FootprintEmbeddedFiles)
28                    && matches!(
29                        indexed.owner,
30                        PcbEmbeddedFileOwner::EmbeddedFootprint { .. }
31                    )
32            })
33            .map(|indexed| embedded_file_from_span(self.source, indexed, self.limits))
34    }
35
36    /// Iterate all board and embedded-footprint resource declarations.
37    pub fn all_embedded_files(&self) -> impl Iterator<Item = Result<PcbEmbeddedFile, Error>> + '_ {
38        self.embedded_files
39            .iter()
40            .filter(move |indexed| match indexed.owner {
41                PcbEmbeddedFileOwner::Board => self.selection.contains(PcbFamily::EmbeddedFiles),
42                PcbEmbeddedFileOwner::EmbeddedFootprint { .. } => {
43                    self.selection.contains(PcbFamily::FootprintEmbeddedFiles)
44                }
45                PcbEmbeddedFileOwner::StandaloneFootprint | PcbEmbeddedFileOwner::Schematic => {
46                    false
47                }
48            })
49            .map(|indexed| embedded_file_from_span(self.source, indexed, self.limits))
50    }
51
52    /// Return the exact joined base64 payload without decoding it.
53    ///
54    /// `None` means no `(data ...)` form was authored; `Some("")` is an
55    /// explicitly empty payload.
56    pub fn embedded_file_encoded_data(
57        &self,
58        file: &PcbEmbeddedFile,
59        maximum: usize,
60    ) -> Result<Option<String>, Error> {
61        if !self
62            .embedded_files
63            .iter()
64            .any(|indexed| indexed.span.range == file.source_range && indexed.owner == file.owner)
65        {
66            return Err(Error::build(
67                ErrorKind::InvalidSpan,
68                format!(
69                    "Embedded resource {:?} does not belong to this PCB view",
70                    file.name
71                ),
72            ));
73        }
74        encoded_data(self.source, file, maximum)
75    }
76
77    /// Decode and checksum-verify one zstd/base64 payload on demand.
78    #[cfg(feature = "embedded-resource-zstd")]
79    pub fn decode_embedded_file(&self, file: &PcbEmbeddedFile) -> Result<Option<Vec<u8>>, Error> {
80        if !self
81            .embedded_files
82            .iter()
83            .any(|indexed| indexed.span.range == file.source_range && indexed.owner == file.owner)
84        {
85            return Err(Error::build(
86                ErrorKind::InvalidSpan,
87                format!(
88                    "Embedded resource {:?} does not belong to this PCB view",
89                    file.name
90                ),
91            ));
92        }
93        decoded_data(self.source, file)
94    }
95}