From 8c1917abe2c14cbd1e26de00fc5b6507aab7b600 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Sun, 15 Nov 2020 20:00:00 +0200 Subject: [PATCH] Simplify parse_block_and_undo --- electrs_index/src/index.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/electrs_index/src/index.rs b/electrs_index/src/index.rs index 7b40b9d..b9cb543 100644 --- a/electrs_index/src/index.rs +++ b/electrs_index/src/index.rs @@ -553,21 +553,19 @@ fn parse_block_and_undo(loc: BlockLocation, blk_file: &mut F, undo_file: &mut where F: Read + Seek, { - blk_file - .seek(SeekFrom::Start(loc.data as u64)) - .expect("failed to seek"); - let block: Block = - parse_from(blk_file, loc.data).unwrap_or_else(|_| panic!("bad Block at {:?}", loc)); - let undo: BlockUndo = loc - .undo - .map(|offset| { - parse_from(undo_file, offset).unwrap_or_else(|_| panic!("bad BlockUndo at {:?}", loc)) - }) - .unwrap_or_else(|| { + let block: Block = match parse_from(blk_file, loc.data) { + Ok(block) => block, + Err(err) => panic!("bad Block at {:?}: {}", loc, err), + }; + let undo: BlockUndo = match loc.undo.map(|offset| parse_from(undo_file, offset)) { + Some(Ok(undo)) => undo, + Some(Err(err)) => panic!("bad BlockUndo at {:?}: {}", loc, err), + None => { // genesis block has no undo assert_eq!(block.header.prev_blockhash, BlockHash::default()); BlockUndo::default() // create an empty undo - }); + } + }; ParseResult { loc, block, undo } }