1use std::fmt;
4use std::io;
5use std::path::PathBuf;
6
7use parquet::errors::ParquetError;
8
9#[derive(Debug)]
11pub enum Error {
12 NoInputs,
14 OutputIsInput(PathBuf),
16 Incompatible {
19 path: PathBuf,
21 first: PathBuf,
23 what: &'static str,
25 },
26 Io {
28 path: PathBuf,
30 source: io::Error,
32 },
33 Parquet {
35 path: PathBuf,
37 source: ParquetError,
39 },
40 RowGroup {
42 path: PathBuf,
44 row_group: usize,
46 row_groups: usize,
48 },
49 Pool(String),
51 Option(String),
54 Footer {
57 path: PathBuf,
59 reason: String,
61 },
62}
63
64pub type Result<T> = std::result::Result<T, Error>;
66
67impl Error {
68 pub(crate) fn io(path: impl Into<PathBuf>) -> impl FnOnce(io::Error) -> Self {
69 let path = path.into();
70 move |source| Error::Io { path, source }
71 }
72
73 pub(crate) fn parquet(path: impl Into<PathBuf>) -> impl FnOnce(ParquetError) -> Self {
74 let path = path.into();
75 move |source| Error::Parquet { path, source }
76 }
77
78 pub(crate) fn footer(path: impl Into<PathBuf>, reason: impl Into<String>) -> Self {
79 Error::Footer {
80 path: path.into(),
81 reason: reason.into(),
82 }
83 }
84}
85
86impl fmt::Display for Error {
87 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88 match self {
89 Error::NoInputs => write!(f, "no input files"),
90 Error::OutputIsInput(p) => write!(f, "the output {} is one of the inputs", p.display()),
91 Error::Incompatible { path, first, what } => {
92 write!(
93 f,
94 "{} has other {what} than {}",
95 path.display(),
96 first.display()
97 )
98 }
99 Error::Io { path, source } => write!(f, "{}: {source}", path.display()),
100 Error::Parquet { path, source } => write!(f, "{}: {source}", path.display()),
101 Error::RowGroup {
102 path,
103 row_group,
104 row_groups,
105 } => write!(
106 f,
107 "{}: no row group {row_group} (the file has {row_groups})",
108 path.display()
109 ),
110 Error::Pool(reason) => write!(f, "no thread pool for the write: {reason}"),
111 Error::Option(reason) => write!(f, "{reason}"),
112 Error::Footer { path, reason } => {
113 write!(
114 f,
115 "{}: column orders not restored: {reason}",
116 path.display()
117 )
118 }
119 }
120 }
121}
122
123impl std::error::Error for Error {
124 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
125 match self {
126 Error::Io { source, .. } => Some(source),
127 Error::Parquet { source, .. } => Some(source),
128 _ => None,
129 }
130 }
131}