don't use unwrap

gui-branch
an 2019-04-12 01:02:29 -04:00
parent 8c9415b867
commit 9e31f6e498
2 changed files with 27 additions and 9 deletions

View File

@ -216,7 +216,10 @@ macro_rules! check_data {
/// assert_eq!(usize_from_u32(777u32), 777usize);
/// ```
#[inline]
pub fn usize_from_u32(n: u32) -> usize {usize::try_from(n).unwrap()}
pub fn usize_from_u32(n: u32) -> usize
{
usize::try_from(n).expect("platform is 16-bit")
}
/// Creates an `Ident` from a slice.
///
@ -232,7 +235,10 @@ pub fn usize_from_u32(n: u32) -> usize {usize::try_from(n).unwrap()}
/// assert_eq!(ident(b"POLY"), Ident([b'P', b'O', b'L', b'Y']));
/// ```
#[inline]
pub fn ident(b: &[u8]) -> Ident {Ident(b[0..4].try_into().unwrap())}
pub fn ident(b: &[u8]) -> Ident
{
Ident(b[0..4].try_into().expect("not enough data"))
}
/// Applies `u32::from_be_bytes` to a slice.
///
@ -248,7 +254,10 @@ pub fn ident(b: &[u8]) -> Ident {Ident(b[0..4].try_into().unwrap())}
/// assert_eq!(u32b(&[0x00, 0x0B, 0xDE, 0x31]), 777_777u32);
/// ```
#[inline]
pub fn u32b(b: &[u8]) -> u32 {u32::from_be_bytes(b[0..4].try_into().unwrap())}
pub fn u32b(b: &[u8]) -> u32
{
u32::from_be_bytes(b[0..4].try_into().expect("not enough data"))
}
/// Applies `u16::from_be_bytes` to a slice.
///
@ -264,7 +273,10 @@ pub fn u32b(b: &[u8]) -> u32 {u32::from_be_bytes(b[0..4].try_into().unwrap())}
/// assert_eq!(u16b(&[0x1E, 0x61]), 7_777u16);
/// ```
#[inline]
pub fn u16b(b: &[u8]) -> u16 {u16::from_be_bytes(b[0..2].try_into().unwrap())}
pub fn u16b(b: &[u8]) -> u16
{
u16::from_be_bytes(b[0..2].try_into().expect("not enough data"))
}
/// Applies `i32::from_be_bytes` to a slice.
///
@ -280,7 +292,10 @@ pub fn u16b(b: &[u8]) -> u16 {u16::from_be_bytes(b[0..2].try_into().unwrap())}
/// assert_eq!(i32b(&[0xFF, 0x89, 0x52, 0x0F]), -7_777_777i32);
/// ```
#[inline]
pub fn i32b(b: &[u8]) -> i32 {i32::from_be_bytes(b[0..4].try_into().unwrap())}
pub fn i32b(b: &[u8]) -> i32
{
i32::from_be_bytes(b[0..4].try_into().expect("not enough data"))
}
/// Applies `i16::from_be_bytes` to a slice.
///
@ -296,7 +311,10 @@ pub fn i32b(b: &[u8]) -> i32 {i32::from_be_bytes(b[0..4].try_into().unwrap())}
/// assert_eq!(i16b(&[0xE1, 0x9F]), -7_777i16);
/// ```
#[inline]
pub fn i16b(b: &[u8]) -> i16 {i16::from_be_bytes(b[0..2].try_into().unwrap())}
pub fn i16b(b: &[u8]) -> i16
{
i16::from_be_bytes(b[0..2].try_into().expect("not enough data"))
}
/// Applies a read function over a slice.
///

View File

@ -24,15 +24,15 @@ impl MapEditor
{
// TODO: handle errors gracefully
let b = &b[machdr::try_mac_header(b)..];
let wad = map::read(b).unwrap();
let wad = map::read(b).expect("bad map file");
let ent = wad.entries.iter().nth(0).unwrap().1;
let ent = wad.entries.iter().nth(0).expect("no entries").1;
let info = ent.chunks.iter().find_map(|cnk| {
match cnk {
map::chnk::Chunk::Minf(info) => Some(info),
_ => None,
}
}).unwrap().clone();
}).expect("no Minf chunk").clone();
let block = Block{info};