• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

royaltm / rust-delharc / 29506494737

16 Jul 2026 02:24PM UTC coverage: 94.52% (-0.5%) from 95.043%
29506494737

push

github

royaltm
replaced static string errors with new LhaHeaderError and DecompressionError objects:
* improve error messages
* improve conversion of LhaError to io::Error to include the original enum capsule

64 of 111 new or added lines in 6 files covered. (57.66%)

3122 of 3303 relevant lines covered (94.52%)

25079920.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

38.89
/src/error.rs
1
use core::{fmt, error};
2
#[cfg(feature = "std")]
3
use std::{io, collections::TryReserveError};
4
#[cfg(not(feature = "std"))]
5
use alloc::collections::TryReserveError;
6
use crate::stub_io::Read;
7

8
pub type LhaResult<T, R> = Result<T, LhaError<<R as Read>::Error>>;
9

10
/// `delharc` error object.
11
///
12
/// With `std` feature enabled `E` is [`std::io::Error`] and
13
/// `LhaError` can be converted to [`std::io::Error`] using [`From`] or [`Into`].
14
#[derive(Debug, PartialEq, Eq)]
15
#[non_exhaustive]
16
pub enum LhaError<E> {
17
    /// I/O error.
18
    Io(E),
19
    /// When parsing LHA header.
20
    HeaderParse(LhaHeaderError),
21
    /// When decompressing a file.
22
    Decompress(DecompressionError),
23
    /// File checksum mismatch.
24
    Checksum,
25
}
26

27
/// An enum of [`LhaHeader`] errors.
28
#[derive(Debug, PartialEq, Eq)]
29
#[non_exhaustive]
30
pub enum LhaHeaderError {
31
    /// Unknown header level
32
    UnknownLevel,
33
    /// Level 3 signature mismatch
34
    Level3Signature,
35
    /// Not enough bytes in the extended header
36
    ExtendedHeaderSize,
37
    /// Wrapping checksum mismatch
38
    WrappingSumMismatch,
39
    /// CRC-16 checksum mismatch
40
    Crc16Mismatch,
41
    /// Size validation failed
42
    SizeMismatch,
43
    /// Long size validation failed
44
    LongSizeMismatch,
45
    /// Skip size validation failed
46
    SkipSizeMismatch,
47
    /// Duplicate common CRC-16 header found
48
    CommonHeader,
49
    /// Header not found
50
    HeaderNotFound,
51
    /// Memory allocation failed
52
    OutOfMemory,
53
}
54

55
/// An enum of errors returned from decompression algorithms.
56
#[derive(Debug, PartialEq, Eq)]
57
#[non_exhaustive]
58
pub enum DecompressionError {
59
    // "unsupported compression method"
60
    UnsupportedCompression,
61
    // "too many tree code lengths"
62
    CodeLengthTableOverflow,
63
    // "not enough leaf nodes in code lengths"
64
    TreeLeavesUndeflow,
65
    // "too many leaf nodes in code lengths"
66
    TreeLeavesOverflow,
67
    // "temporary code length table size overflow"
68
    TemporaryCodeTableOverflow,
69
    // "commands code length table size overflow"
70
    CommandCodeTableOverflow,
71
    // "offset code length table size overflow"
72
    OffsetCodeTableOverflow,
73
    // "command code overflow"
74
    CommandOverflow,
75
    // "offset code overflow"
76
    OffsetOverflow,
77
    // "code length overflow"
78
    CodeLengthOverflow,
79
    // "too many bits requested"
80
    BitSizeOverflow,
81
    /// Memory allocation failed
82
    OutOfMemory,
83
}
84

85
impl fmt::Display for LhaHeaderError {
86
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25✔
87
        use LhaHeaderError::*;
88
        match self {
25✔
NEW
89
            UnknownLevel => "unknown header level",
×
NEW
90
            Level3Signature => "level 3 signature mismatch",
×
NEW
91
            ExtendedHeaderSize => "not enough bytes in the extended header",
×
NEW
92
            WrappingSumMismatch => "wrapping checksum mismatch",
×
NEW
93
            Crc16Mismatch => "CRC-16 checksum mismatch",
×
NEW
94
            SizeMismatch => "size validation failed",
×
95
            LongSizeMismatch => "long size validation failed",
24✔
NEW
96
            SkipSizeMismatch => "skip size validation failed",
×
NEW
97
            CommonHeader => "duplicate CRC-16 header found",
×
98
            HeaderNotFound => "header not found",
1✔
NEW
99
            OutOfMemory => "memory allocation failed",
×
100
        }
101
        .fmt(f)
25✔
102
    }
25✔
103
}
104

105
impl error::Error for LhaHeaderError {}
106

107
impl From<TryReserveError> for LhaHeaderError {
NEW
108
    fn from(_err: TryReserveError) -> LhaHeaderError {
×
NEW
109
        LhaHeaderError::OutOfMemory
×
NEW
110
    }
×
111
}
112

113
impl fmt::Display for DecompressionError {
114
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24✔
115
        use DecompressionError::*;
116
        match self {
24✔
NEW
117
            UnsupportedCompression => "unsupported compression method",
×
NEW
118
            CodeLengthTableOverflow => "too many tree code lengths",
×
NEW
119
            TreeLeavesUndeflow => "not enough leaf nodes in code lengths",
×
NEW
120
            TreeLeavesOverflow => "too many leaf nodes in code lengths",
×
121
            TemporaryCodeTableOverflow => "temporary code length table size overflow",
24✔
NEW
122
            CommandCodeTableOverflow => "commands code length table size overflow",
×
NEW
123
            OffsetCodeTableOverflow => "offset code length table size overflow",
×
NEW
124
            CommandOverflow => "command code overflow",
×
NEW
125
            OffsetOverflow => "offset code overflow",
×
NEW
126
            CodeLengthOverflow => "code length overflow",
×
NEW
127
            BitSizeOverflow => "too many bits requested",
×
NEW
128
            OutOfMemory => "memory allocation failed",
×
129
        }
130
        .fmt(f)
24✔
131
    }
24✔
132
}
133

134
impl error::Error for DecompressionError {}
135

136
impl From<TryReserveError> for DecompressionError {
NEW
137
    fn from(_err: TryReserveError) -> DecompressionError {
×
NEW
138
        DecompressionError::OutOfMemory
×
NEW
139
    }
×
140
}
141

142
impl<E: fmt::Display> fmt::Display for LhaError<E> {
143
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49✔
144
        use LhaError::*;
145
        match self {
49✔
146
            Io(e) => e.fmt(f),
×
147
            HeaderParse(e) => write!(f, "while parsing LHA header: {}", e),
25✔
148
            Decompress(e) => write!(f, "while decompressing: {}", e),
24✔
NEW
149
            Checksum => write!(f, "CRC-16 file checksum mismatch"),
×
150
        }
151
    }
49✔
152
}
153

154
impl<E: error::Error + 'static> error::Error for LhaError<E> {
NEW
155
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
×
156
        use LhaError::*;
157
        match self {
×
158
            Io(e) => Some(e),
×
159
            _ => None
×
160
        }
161
    }
×
162
}
163

164
#[cfg(feature = "std")]
165
impl From<LhaError<io::Error>> for io::Error {
166
    fn from(err: LhaError<io::Error>) -> Self {
867✔
167
        use LhaError::*;
168
        use io::{Error, ErrorKind};
169
        match err {
867✔
170
            Io(e) => e,
27✔
171
            err => Error::new(ErrorKind::InvalidData, err),
840✔
172
        }
173
    }
867✔
174
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc