From 55560f4f22c0b8b2c8f18451d03ec0359bb42b72 Mon Sep 17 00:00:00 2001 From: Pedro de Oliveira Date: Sun, 13 Nov 2022 03:22:21 +0000 Subject: [PATCH] Move stuff around --- src/voc.rs | 160 +++++++++++++++++++++++++++++------------------------ 1 file changed, 88 insertions(+), 72 deletions(-) diff --git a/src/voc.rs b/src/voc.rs index 0a8eadc..6390d0f 100644 --- a/src/voc.rs +++ b/src/voc.rs @@ -160,16 +160,39 @@ impl VocFile { } } +type BlockT = Box; + pub trait Block: std::fmt::Debug { fn to_bytes(&self) -> Vec; } +// Terminator + impl Block for Terminator { fn to_bytes(&self) -> Vec { vec![self.block_type as u8] } } +pub struct Terminator { + block_type: BlockType, +} + +impl fmt::Debug for Terminator { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Terminator") + .finish() + } +} + +impl Terminator { + pub fn new() -> Self { + Self { block_type: BlockType::Terminator } + } +} + +// SoundData + impl Block for SoundData { fn to_bytes(&self) -> Vec { let size_bytes: [u8; 4] = self.size.to_le_bytes(); @@ -190,78 +213,6 @@ impl Block for SoundData { } } -impl Block for SoundDataContinuation { - fn to_bytes(&self) -> Vec { - let mut result: Vec = vec![self.block_type as u8]; - result.extend_from_slice(&self.data); - result - } -} - -impl Block for Silence { - fn to_bytes(&self) -> Vec { - panic!("Not implemented"); - vec![self.block_type as u8] - } -} - -impl Block for Marker { - fn to_bytes(&self) -> Vec { - panic!("Not implemented"); - vec![self.block_type as u8] - } -} - -impl Block for Text { - fn to_bytes(&self) -> Vec { - let size_bytes: [u8; 4] = self.size.to_le_bytes(); - - let mut result: Vec = vec![ - self.block_type as u8, - size_bytes[0], - size_bytes[1], - size_bytes[2], - ]; - - result.extend_from_slice(&self.data); - - result - } -} - -impl Block for RepeatStart { - fn to_bytes(&self) -> Vec { - panic!("Not implemented"); - vec![self.block_type as u8] - } -} - -impl Block for RepeatEnd { - fn to_bytes(&self) -> Vec { - panic!("Not implemented"); - vec![self.block_type as u8] - } -} - -type BlockT = Box; - -pub struct Terminator { - block_type: BlockType, -} - -impl fmt::Debug for Terminator { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("Terminator") - .finish() - } -} - -impl Terminator { - pub fn new() -> Self { - Self { block_type: BlockType::Terminator } - } -} - pub struct SoundData { block_type: BlockType, pub size: i32, @@ -292,6 +243,16 @@ impl SoundData { } } +// SoundDataContinuation + +impl Block for SoundDataContinuation { + fn to_bytes(&self) -> Vec { + let mut result: Vec = vec![self.block_type as u8]; + result.extend_from_slice(&self.data); + result + } +} + pub struct SoundDataContinuation { pub block_type: BlockType, pub size: i32, @@ -316,6 +277,15 @@ impl SoundDataContinuation { } } +// Silence + +impl Block for Silence { + fn to_bytes(&self) -> Vec { + panic!("Not implemented"); + vec![self.block_type as u8] + } +} + #[derive(Debug)] pub struct Silence { pub block_type: BlockType, @@ -324,6 +294,15 @@ pub struct Silence { pub length: i16, } +// Marker + +impl Block for Marker { + fn to_bytes(&self) -> Vec { + panic!("Not implemented"); + vec![self.block_type as u8] + } +} + #[derive(Debug)] pub struct Marker { block_type: BlockType, @@ -331,6 +310,25 @@ pub struct Marker { value: i16, } +// Text + +impl Block for Text { + fn to_bytes(&self) -> Vec { + let size_bytes: [u8; 4] = self.size.to_le_bytes(); + + let mut result: Vec = vec![ + self.block_type as u8, + size_bytes[0], + size_bytes[1], + size_bytes[2], + ]; + + result.extend_from_slice(&self.data); + + result + } +} + pub struct Text { pub block_type: BlockType, pub size: i32, @@ -345,6 +343,15 @@ impl fmt::Debug for Text { } } +// RepeatStart + +impl Block for RepeatStart { + fn to_bytes(&self) -> Vec { + panic!("Not implemented"); + vec![self.block_type as u8] + } +} + #[derive(Debug)] pub struct RepeatStart { block_type: BlockType, @@ -352,6 +359,15 @@ pub struct RepeatStart { repeat: i16, } +// RepeatEnd + +impl Block for RepeatEnd { + fn to_bytes(&self) -> Vec { + panic!("Not implemented"); + vec![self.block_type as u8] + } +} + #[derive(Debug)] pub struct RepeatEnd { block_type: BlockType,