use std::fmt;
use std::{io, path::PathBuf};
pub type Result<T> = std::result::Result<T, BuildError>;
pub enum BuildError {
EnvVar(std::env::VarError),
Io(io::Error),
LibraryNotFound(PathBuf),
RssPathNotFound,
StubGenerationFailed(String),
BindgenError(String),
PythonError(String),
CompilationError(String),
HeadersNotFound(PathBuf),
InvalidRssInclude(PathBuf),
}
impl fmt::Display for BuildError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BuildError::EnvVar(e) => write!(f, "Environment variable error: {}", e),
BuildError::Io(e) => write!(f, "IO error: {}", e),
BuildError::LibraryNotFound(path) => {
write!(f, "Library not found at: {}", path.display())?;
if path.is_relative() && path.as_os_str() != "." {
write!(f, " (relative paths resolve against the a121-sys directory, use an absolute path)")?;
}
Ok(())
}
BuildError::RssPathNotFound => write!(f, "RSS directory not found"),
BuildError::StubGenerationFailed(msg) => write!(f, "Stub generation failed: {}", msg),
BuildError::BindgenError(msg) => write!(f, "Bindgen error: {}", msg),
BuildError::PythonError(msg) => write!(f, "Python script error: {}", msg),
BuildError::CompilationError(msg) => write!(f, "Compilation error: {}", msg),
BuildError::HeadersNotFound(path) => {
write!(f, "Headers not found at: {}", path.display())
}
BuildError::InvalidRssInclude(path) => write!(
f,
"A121_RSS_INCLUDE must be an existing directory named `include`. Use an \
absolute path, since a relative one resolves against the a121-sys directory: {}",
path.display()
),
}
}
}
impl fmt::Debug for BuildError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl std::error::Error for BuildError {}
impl From<io::Error> for BuildError {
fn from(err: io::Error) -> Self {
BuildError::Io(err)
}
}