use std::fmt::Debug;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::path::Path;
use std::path::PathBuf;
#[cfg(doc)]
use super::Inspector;
cfg_breakpad! {
#[derive(Clone, PartialEq)]
pub struct Breakpad {
pub path: PathBuf,
#[doc(hidden)]
pub _non_exhaustive: (),
}
impl Breakpad {
pub fn new(path: impl Into<PathBuf>) -> Self {
Self {
path: path.into(),
_non_exhaustive: (),
}
}
}
impl From<Breakpad> for Source {
fn from(breakpad: Breakpad) -> Self {
Source::Breakpad(breakpad)
}
}
impl Debug for Breakpad {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
let Self {
path,
_non_exhaustive: (),
} = self;
f.debug_tuple(stringify!(Breakpad)).field(path).finish()
}
}
}
#[derive(Clone, PartialEq)]
pub struct Elf {
pub path: PathBuf,
pub debug_syms: bool,
#[doc(hidden)]
pub _non_exhaustive: (),
}
impl Elf {
pub fn new(path: impl Into<PathBuf>) -> Self {
Self {
path: path.into(),
debug_syms: true,
_non_exhaustive: (),
}
}
}
impl From<Elf> for Source {
fn from(elf: Elf) -> Self {
Source::Elf(elf)
}
}
impl Debug for Elf {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
let Self {
path,
debug_syms: _,
_non_exhaustive: (),
} = self;
f.debug_tuple(stringify!(Elf)).field(path).finish()
}
}
#[derive(Clone, PartialEq)]
#[non_exhaustive]
pub enum Source {
#[cfg(feature = "breakpad")]
#[cfg_attr(docsrs, doc(cfg(feature = "breakpad")))]
Breakpad(Breakpad),
Elf(Elf),
}
impl Source {
pub fn path(&self) -> Option<&Path> {
match self {
#[cfg(feature = "breakpad")]
Self::Breakpad(breakpad) => Some(&breakpad.path),
Self::Elf(elf) => Some(&elf.path),
}
}
}
impl Debug for Source {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
#[cfg(feature = "breakpad")]
Self::Breakpad(breakpad) => Debug::fmt(breakpad, f),
Self::Elf(elf) => Debug::fmt(elf, f),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn debug_repr() {
let breakpad = Breakpad::new("/a-path/with/components.sym");
assert_eq!(
format!("{breakpad:?}"),
"Breakpad(\"/a-path/with/components.sym\")"
);
let src = Source::from(breakpad);
assert_eq!(
format!("{src:?}"),
"Breakpad(\"/a-path/with/components.sym\")"
);
let elf = Elf::new("/a-path/with/components.elf");
assert_eq!(format!("{elf:?}"), "Elf(\"/a-path/with/components.elf\")");
let src = Source::from(elf);
assert_eq!(format!("{src:?}"), "Elf(\"/a-path/with/components.elf\")");
}
}