pdb-bindgen-codegen 0.1.0

Build-time discovery, PDB acquisition, and binding generation for pdb-bindgen.
Documentation
use crate::output::OutputError;
use std::error::Error as StdError;
use std::fmt;

#[derive(Debug)]
pub(crate) enum Error {
    Context {
        image: String,
        source: Box<Self>,
    },
    MissingDatabase {
        image: String,
    },
    Record {
        image: String,
        pdb_name: String,
        source: crate::pdb::error::Error,
    },
    SymbolLookup {
        image: String,
        kind: &'static str,
        name: String,
        source: Box<crate::pdb::error::Error>,
    },
    SymbolKindMismatch {
        image: String,
        name: String,
        expected: &'static str,
        actual: &'static str,
    },
    KindMismatch {
        pdb_name: String,
        expected: &'static str,
        actual: &'static str,
    },
    Alignment {
        pdb_name: String,
        alignment: Option<u64>,
    },
    Layout {
        pdb_name: String,
        source: crate::layout::Error,
    },
    MissingField {
        pdb_name: String,
        rust_name: String,
    },
    DuplicateField {
        pdb_name: String,
    },
    NameCollision {
        name: String,
    },
    Unsupported {
        pdb_name: String,
        rust_name: String,
        detail: String,
    },
    InvalidFragment {
        rust_name: String,
        source: syn::Error,
    },
    Output(OutputError),
}

impl Error {
    pub(crate) fn context(image: String, source: Self) -> Self {
        if source.image().is_some() {
            source
        } else {
            Self::Context {
                image,
                source: Box::new(source),
            }
        }
    }

    pub(crate) fn image(&self) -> Option<&str> {
        match self {
            Self::Context { image, .. }
            | Self::MissingDatabase { image }
            | Self::Record { image, .. }
            | Self::SymbolLookup { image, .. }
            | Self::SymbolKindMismatch { image, .. } => Some(image),
            Self::KindMismatch { .. }
            | Self::Alignment { .. }
            | Self::Layout { .. }
            | Self::MissingField { .. }
            | Self::DuplicateField { .. }
            | Self::NameCollision { .. }
            | Self::Unsupported { .. }
            | Self::InvalidFragment { .. }
            | Self::Output(_) => None,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Context { image, source } => write!(f, "for image `{image}`: {source}"),
            Self::MissingDatabase { image } => {
                write!(f, "missing PDB database for image `{image}`")
            }
            Self::Record {
                image, pdb_name, ..
            } => write!(
                f,
                "cannot resolve PDB record `{pdb_name}` for image `{image}`"
            ),
            Self::SymbolLookup {
                image, kind, name, ..
            } => write!(f, "cannot resolve PDB {kind} `{name}` for image `{image}`"),
            Self::SymbolKindMismatch {
                image,
                name,
                expected,
                actual,
            } => write!(
                f,
                "PDB symbol `{name}` for image `{image}` is a {actual}, expected {expected}"
            ),
            Self::KindMismatch {
                pdb_name,
                expected,
                actual,
            } => write!(
                f,
                "PDB record `{pdb_name}` is {actual}, expected {expected}"
            ),
            Self::Alignment {
                pdb_name,
                alignment,
            } => write!(
                f,
                "PDB record `{pdb_name}` has unsupported alignment {alignment:?}"
            ),
            Self::Layout { pdb_name, .. } => write!(f, "cannot lay out PDB record `{pdb_name}`"),
            Self::MissingField {
                pdb_name,
                rust_name,
            } => write!(
                f,
                "selected Rust field `{rust_name}` has no unique PDB field `{pdb_name}`"
            ),
            Self::DuplicateField { pdb_name } => {
                write!(f, "PDB field `{pdb_name}` is selected more than once")
            }
            Self::NameCollision { name } => write!(f, "generated name collides with `{name}`"),
            Self::Unsupported {
                pdb_name,
                rust_name,
                detail,
            } => write!(f, "cannot render `{pdb_name}` as `{rust_name}`: {detail}"),
            Self::InvalidFragment { rust_name, .. } => {
                write!(f, "renderer emitted invalid Rust for `{rust_name}`")
            }
            Self::Output(error) => error.fmt(f),
        }
    }
}

impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self {
            Self::Context { source, .. } => Some(source),
            Self::Record { source, .. } => Some(source),
            Self::SymbolLookup { source, .. } => Some(source.as_ref()),
            Self::Layout { source, .. } => Some(source),
            Self::InvalidFragment { source, .. } => Some(source),
            Self::Output(source) => Some(source),
            _ => None,
        }
    }
}