pdb-bindgen-codegen 0.1.0

Build-time discovery, PDB acquisition, and binding generation for pdb-bindgen.
Documentation
use proc_macro2::Span;
use std::error::Error;
use std::fmt;
use std::io;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum DiagnosticCategory {
    PackageRoot,
    SourceRoot,
    Symlink,
    Io,
    Utf8,
    RustSyntax,
    Manifest,
    FeatureCfg,
    Cfg,
    Marker,
    ModuleGraph,
}

#[derive(Debug)]
pub(crate) struct Diagnostic {
    category: DiagnosticCategory,
    path: String,
    line: Option<usize>,
    column: Option<usize>,
    message: String,
    pub(super) source: Option<Box<dyn Error + Send + Sync>>,
}

impl Diagnostic {
    pub(super) fn new(
        category: DiagnosticCategory,
        path: impl Into<String>,
        message: impl Into<String>,
    ) -> Self {
        Self {
            category,
            path: path.into(),
            line: None,
            column: None,
            message: message.into(),
            source: None,
        }
    }

    pub(super) fn at(
        category: DiagnosticCategory,
        path: &str,
        span: Span,
        message: impl Into<String>,
    ) -> Self {
        let start = span.start();
        Self {
            category,
            path: path.to_owned(),
            line: Some(start.line),
            column: Some(start.column + 1),
            message: message.into(),
            source: None,
        }
    }

    pub(super) fn at_location(
        category: DiagnosticCategory,
        path: &str,
        location: super::model::Location,
        message: impl Into<String>,
    ) -> Self {
        Self {
            category,
            path: path.to_owned(),
            line: Some(location.line),
            column: Some(location.column),
            message: message.into(),
            source: None,
        }
    }

    pub(super) fn io(
        category: DiagnosticCategory,
        path: impl Into<String>,
        message: impl Into<String>,
        error: io::Error,
    ) -> Self {
        Self {
            category,
            path: path.into(),
            line: None,
            column: None,
            message: message.into(),
            source: Some(Box::new(error)),
        }
    }

    pub(crate) const fn category(&self) -> DiagnosticCategory {
        self.category
    }
    pub(crate) fn path(&self) -> &str {
        &self.path
    }
    pub(crate) const fn line(&self) -> Option<usize> {
        self.line
    }
    pub(crate) const fn column(&self) -> Option<usize> {
        self.column
    }
}

impl fmt::Display for Diagnostic {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "{}", self.path)?;
        if let Some(line) = self.line {
            write!(formatter, ":{line}:{}", self.column.unwrap_or(1))?;
        }
        write!(formatter, ": {:?}: {}", self.category, self.message)
    }
}

impl Error for Diagnostic {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        self.source
            .as_deref()
            .map(|error| error as &(dyn Error + 'static))
    }
}