use std::fmt;
use std::io;
use std::path::PathBuf;
use parquet::errors::ParquetError;
#[derive(Debug)]
pub enum Error {
NoInputs,
OutputIsInput(PathBuf),
Incompatible {
path: PathBuf,
first: PathBuf,
what: &'static str,
},
Io {
path: PathBuf,
source: io::Error,
},
Parquet {
path: PathBuf,
source: ParquetError,
},
RowGroup {
path: PathBuf,
row_group: usize,
row_groups: usize,
},
Pool(String),
Option(String),
Footer {
path: PathBuf,
reason: String,
},
}
pub type Result<T> = std::result::Result<T, Error>;
impl Error {
pub(crate) fn io(path: impl Into<PathBuf>) -> impl FnOnce(io::Error) -> Self {
let path = path.into();
move |source| Error::Io { path, source }
}
pub(crate) fn parquet(path: impl Into<PathBuf>) -> impl FnOnce(ParquetError) -> Self {
let path = path.into();
move |source| Error::Parquet { path, source }
}
pub(crate) fn footer(path: impl Into<PathBuf>, reason: impl Into<String>) -> Self {
Error::Footer {
path: path.into(),
reason: reason.into(),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::NoInputs => write!(f, "no input files"),
Error::OutputIsInput(p) => write!(f, "the output {} is one of the inputs", p.display()),
Error::Incompatible { path, first, what } => {
write!(
f,
"{} has other {what} than {}",
path.display(),
first.display()
)
}
Error::Io { path, source } => write!(f, "{}: {source}", path.display()),
Error::Parquet { path, source } => write!(f, "{}: {source}", path.display()),
Error::RowGroup {
path,
row_group,
row_groups,
} => write!(
f,
"{}: no row group {row_group} (the file has {row_groups})",
path.display()
),
Error::Pool(reason) => write!(f, "no thread pool for the write: {reason}"),
Error::Option(reason) => write!(f, "{reason}"),
Error::Footer { path, reason } => {
write!(
f,
"{}: column orders not restored: {reason}",
path.display()
)
}
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io { source, .. } => Some(source),
Error::Parquet { source, .. } => Some(source),
_ => None,
}
}
}