replace err_msg with failure crate's

png-branch
an 2019-02-05 17:51:49 -05:00
parent 23871101b4
commit 256c05c806
4 changed files with 20 additions and 23 deletions

View File

@ -35,19 +35,19 @@ impl BinUtil for [u8]
{ {
fn c_iden(&self, i: usize) -> ResultS<Ident> fn c_iden(&self, i: usize) -> ResultS<Ident>
{ {
if i + 3 >= self.len() {return err_msg("not enough data")} if i + 3 >= self.len() {return Err(err_msg("not enough data"));}
Ok([self[i], self[i+1], self[i+2], self[i+3]]) Ok([self[i], self[i+1], self[i+2], self[i+3]])
} }
fn c_u32b(&self, i: usize) -> ResultS<u32> fn c_u32b(&self, i: usize) -> ResultS<u32>
{ {
if i + 3 >= self.len() {return err_msg("not enough data")} if i + 3 >= self.len() {return Err(err_msg("not enough data"));}
Ok(u32::from_be_bytes([self[i], self[i+1], self[i+2], self[i+3]])) Ok(u32::from_be_bytes([self[i], self[i+1], self[i+2], self[i+3]]))
} }
fn c_u16b(&self, i: usize) -> ResultS<u16> fn c_u16b(&self, i: usize) -> ResultS<u16>
{ {
if i + 1 >= self.len() {return err_msg("not enough data")} if i + 1 >= self.len() {return Err(err_msg("not enough data"));}
Ok(u16::from_be_bytes([self[i], self[i+1]])) Ok(u16::from_be_bytes([self[i], self[i+1]]))
} }
} }

View File

