Cleanup big match statement

This commit is contained in:
Pedro de Oliveira 2023-04-23 00:38:14 +01:00
parent 18fbcfa71f
commit 32ea4e5503
1 changed files with 21 additions and 44 deletions

View File

@ -69,55 +69,32 @@ impl VocFile {
_ => panic!("Bad block type. Got {}", block_type_buffer[0])
};
let mut block_size: u32 = 0;
// Terminator and RepeatEnd dont have size in header so block_size is 0
if block_type != BlockType::Terminator || block_type != BlockType::RepeatEnd {
let mut block_size_buffer: [u8; 3] = [0; 3];
drop(file.read(&mut block_size_buffer));
block_size = u32::from_le_bytes(
[block_size_buffer[0], block_size_buffer[1], block_size_buffer[2], 0]
);
}
let block_size = match block_type {
BlockType::Terminator | BlockType::RepeatEnd => 0,
_ => {
let mut block_size_buffer: [u8; 3] = [0; 3];
file.read_exact(&mut block_size_buffer).unwrap();
u32::from_le_bytes([block_size_buffer[0], block_size_buffer[1], block_size_buffer[2], 0])
}
};
let current_address = file.stream_position().unwrap() as u32;
let block_end_address = current_address + block_size;
let block: Box<dyn Block> = match block_type {
BlockType::Terminator => Box::new(Terminator::from_stream(&mut file, block_start_address, block_end_address)),
BlockType::SoundData => Box::new(SoundData::from_stream(&mut file, block_start_address, block_end_address)),
BlockType::SoundDataContinuation => Box::new(SoundDataContinuation::from_stream(&mut file, block_start_address, block_end_address)),
BlockType::Silence => Box::new(Silence::from_stream(&mut file, block_start_address, block_end_address)),
BlockType::Marker => Box::new(Marker::from_stream(&mut file, block_start_address, block_end_address)),
BlockType::Text => Box::new(Text::from_stream(&mut file, block_start_address, block_end_address)),
BlockType::RepeatStart => Box::new(RepeatStart::from_stream(&mut file, block_start_address, block_end_address)),
BlockType::RepeatEnd => Box::new(RepeatEnd::from_stream(&mut file, block_start_address, block_end_address)),
};
voc_file.blocks.push(block);
match block_type {
BlockType::Terminator => {
let block = Terminator::from_stream(&mut file, block_start_address, block_end_address);
voc_file.blocks.push(Box::new(block));
break;
}
BlockType::SoundData => {
let block = SoundData::from_stream(&mut file, block_start_address, block_end_address);
voc_file.blocks.push(Box::new(block));
}
BlockType::SoundDataContinuation => {
let block = SoundDataContinuation::from_stream(&mut file, block_start_address, block_end_address);
voc_file.blocks.push(Box::new(block));
}
BlockType::Silence => {
let block = Silence::from_stream(&mut file, block_start_address, block_end_address);
voc_file.blocks.push(Box::new(block));
}
BlockType::Marker => {
let block = Marker::from_stream(&mut file, block_start_address, block_end_address);
voc_file.blocks.push(Box::new(block));
}
BlockType::Text => {
let block = Text::from_stream(&mut file, block_start_address, block_end_address);
voc_file.blocks.push(Box::new(block));
}
BlockType::RepeatStart => {
let block = RepeatStart::from_stream(&mut file, block_start_address, block_end_address);
voc_file.blocks.push(Box::new(block));
}
BlockType::RepeatEnd => {
let block = RepeatEnd::from_stream(&mut file, block_start_address, block_end_address);
voc_file.blocks.push(Box::new(block));
}
if block_type == BlockType::Terminator {
break;
}
}
voc_file