lief 1.0.0

Official Rust bindings for LIEF
use lief_ffi as ffi;

use crate::common::{FromFFI, into_optional};
use crate::generic;
use std::marker::PhantomData;
use std::path::Path;

use super::PublicSymbol;
use super::compilation_unit::CompilationUnits;
use super::public_symbol::PublicSymbols;
use super::types::{Type, Types};

/// Main interface over a PDB.
///
/// One can instantiate this structure with [`crate::pdb::load`],
/// [`DebugInfo::from`] or using [`crate::generic::Binary::debug_info`].
pub struct DebugInfo<'a> {
    ptr: cxx::UniquePtr<ffi::PDB_DebugInfo>,
    _owner: PhantomData<&'a ()>,
}

impl FromFFI<ffi::PDB_DebugInfo> for DebugInfo<'_> {
    fn from_ffi(info: cxx::UniquePtr<ffi::PDB_DebugInfo>) -> Self {
        Self {
            ptr: info,
            _owner: PhantomData,
        }
    }
}

impl DebugInfo<'_> {
    /// Create a DebugInfo from a PDB file path
    pub fn from<P: AsRef<Path>>(path: P) -> Option<DebugInfo<'static>> {
        cxx::let_cxx_string!(__cxx_s = path.as_ref().to_str().unwrap());
        into_optional(ffi::PDB_DebugInfo::from_file(&__cxx_s))
    }

    /// The number of times the PDB file has been written.
    pub fn age(&self) -> u32 {
        self.ptr.age()
    }

    /// Unique identifier of the PDB file
    pub fn guid(&self) -> String {
        self.ptr.guid().to_string()
    }

    /// Iterator over the CompilationUnit from the PDB's DBI stream.
    /// [`crate::pdb::CompilationUnit`] are also named "Module" in the PDB's official documentation
    pub fn compilation_units(&self) -> CompilationUnits<'_> {
        CompilationUnits::new(self.ptr.compilation_units())
    }

    /// Return an iterator over the public symbol stream ([`PublicSymbol`])
    pub fn public_symbols(&self) -> PublicSymbols<'_> {
        PublicSymbols::new(self.ptr.public_symbols())
    }

    /// Try to find the [`PublicSymbol`] from the given name (based on the public symbol stream)
    ///
    ///
    /// ```
    /// if let Some(symbol) = info.public_symbol_by_name("MiSyncSystemPdes") {
    ///   // FOUND!
    /// }
    /// ```
    pub fn public_symbol_by_name(&self, name: &str) -> Option<PublicSymbol<'_>> {
        cxx::let_cxx_string!(__cxx_s = name);
        into_optional(self.ptr.public_symbol_by_name(&__cxx_s))
    }

    /// Return an iterator over the different [`crate::pdb::Type`] registered for this PDB file
    pub fn types(&self) -> Types<'_> {
        Types::new(self.ptr.types())
    }

    /// Try to find the type with the given name
    pub fn type_by_name(&self, name: &str) -> Option<Type<'_>> {
        cxx::let_cxx_string!(__cxx_s = name);
        into_optional(self.ptr.find_type(&__cxx_s))
    }

    /// Try to find the type at the given index
    pub fn type_by_index(&self, index: u32) -> Option<Type<'_>> {
        into_optional(self.ptr.find_type_by_index(index))
    }
}

impl generic::DebugInfo for DebugInfo<'_> {
    fn as_generic(&self) -> &ffi::AbstracDebugInfo {
        self.ptr.as_ref().unwrap().as_ref()
    }
}

impl std::fmt::Display for DebugInfo<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.ptr.to_string())
    }
}