Skip to main content

pdb_bindgen_codegen/config/
image.rs

1use super::ConfigError;
2use crate::PdbIdentity;
3use std::path::{Path, PathBuf};
4
5/// A logical image and its authoritative PDB configuration.
6///
7/// The runtime base provider belongs to the source-side
8/// `#[pdb(image = "...", base = path)]` declaration. Build configuration deliberately
9/// does not duplicate that provider.
10#[derive(Clone, Debug, Eq, PartialEq)]
11pub struct Image {
12    key: String,
13    identity: PdbIdentity,
14    local_pdb: Option<PathBuf>,
15}
16
17impl Image {
18    /// Creates an image configuration.
19    ///
20    /// `key` is matched exactly against source-visible `#[pdb(image = "...")]`
21    /// values during generation. No normalization is performed.
22    ///
23    /// # Errors
24    ///
25    /// Returns [`ConfigError::EmptyImageKey`] for an empty key.
26    pub fn new(key: impl Into<String>, identity: PdbIdentity) -> Result<Self, ConfigError> {
27        let key = key.into();
28        if key.is_empty() {
29            return Err(ConfigError::EmptyImageKey);
30        }
31        Ok(Self {
32            key,
33            identity,
34            local_pdb: None,
35        })
36    }
37
38    /// Supplies a physical local PDB path for this image.
39    ///
40    /// The path is retained without filesystem access. Generation may use it before
41    /// cache or server lookup.
42    ///
43    /// # Errors
44    ///
45    /// Returns [`ConfigError::EmptyLocalPdbPath`] when `path` is empty.
46    pub fn local_pdb(mut self, path: impl AsRef<Path>) -> Result<Self, ConfigError> {
47        let path = path.as_ref();
48        if path.as_os_str().is_empty() {
49            return Err(ConfigError::EmptyLocalPdbPath);
50        }
51        self.local_pdb = Some(path.to_path_buf());
52        Ok(self)
53    }
54
55    /// Returns the exact source-visible image key.
56    #[must_use]
57    pub fn key(&self) -> &str {
58        &self.key
59    }
60
61    /// Returns the authoritative PDB identity.
62    #[must_use]
63    pub const fn identity(&self) -> &PdbIdentity {
64        &self.identity
65    }
66
67    /// Returns the directly supplied local PDB path, if any.
68    #[must_use]
69    pub fn local_pdb_path(&self) -> Option<&Path> {
70        self.local_pdb.as_deref()
71    }
72
73    pub(super) fn resolve_local_pdb(&mut self, package: &Path) {
74        if let Some(path) = self.local_pdb.as_mut().filter(|path| path.is_relative()) {
75            *path = package.join(&*path);
76        }
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    const GUID_TEXT: &str = "12345678-9abc-def0-1234-56789abcdef0";
85
86    fn identity(filename: &str) -> PdbIdentity {
87        PdbIdentity::new(filename, GUID_TEXT.parse().expect("valid GUID"), 0)
88            .expect("valid identity")
89    }
90
91    fn image(key: &str) -> Image {
92        Image::new(key, identity("kernel.pdb")).expect("valid image")
93    }
94
95    #[test]
96    fn image_validates_key_and_local_path() {
97        let configured = Image::new("ntoskrnl", identity("ntkrnlmp.pdb"))
98            .expect("valid image")
99            .local_pdb("symbols/ntkrnlmp.pdb")
100            .expect("nonempty local path");
101
102        assert_eq!(configured.key(), "ntoskrnl");
103        assert_eq!(configured.identity().filename(), "ntkrnlmp.pdb");
104        assert_eq!(
105            configured.local_pdb_path(),
106            Some(Path::new("symbols/ntkrnlmp.pdb"))
107        );
108        assert_eq!(
109            Image::new("", identity("x")),
110            Err(ConfigError::EmptyImageKey)
111        );
112        assert_eq!(
113            image("key").local_pdb(""),
114            Err(ConfigError::EmptyLocalPdbPath)
115        );
116    }
117}