Start to remove size from structs and use data.len() instead.

This commit is contained in:
Pedro de Oliveira 2022-11-13 05:35:59 +00:00
parent 33484bfa97
commit c9aa7c30ef
1 changed files with 27 additions and 16 deletions

View File

@ -123,14 +123,14 @@ impl VocFile {
let mut data: Vec<u8> = vec![0; (next - address) as usize];
drop(fp.read(&mut data));
let block = SoundData::new(block_size, sample_rate, codec, data);
let block = SoundData::new(sample_rate, codec, data);
voc.blocks.push(Box::new(block));
}
BlockType::SoundDataContinuation => {
let mut data: Vec<u8> = vec![0; (next - address) as usize];
drop(fp.read(&mut data));
let block = SoundDataContinuation::new(block_size, data);
let block = SoundDataContinuation::new(data);
voc.blocks.push(Box::new(block));
}
BlockType::Silence => {
@ -149,7 +149,7 @@ impl VocFile {
let mut data: Vec<u8> = vec![0; (next - address) as usize];
drop(fp.read(&mut data));
let block = Text { block_type, size: block_size, data };
let block = Text::new(data);
voc.blocks.push(Box::new(block));
}
_ => panic!("block Type {:?} not implemented", block_type),
@ -226,17 +226,15 @@ impl fmt::Debug for Terminator {
pub struct SoundData {
block_type: BlockType,
pub size: i32,
pub sample_rate: i32,
pub codec: Codec,
pub data: Vec<u8>,
}
impl SoundData {
pub fn new(size: i32, sample_rate: i32, codec: Codec, data: Vec<u8>) -> Self {
pub fn new(sample_rate: i32, codec: Codec, data: Vec<u8>) -> Self {
Self {
block_type: BlockType::SoundData,
size,
sample_rate,
codec,
data,
@ -246,7 +244,7 @@ impl SoundData {
impl Block for SoundData {
fn to_bytes(&self) -> Vec<u8> {
let size_bytes: [u8; 4] = self.size.to_le_bytes();
let size_bytes: [u8; 4] = (self.data.len() as u32 + 2).to_le_bytes();
let frequency_divisor: u8 = (256i32 - 1000000i32 / self.sample_rate as i32) as u8;
let mut result: Vec<u8> = vec![
@ -267,7 +265,7 @@ impl Block for SoundData {
impl fmt::Debug for SoundData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SoundData")
.field("size", &self.size)
.field("size", &self.data.len())
.field("sample_rate", &self.sample_rate)
.field("codec", &self.codec)
.finish()
@ -278,15 +276,13 @@ impl fmt::Debug for SoundData {
pub struct SoundDataContinuation {
pub block_type: BlockType,
pub size: i32,
pub data: Vec<u8>,
}
impl SoundDataContinuation {
pub fn new(size: i32, data: Vec<u8>) -> Self {
pub fn new(data: Vec<u8>) -> Self {
Self {
block_type: BlockType::SoundDataContinuation,
size,
data,
}
}
@ -294,7 +290,14 @@ impl SoundDataContinuation {
impl Block for SoundDataContinuation {
fn to_bytes(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![self.block_type as u8];
let size_bytes: [u8; 4] = (self.data.len() as u32).to_le_bytes();
let mut result: Vec<u8> = vec![
self.block_type as u8,
size_bytes[0],
size_bytes[1],
size_bytes[2]
];
result.extend_from_slice(&self.data);
result
}
@ -303,7 +306,7 @@ impl Block for SoundDataContinuation {
impl fmt::Debug for SoundDataContinuation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SoundDataContinuation")
.field("size", &self.size)
.field("size", &self.data.len())
.finish()
}
}
@ -345,13 +348,21 @@ impl Block for Marker {
pub struct Text {
pub block_type: BlockType,
pub size: i32,
pub data: Vec<u8>,
}
impl Text {
pub fn new(data: Vec<u8>) -> Self {
Self {
block_type: BlockType::SoundData,
data,
}
}
}
impl Block for Text {
fn to_bytes(&self) -> Vec<u8> {
let size_bytes: [u8; 4] = self.size.to_le_bytes();
let size_bytes: [u8; 4] = (self.data.len() as u32).to_le_bytes();
let mut result: Vec<u8> = vec![
self.block_type as u8,
@ -369,7 +380,7 @@ impl Block for Text {
impl fmt::Debug for Text {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Text")
.field("size", &self.size)
.field("size", &self.data.len())
.finish()
}
}