summaryrefslogtreecommitdiffstats
path: root/rust/vendor/lzma-rs/src/error.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 17:39:49 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 17:39:49 +0000
commita0aa2307322cd47bbf416810ac0292925e03be87 (patch)
tree37076262a026c4b48c8a0e84f44ff9187556ca35 /rust/vendor/lzma-rs/src/error.rs
parentInitial commit. (diff)
downloadsuricata-a0aa2307322cd47bbf416810ac0292925e03be87.tar.xz
suricata-a0aa2307322cd47bbf416810ac0292925e03be87.zip
Adding upstream version 1:7.0.3.upstream/1%7.0.3
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'rust/vendor/lzma-rs/src/error.rs')
-rw-r--r--rust/vendor/lzma-rs/src/error.rs72
1 files changed, 72 insertions, 0 deletions
diff --git a/rust/vendor/lzma-rs/src/error.rs b/rust/vendor/lzma-rs/src/error.rs
new file mode 100644
index 0000000..be5bfcd
--- /dev/null
+++ b/rust/vendor/lzma-rs/src/error.rs
@@ -0,0 +1,72 @@
+//! Error handling.
+
+use std::fmt::Display;
+use std::io;
+use std::result;
+
+/// Library errors.
+#[derive(Debug)]
+pub enum Error {
+ /// I/O error.
+ IoError(io::Error),
+ /// Not enough bytes to complete header
+ HeaderTooShort(io::Error),
+ /// LZMA error.
+ LzmaError(String),
+ /// XZ error.
+ XzError(String),
+}
+
+/// Library result alias.
+pub type Result<T> = result::Result<T, Error>;
+
+impl From<io::Error> for Error {
+ fn from(e: io::Error) -> Error {
+ Error::IoError(e)
+ }
+}
+
+impl Display for Error {
+ fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ Error::IoError(e) => write!(fmt, "io error: {}", e),
+ Error::HeaderTooShort(e) => write!(fmt, "header too short: {}", e),
+ Error::LzmaError(e) => write!(fmt, "lzma error: {}", e),
+ Error::XzError(e) => write!(fmt, "xz error: {}", e),
+ }
+ }
+}
+
+impl std::error::Error for Error {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ match self {
+ Error::IoError(e) | Error::HeaderTooShort(e) => Some(e),
+ Error::LzmaError(_) | Error::XzError(_) => None,
+ }
+ }
+}
+
+#[cfg(test)]
+mod test {
+ use super::Error;
+
+ #[test]
+ fn test_display() {
+ assert_eq!(
+ Error::IoError(std::io::Error::new(
+ std::io::ErrorKind::Other,
+ "this is an error"
+ ))
+ .to_string(),
+ "io error: this is an error"
+ );
+ assert_eq!(
+ Error::LzmaError("this is an error".to_string()).to_string(),
+ "lzma error: this is an error"
+ );
+ assert_eq!(
+ Error::XzError("this is an error".to_string()).to_string(),
+ "xz error: this is an error"
+ );
+ }
+}