Skip to content

Commit 1bb343f

Browse files
committed
simplify logic for error messages
1 parent 5bf612d commit 1bb343f

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

zlib-rs/src/lib.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,22 +187,23 @@ impl From<i32> for ReturnCode {
187187
}
188188

189189
impl ReturnCode {
190-
const TABLE: [&'static str; 10] = [
191-
"need dictionary\0", /* Z_NEED_DICT 2 */
192-
"stream end\0", /* Z_STREAM_END 1 */
193-
"\0", /* Z_OK 0 */
194-
"file error\0", /* Z_ERRNO (-1) */
195-
"stream error\0", /* Z_STREAM_ERROR (-2) */
196-
"data error\0", /* Z_DATA_ERROR (-3) */
197-
"insufficient memory\0", /* Z_MEM_ERROR (-4) */
198-
"buffer error\0", /* Z_BUF_ERROR (-5) */
199-
"incompatible version\0", /* Z_VERSION_ERROR (-6) */
200-
"\0",
201-
];
190+
const fn error_message_str(self) -> &'static str {
191+
match self {
192+
ReturnCode::Ok => "\0",
193+
ReturnCode::StreamEnd => "stream end\0",
194+
ReturnCode::NeedDict => "need dictionary\0",
195+
ReturnCode::ErrNo => "file error\0",
196+
ReturnCode::StreamError => "stream error\0",
197+
ReturnCode::DataError => "data error\0",
198+
ReturnCode::MemError => "insufficient memory\0",
199+
ReturnCode::BufError => "buffer error\0",
200+
ReturnCode::VersionError => "incompatible version\0",
201+
}
202+
}
202203

203204
pub const fn error_message(self) -> *const core::ffi::c_char {
204-
let index = (ReturnCode::NeedDict as i32 - self as i32) as usize;
205-
Self::TABLE[index].as_ptr().cast()
205+
let msg = self.error_message_str();
206+
msg.as_ptr().cast::<core::ffi::c_char>()
206207
}
207208

208209
pub const fn try_from_c_int(err: core::ffi::c_int) -> Option<Self> {

0 commit comments

Comments
 (0)