use super::ConfigError;
use crate::PdbIdentity;
use std::path::{Path, PathBuf};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Image {
key: String,
identity: PdbIdentity,
local_pdb: Option<PathBuf>,
}
impl Image {
pub fn new(key: impl Into<String>, identity: PdbIdentity) -> Result<Self, ConfigError> {
let key = key.into();
if key.is_empty() {
return Err(ConfigError::EmptyImageKey);
}
Ok(Self {
key,
identity,
local_pdb: None,
})
}
pub fn local_pdb(mut self, path: impl AsRef<Path>) -> Result<Self, ConfigError> {
let path = path.as_ref();
if path.as_os_str().is_empty() {
return Err(ConfigError::EmptyLocalPdbPath);
}
self.local_pdb = Some(path.to_path_buf());
Ok(self)
}
#[must_use]
pub fn key(&self) -> &str {
&self.key
}
#[must_use]
pub const fn identity(&self) -> &PdbIdentity {
&self.identity
}
#[must_use]
pub fn local_pdb_path(&self) -> Option<&Path> {
self.local_pdb.as_deref()
}
pub(super) fn resolve_local_pdb(&mut self, package: &Path) {
if let Some(path) = self.local_pdb.as_mut().filter(|path| path.is_relative()) {
*path = package.join(&*path);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const GUID_TEXT: &str = "12345678-9abc-def0-1234-56789abcdef0";
fn identity(filename: &str) -> PdbIdentity {
PdbIdentity::new(filename, GUID_TEXT.parse().expect("valid GUID"), 0)
.expect("valid identity")
}
fn image(key: &str) -> Image {
Image::new(key, identity("kernel.pdb")).expect("valid image")
}
#[test]
fn image_validates_key_and_local_path() {
let configured = Image::new("ntoskrnl", identity("ntkrnlmp.pdb"))
.expect("valid image")
.local_pdb("symbols/ntkrnlmp.pdb")
.expect("nonempty local path");
assert_eq!(configured.key(), "ntoskrnl");
assert_eq!(configured.identity().filename(), "ntkrnlmp.pdb");
assert_eq!(
configured.local_pdb_path(),
Some(Path::new("symbols/ntkrnlmp.pdb"))
);
assert_eq!(
Image::new("", identity("x")),
Err(ConfigError::EmptyImageKey)
);
assert_eq!(
image("key").local_pdb(""),
Err(ConfigError::EmptyLocalPdbPath)
);
}
}