pdb-bindgen-codegen 0.1.0

Build-time discovery, PDB acquisition, and binding generation for pdb-bindgen.
Documentation
use super::{ConfigError, SymbolServerErrorKind};
use std::path::{Path, PathBuf};

const MICROSOFT_SYMBOL_SERVER: &str = "https://msdl.microsoft.com/download/symbols";
const DEFAULT_CACHE_RELATIVE_PATH: &str = "pdb-bindgen/cache";

/// Describes where PDB acquisition keeps cached files.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CachePolicy {
    location: CacheLocation,
}

impl CachePolicy {
    pub(super) const fn out_dir() -> Self {
        Self {
            location: CacheLocation::OutDir,
        }
    }

    pub(super) fn set_directory(&mut self, path: PathBuf) {
        self.location = CacheLocation::Directory(path);
    }

    pub(super) fn reset_out_dir(&mut self) {
        self.location = CacheLocation::OutDir;
    }

    /// Returns whether the cache is unresolved relative to `OUT_DIR`.
    #[must_use]
    pub const fn is_out_dir_relative(&self) -> bool {
        matches!(&self.location, CacheLocation::OutDir)
    }

    /// Returns the relative suffix used by the default policy.
    #[must_use]
    pub fn out_dir_relative_path(&self) -> Option<&Path> {
        match &self.location {
            CacheLocation::OutDir => Some(Path::new(DEFAULT_CACHE_RELATIVE_PATH)),
            CacheLocation::Directory(_) => None,
        }
    }

    /// Returns the explicit cache directory, if configured.
    #[must_use]
    pub fn directory(&self) -> Option<&Path> {
        match &self.location {
            CacheLocation::OutDir => None,
            CacheLocation::Directory(path) => Some(path),
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
enum CacheLocation {
    OutDir,
    Directory(PathBuf),
}

/// Describes the configured Symbol Server source.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SymbolServer {
    source: SymbolServerSource,
}

impl SymbolServer {
    pub(super) const fn microsoft() -> Self {
        Self {
            source: SymbolServerSource::Microsoft,
        }
    }

    pub(super) fn set_custom(&mut self, url: String) {
        self.source = SymbolServerSource::Custom(url);
    }

    pub(super) fn reset_microsoft(&mut self) {
        self.source = SymbolServerSource::Microsoft;
    }

    pub(super) fn disable(&mut self) {
        self.source = SymbolServerSource::Disabled;
    }

    /// Returns the configured server URL, or `None` when disabled.
    #[must_use]
    pub fn url(&self) -> Option<&str> {
        match &self.source {
            SymbolServerSource::Microsoft => Some(MICROSOFT_SYMBOL_SERVER),
            SymbolServerSource::Custom(url) => Some(url),
            SymbolServerSource::Disabled => None,
        }
    }

    /// Returns whether Microsoft's public Symbol Server is configured.
    #[must_use]
    pub const fn is_microsoft(&self) -> bool {
        matches!(&self.source, SymbolServerSource::Microsoft)
    }

    /// Returns whether Symbol Server acquisition is disabled.
    #[must_use]
    pub const fn is_disabled(&self) -> bool {
        matches!(&self.source, SymbolServerSource::Disabled)
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
enum SymbolServerSource {
    Microsoft,
    Custom(String),
    Disabled,
}

pub(super) fn validate_symbol_server(url: &str) -> Result<(), ConfigError> {
    let remainder = if let Some(remainder) = url.strip_prefix("https://") {
        remainder
    } else if let Some(remainder) = url.strip_prefix("http://") {
        remainder
    } else {
        return Err(ConfigError::InvalidSymbolServer {
            url: url.to_owned(),
            reason: SymbolServerErrorKind::Scheme,
        });
    };

    if remainder.is_empty() || remainder.starts_with('/') {
        return Err(ConfigError::InvalidSymbolServer {
            url: url.to_owned(),
            reason: SymbolServerErrorKind::MissingAuthority,
        });
    }
    if url
        .chars()
        .any(|character| character.is_ascii_whitespace() || character.is_control())
        || url.contains(['?', '#'])
    {
        return Err(ConfigError::InvalidSymbolServer {
            url: url.to_owned(),
            reason: SymbolServerErrorKind::InvalidCharacter,
        });
    }
    Ok(())
}