diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..dbbab18 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod voc; diff --git a/src/main.rs b/src/main.rs index 6231225..37cf2cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -pub mod voc; +use voctool::*; fn main() { let cmd = clap::Command::new(env!("CARGO_CRATE_NAME")) diff --git a/src/voc/blocks.rs b/src/voc/blocks.rs index 843afc6..a15daa8 100644 --- a/src/voc/blocks.rs +++ b/src/voc/blocks.rs @@ -34,6 +34,7 @@ pub trait Block: fmt::Debug { } pub struct BlockInfo { + pub block_type: BlockType, pub start_address: u32, pub end_address: u32, } @@ -52,12 +53,11 @@ pub struct Terminator { } impl Terminator { - #[allow(dead_code)] pub fn new() -> Self { Self { info: None, block_type: BlockType::Terminator } } pub fn from_stream(_fp: &mut File, start_address: u32, end_address: u32) -> Self { Self { - info: Some(BlockInfo { start_address, end_address }), + info: Some(BlockInfo { block_type: BlockType::Terminator, start_address, end_address }), block_type: BlockType::Terminator, } } @@ -86,7 +86,6 @@ pub struct SoundData { } impl SoundData { - #[allow(dead_code)] pub fn new(sample_rate: u32, codec: Codec, data: Vec) -> Self { Self { info: None, @@ -121,7 +120,7 @@ impl SoundData { drop(fp.read(&mut data)); Self { - info: Some(BlockInfo { start_address, end_address }), + info: Some(BlockInfo { block_type: BlockType::SoundData, start_address, end_address }), block_type: BlockType::SoundData, sample_rate, codec, @@ -170,7 +169,6 @@ pub struct SoundDataContinuation { } impl SoundDataContinuation { - #[allow(dead_code)] pub fn new(data: Vec) -> Self { Self { info: None, @@ -184,7 +182,7 @@ impl SoundDataContinuation { drop(fp.read(&mut data)); Self { - info: Some(BlockInfo { start_address, end_address }), + info: Some(BlockInfo { block_type: BlockType::SoundDataContinuation, start_address, end_address }), block_type: BlockType::SoundDataContinuation, data, } @@ -225,7 +223,6 @@ pub struct Silence { } impl Silence { - #[allow(dead_code)] pub fn new(length: u16, sample_rate: u32) -> Self { Self { info: None, @@ -244,7 +241,7 @@ impl Silence { let sample_rate: u32 = 1000000u32 / (256u32 - frequency_divisor_buffer[0] as u32); Self { - info: Some(BlockInfo { start_address, end_address }), + info: Some(BlockInfo { block_type: BlockType::Silence, start_address, end_address }), block_type: BlockType::Silence, length, sample_rate, @@ -291,7 +288,6 @@ pub struct Marker { } impl Marker { - #[allow(dead_code)] pub fn new(value: u16) -> Self { Self { info: None, @@ -305,7 +301,7 @@ impl Marker { let value = u16::from_le_bytes(value_buffer); Self { - info: Some(BlockInfo { start_address, end_address }), + info: Some(BlockInfo { block_type: BlockType::Marker, start_address, end_address }), block_type: BlockType::Marker, value, } @@ -348,7 +344,6 @@ pub struct Text { } impl Text { - #[allow(dead_code)] pub fn new(data: Vec) -> Self { Self { info: None, block_type: BlockType::Text, data } } pub fn from_stream(fp: &mut File, start_address: u32, end_address: u32) -> Self { let address = fp.stream_position().unwrap() as u32; @@ -356,7 +351,7 @@ impl Text { drop(fp.read(&mut data)); Self { - info: Some(BlockInfo { start_address, end_address }), + info: Some(BlockInfo { block_type: BlockType::Text, start_address, end_address }), block_type: BlockType::Text, data, } @@ -398,7 +393,6 @@ pub struct RepeatStart { } impl RepeatStart { - #[allow(dead_code)] pub fn new(count: u16) -> Self { Self { info: None, @@ -412,7 +406,7 @@ impl RepeatStart { let count = u16::from_le_bytes(count_buffer) + 1; Self { - info: Some(BlockInfo { start_address, end_address }), + info: Some(BlockInfo { block_type: BlockType::RepeatStart, start_address, end_address }), block_type: BlockType::RepeatStart, count, } @@ -454,11 +448,10 @@ pub struct RepeatEnd { } impl RepeatEnd { - #[allow(dead_code)] pub fn new() -> Self { Self { info: None, block_type: BlockType::RepeatEnd } } pub fn from_stream(_fp: &mut File, start_address: u32, end_address: u32) -> Self { Self { - info: Some(BlockInfo { start_address, end_address }), + info: Some(BlockInfo { block_type: BlockType::RepeatEnd, start_address, end_address }), block_type: BlockType::RepeatEnd, } } diff --git a/src/voc.rs b/src/voc/mod.rs similarity index 99% rename from src/voc.rs rename to src/voc/mod.rs index e7c853a..f6a9368 100644 --- a/src/voc.rs +++ b/src/voc/mod.rs @@ -3,7 +3,7 @@ use std::io::{Read, Seek, Write}; use crate::voc::blocks::*; -mod blocks; +pub mod blocks; #[derive(Debug)] pub struct VocFile { diff --git a/tests/EDEN.MUS b/tests/EDEN.MUS new file mode 100644 index 0000000..5ca167b Binary files /dev/null and b/tests/EDEN.MUS differ diff --git a/tests/unit_tests.rs b/tests/unit_tests.rs new file mode 100644 index 0000000..4bc979f --- /dev/null +++ b/tests/unit_tests.rs @@ -0,0 +1,25 @@ +mod tests { + use voctool::voc; + use voctool::voc::VocFile; + use voctool::voc::blocks::BlockType; + + #[test] + fn text_eden_mus() { + let tests_path = std::env::current_dir().unwrap().join("tests"); + let file_path = tests_path.join("EDEN.MUS"); + let voc = VocFile::from_file(file_path.to_str().unwrap()); + + // Version + assert_eq!((1, 10), voc.version); + + // Checksum + assert!(voc.checksum); + + // Text Block + let block = voc.blocks.get(0).unwrap().get_info().as_ref().unwrap(); + assert_eq!(BlockType::Text, block.block_type); + assert_eq!(0x1A, block.start_address); + assert_eq!(0xB5, block.end_address); + assert_eq!(155, block.get_size()); + } +} \ No newline at end of file