Coverage Report

Created: 2025-07-23 07:29

/rust/registry/src/index.crates.io-6f17d22bba15001f/lzma-rs-0.2.0/src/error.rs
Line
Count
Source (jump to first uncovered line)
1
//! Error handling.
2
3
use std::fmt::Display;
4
use std::io;
5
use std::result;
6
7
/// Library errors.
8
#[derive(Debug)]
9
pub enum Error {
10
    /// I/O error.
11
    IoError(io::Error),
12
    /// Not enough bytes to complete header
13
    HeaderTooShort(io::Error),
14
    /// LZMA error.
15
    LzmaError(String),
16
    /// XZ error.
17
    XzError(String),
18
}
19
20
/// Library result alias.
21
pub type Result<T> = result::Result<T, Error>;
22
23
impl From<io::Error> for Error {
24
40.9k
    fn from(e: io::Error) -> Error {
25
40.9k
        Error::IoError(e)
26
40.9k
    }
27
}
28
29
impl Display for Error {
30
0
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31
0
        match self {
32
0
            Error::IoError(e) => write!(fmt, "io error: {}", e),
33
0
            Error::HeaderTooShort(e) => write!(fmt, "header too short: {}", e),
34
0
            Error::LzmaError(e) => write!(fmt, "lzma error: {}", e),
35
0
            Error::XzError(e) => write!(fmt, "xz error: {}", e),
36
        }
37
0
    }
38
}
39
40
impl std::error::Error for Error {
41
0
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
42
0
        match self {
43
0
            Error::IoError(e) | Error::HeaderTooShort(e) => Some(e),
44
0
            Error::LzmaError(_) | Error::XzError(_) => None,
45
        }
46
0
    }
47
}
48
49
#[cfg(test)]
50
mod test {
51
    use super::Error;
52
53
    #[test]
54
    fn test_display() {
55
        assert_eq!(
56
            Error::IoError(std::io::Error::new(
57
                std::io::ErrorKind::Other,
58
                "this is an error"
59
            ))
60
            .to_string(),
61
            "io error: this is an error"
62
        );
63
        assert_eq!(
64
            Error::LzmaError("this is an error".to_string()).to_string(),
65
            "lzma error: this is an error"
66
        );
67
        assert_eq!(
68
            Error::XzError("this is an error".to_string()).to_string(),
69
            "xz error: this is an error"
70
        );
71
    }
72
}