Skip to content

Commit 9100bda

Browse files
committed
Refactor to clarify code
1 parent 68d7b16 commit 9100bda

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

mithril-common/src/cardano_transaction_parser.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -119,38 +119,36 @@ impl CardanoTransactionParser {
119119
let cardano_blocks_reader =
120120
CardanoTransactionParser::cardano_blocks_reader(immutable_file)?;
121121

122-
cardano_blocks_reader
123-
.into_iter()
124-
.map(|b| CardanoTransactionParser::convert_to_block(b, immutable_file))
125-
.collect::<Result<Vec<_>, _>>()
126-
.map_err(|e| anyhow!(e))
122+
let mut blocks = Vec::new();
123+
for block in cardano_blocks_reader {
124+
let block = block.map_err(|e| {
125+
anyhow!(e).context(format!(
126+
"Error while reading block in immutable file: '{:?}'",
127+
immutable_file.path
128+
))
129+
})?;
130+
blocks.push(CardanoTransactionParser::convert_to_block(
131+
&block,
132+
immutable_file,
133+
)?);
134+
}
135+
Ok(blocks)
127136
}
128137

129-
fn convert_to_block(
130-
block: Result<Vec<u8>, std::io::Error>,
131-
immutable_file: &ImmutableFile,
132-
) -> StdResult<Block> {
133-
let block = block.map_err(|e| {
134-
anyhow!(e).context(format!(
135-
"Error while reading block in immutable file: '{:?}'",
136-
immutable_file.path
137-
))
138-
})?;
139-
let multi_era_block = MultiEraBlock::decode(&block).map_err(|e| {
138+
fn convert_to_block(block: &[u8], immutable_file: &ImmutableFile) -> StdResult<Block> {
139+
let multi_era_block = MultiEraBlock::decode(block).map_err(|e| {
140140
anyhow!(e).context(format!(
141141
"Error while decoding block in immutable file: '{:?}'",
142142
immutable_file.path
143143
))
144144
})?;
145-
let block =
146-
Block::try_convert(multi_era_block, immutable_file.number).with_context(|| {
147-
format!(
148-
"CardanoTransactionParser could not read data from block in immutable file: {:?}",
149-
immutable_file.path
150-
)
151-
})?;
152145

153-
Ok(block)
146+
Block::try_convert(multi_era_block, immutable_file.number).with_context(|| {
147+
format!(
148+
"CardanoTransactionParser could not read data from block in immutable file: {:?}",
149+
immutable_file.path
150+
)
151+
})
154152
}
155153

156154
fn cardano_blocks_reader(immutable_file: &ImmutableFile) -> StdResult<Reader> {

0 commit comments

Comments
 (0)