@ -1,6 +1,6 @@
//! Error handling. //! Error handling.
pub use failure::{Error, Fail, format_err}; pub use failure::{Error, Fail, err_msg, format_err};
use crate::durandal::traits::PrimInt; use crate::durandal::traits::PrimInt;
use std::fmt; use std::fmt;
@ -24,7 +24,4 @@ impl<T> fmt::Debug for ReprError<T> where T: PrimInt
pub type ResultS<T> = Result<T, Error>; pub type ResultS<T> = Result<T, Error>;
pub fn err_msg<T>(s: &'static str) -> ResultS<T> {Err(failure::err_msg(s))}
pub fn err_msg_v (s: &'static str) -> Error { failure::err_msg(s) }
// EOF // EOF

View File

@ -34,10 +34,10 @@ pub fn read_bitmap_area(mut im: Image, b: &[u8], packed: bool, clip: bool) -> Re
// clut_id = b.c_u32b(p+38)?; // clut_id = b.c_u32b(p+38)?;
if pitch_fl & 0x8000 == 0 { if pitch_fl & 0x8000 == 0 {
return err_msg("PICT1 not supported"); return Err(err_msg("PICT1 not supported"));
} }
if right - left != w || bottom - top != h { if right - left != w || bottom - top != h {
return err_msg("image bounds are incorrect"); return Err(err_msg("image bounds are incorrect"));
} }
p += 46; // size of header p += 46; // size of header
@ -63,7 +63,7 @@ pub fn read_bitmap_area(mut im: Image, b: &[u8], packed: bool, clip: bool) -> Re
match depth { match depth {
1 | 2 | 4 | 8 => { 1 | 2 | 4 | 8 => {
let clut = clut.ok_or_else(|| err_msg_v("no clut in indexed mode"))?; let clut = clut.ok_or_else(|| err_msg("no clut in indexed mode"))?;
if pitch < 8 && depth == 8 { if pitch < 8 && depth == 8 {
// uncompressed 8-bit colormap indices // uncompressed 8-bit colormap indices
for _ in 0..h { for _ in 0..h {
@ -86,7 +86,7 @@ pub fn read_bitmap_area(mut im: Image, b: &[u8], packed: bool, clip: bool) -> Re
Ok(im) Ok(im)
} }
else {err_msg("invalid configuration")} else {Err(err_msg("invalid configuration"))}
}, },
16 => 16 =>
if pitch < 8 || pack_typ == PACK_NONE { if pitch < 8 || pack_typ == PACK_NONE {
@ -110,7 +110,7 @@ pub fn read_bitmap_area(mut im: Image, b: &[u8], packed: bool, clip: bool) -> Re
Ok(im) Ok(im)
} }
else {err_msg("invalid configuration")}, else {Err(err_msg("invalid configuration"))},
32 => 32 =>
if pitch < 8 || pack_typ == PACK_NONE || pack_typ == PACK_NOPAD { if pitch < 8 || pack_typ == PACK_NONE || pack_typ == PACK_NOPAD {
// uncompressed RGB8 or XRGB8 // uncompressed RGB8 or XRGB8
@ -140,8 +140,8 @@ pub fn read_bitmap_area(mut im: Image, b: &[u8], packed: bool, clip: bool) -> Re
Ok(im) Ok(im)
} }
else {err_msg("invalid configuration")}, else {Err(err_msg("invalid configuration"))},
_ => err_msg("invalid bit depth") _ => Err(err_msg("invalid bit depth"))
} }
} }
@ -222,11 +222,11 @@ pub fn load_pict(b: &[u8]) -> ResultS<Image>
0x00a1 => p += (b.c_u16b(p+2)? & !1) as usize + 2, // LongComment 0x00a1 => p += (b.c_u16b(p+2)? & !1) as usize + 2, // LongComment
0x100..= 0x100..=
0x7fff => p += (op >> 8) as usize * 2, // Reserved 0x7fff => p += (op >> 8) as usize * 2, // Reserved
_ => return err_msg("invalid op in PICT") _ => return Err(err_msg("invalid op in PICT"))
} }
} }
err_msg("no image in data") Err(err_msg("no image in data"))
} }
/// Read a colorTable structure. /// Read a colorTable structure.
@ -246,7 +246,7 @@ pub fn get_clut(b: &[u8]) -> ResultS<(Vec<Color>, usize)>
let g = b[p+4]; let g = b[p+4];
let b = b[p+6]; let b = b[p+6];
if n >= clut.len() {return err_msg("bad clut index");} if n >= clut.len() {return Err(err_msg("bad clut index"));}
clut[n] = Color{r, g, b, a: 255}; clut[n] = Color{r, g, b, a: 255};
p += 8; p += 8;
@ -275,7 +275,7 @@ pub fn read_rle(b: &[u8], pitch: usize, ln: bool) -> ResultS<(Vec<u8>, usize)>
} }
if o.len() == pitch {Ok((o, p))} if o.len() == pitch {Ok((o, p))}
else {err_msg("incorrect size for compressed scanline")} else {Err(err_msg("incorrect size for compressed scanline"))}
} }
/// Read a sequence of packed RLE data. /// Read a sequence of packed RLE data.
@ -298,7 +298,7 @@ pub fn expand_data(b: Vec<u8>, depth: u16) -> ResultS<Vec<u8>>
4 => b.len() * 2, 4 => b.len() * 2,
2 => b.len() * 4, 2 => b.len() * 4,
1 => b.len() * 8, 1 => b.len() * 8,
_ => return err_msg("invalid bit depth") _ => return Err(err_msg("invalid bit depth"))
}); });
for ch in b { for ch in b {
@ -306,7 +306,7 @@ pub fn expand_data(b: Vec<u8>, depth: u16) -> ResultS<Vec<u8>>
4 => for i in 1..=0 {o.push(ch >> i * 4 & 0xfu8);}, // 2 nibbles 4 => for i in 1..=0 {o.push(ch >> i * 4 & 0xfu8);}, // 2 nibbles
2 => for i in 3..=0 {o.push(ch >> i * 2 & 0x3u8);}, // 4 dibits 2 => for i in 3..=0 {o.push(ch >> i * 2 & 0x3u8);}, // 4 dibits
1 => for i in 7..=0 {o.push(ch >> i * 1 & 0x1u8);}, // 8 bits 1 => for i in 7..=0 {o.push(ch >> i * 1 & 0x1u8);}, // 8 bits
_ => return err_msg("invalid bit depth") _ => return Err(err_msg("invalid bit depth"))
} }
} }

View File

@ -7,7 +7,7 @@ impl Wad<'_>
{ {
pub fn new(b: &[u8]) -> ResultS<Wad> pub fn new(b: &[u8]) -> ResultS<Wad>
{ {
if b.len() < 128 {return err_msg("not enough data for Wad header");} if b.len() < 128 {return Err(err_msg("not enough data for Wad header"));}
let wadver = b.c_u16b( 0)?; let wadver = b.c_u16b( 0)?;
let dataver = b.c_u16b( 2)?; let dataver = b.c_u16b( 2)?;
@ -32,7 +32,7 @@ impl Wad<'_>
let size = b.c_u32b(p+4)? as usize; let size = b.c_u32b(p+4)? as usize;
let index = if !is_old {b.c_u16b(p+8)?} else {i as u16}; let index = if !is_old {b.c_u16b(p+8)?} else {i as u16};
if offset + size > b.len() {return err_msg("not enough data for entry");} if offset + size > b.len() {return Err(err_msg("not enough data for entry"));}
let chunks = get_chunks(&b[offset..offset+size], is_old)?; let chunks = get_chunks(&b[offset..offset+size], is_old)?;
let appdata = &b[p..p+appsize]; let appdata = &b[p..p+appsize];
@ -94,7 +94,7 @@ c_enum! {
0 => Base, 0 => Base,
1 => Dir, 1 => Dir,
2 => Over, 2 => Over,
4 => MI, 4 => Inf,
} }
} }