parquet2/
error.rs

1//! Contains [`Error`]
2
3/// List of features whose non-activation may cause a runtime error.
4/// Used to indicate which lack of feature caused [`Error::FeatureNotActive`].
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6#[non_exhaustive]
7pub enum Feature {
8    /// Snappy compression and decompression
9    Snappy,
10    /// Brotli compression and decompression
11    Brotli,
12    /// Gzip compression and decompression
13    Gzip,
14    /// Lz4 raw compression and decompression
15    Lz4,
16    /// Zstd compression and decompression
17    Zstd,
18}
19
20/// Errors generated by this crate
21#[derive(Debug, Clone)]
22#[non_exhaustive]
23pub enum Error {
24    /// When the parquet file is known to be out of spec.
25    OutOfSpec(String),
26    /// Error presented when trying to use a code branch that requires activating a feature.
27    FeatureNotActive(Feature, String),
28    /// Error presented when trying to use a feature from parquet that is not yet supported
29    FeatureNotSupported(String),
30    /// When encoding, the user passed an invalid parameter
31    InvalidParameter(String),
32    /// When decoding or decompressing, the page would allocate more memory than allowed
33    WouldOverAllocate,
34}
35
36impl Error {
37    pub(crate) fn oos<I: Into<String>>(message: I) -> Self {
38        Self::OutOfSpec(message.into())
39    }
40}
41
42impl std::error::Error for Error {}
43
44impl std::fmt::Display for Error {
45    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
46        match self {
47            Error::OutOfSpec(message) => {
48                write!(fmt, "File out of specification: {}", message)
49            }
50            Error::FeatureNotActive(feature, reason) => {
51                write!(
52                    fmt,
53                    "The feature \"{:?}\" needs to be active to {}",
54                    feature, reason
55                )
56            }
57            Error::FeatureNotSupported(reason) => {
58                write!(fmt, "Not yet supported: {}", reason)
59            }
60            Error::InvalidParameter(message) => {
61                write!(fmt, "Invalid parameter: {}", message)
62            }
63            Error::WouldOverAllocate => {
64                write!(fmt, "Operation would exceed memory use threshold")
65            }
66        }
67    }
68}
69
70#[cfg(feature = "snappy")]
71impl From<snap::Error> for Error {
72    fn from(e: snap::Error) -> Error {
73        Error::OutOfSpec(format!("underlying snap error: {}", e))
74    }
75}
76
77#[cfg(feature = "lz4_flex")]
78impl From<lz4_flex::block::DecompressError> for Error {
79    fn from(e: lz4_flex::block::DecompressError) -> Error {
80        Error::OutOfSpec(format!("underlying lz4_flex error: {}", e))
81    }
82}
83
84#[cfg(feature = "lz4_flex")]
85impl From<lz4_flex::block::CompressError> for Error {
86    fn from(e: lz4_flex::block::CompressError) -> Error {
87        Error::OutOfSpec(format!("underlying lz4_flex error: {}", e))
88    }
89}
90
91impl From<parquet_format_safe::thrift::Error> for Error {
92    fn from(e: parquet_format_safe::thrift::Error) -> Error {
93        Error::OutOfSpec(format!("Invalid thrift: {}", e))
94    }
95}
96
97impl From<std::io::Error> for Error {
98    fn from(e: std::io::Error) -> Error {
99        Error::OutOfSpec(format!("underlying IO error: {}", e))
100    }
101}
102
103impl From<std::collections::TryReserveError> for Error {
104    fn from(e: std::collections::TryReserveError) -> Error {
105        Error::OutOfSpec(format!("OOM: {}", e))
106    }
107}
108
109impl From<std::num::TryFromIntError> for Error {
110    fn from(e: std::num::TryFromIntError) -> Error {
111        Error::OutOfSpec(format!("Number must be zero or positive: {}", e))
112    }
113}
114
115impl From<std::array::TryFromSliceError> for Error {
116    fn from(e: std::array::TryFromSliceError) -> Error {
117        Error::OutOfSpec(format!("Can't deserialize to parquet native type: {}", e))
118    }
119}
120
121/// A specialized `Result` for Parquet errors.
122pub type Result<T> = std::result::Result<T, Error>;