From d21d16605c5110400d430a5f24dbeb2f9c042c14 Mon Sep 17 00:00:00 2001 From: ashley Date: Mon, 14 Aug 2023 20:36:33 -0400 Subject: [PATCH] Refactor --- src/reskit/soundtrack.rs | 67 ++-------------------------------------- src/reskit/utility.rs | 67 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 65 deletions(-) diff --git a/src/reskit/soundtrack.rs b/src/reskit/soundtrack.rs index c4dd390..f3e1005 100644 --- a/src/reskit/soundtrack.rs +++ b/src/reskit/soundtrack.rs @@ -1,17 +1,12 @@ -use std::{error::Error, fs::File, io::Read, str::from_utf8, slice::Iter, convert::TryInto, collections::HashMap}; +use std::{error::Error, fs::File, io::Read, convert::TryInto, collections::HashMap}; use flate2::read::ZlibDecoder; +use crate::reskit::utility::{Either, get_string, get_u8, skip, get_u32, get_i8, get_i32, get_u16, get_i16}; const DMF_MAGIC_NUMBER: &'static str = ".DelekDefleMask."; const DMF_SUPPORTED_VERSION: u8 = 0x18; const DMF_MD: u8 = 0x02; const DMF_MD_ENHANCED_CH3: u8 = 0x42; -#[derive(Debug)] -pub enum Either< Type1, Type2 > { - Type1( Type1 ), - Type2( Type2 ) -} - #[derive(Debug)] pub enum FrameMode { Ntsc, @@ -140,64 +135,6 @@ pub struct DmfModule { samples: Vec } -fn get_string( bytes: &mut Iter<'_, u8>, take: usize ) -> Result> { - let took = bytes.take( take ); - let took: Vec = took.cloned().collect(); - let took = from_utf8( &took )?.to_string(); - - Ok( took ) -} - -fn get_u8( bytes: &mut Iter<'_, u8> ) -> Result> { - let took = bytes.take( 1 ).cloned().next().ok_or( "invalid file: expected 1 byte" )?; - - Ok( took ) -} - -fn get_i8( bytes: &mut Iter<'_, u8> ) -> Result> { - let took = bytes.take( 1 ).cloned().next().ok_or( "invalid file: expected 1 byte" )?; - - Ok( took as i8 ) -} - -fn get_u16( bytes: &mut Iter<'_, u8> ) -> Result> { - let took = bytes.take( 2 ); - let took: Vec = took.cloned().collect(); - let took = u16::from_le_bytes( took[0..2].try_into()? ); - - Ok( took ) -} - -fn get_i16( bytes: &mut Iter<'_, u8> ) -> Result> { - let took = bytes.take( 2 ); - let took: Vec = took.cloned().collect(); - let took = i16::from_le_bytes( took[0..2].try_into()? ); - - Ok( took ) -} - -fn get_u32( bytes: &mut Iter<'_, u8> ) -> Result> { - let took = bytes.take( 4 ); - let took: Vec = took.cloned().collect(); - let took = u32::from_le_bytes( took[0..4].try_into()? ); - - Ok( took ) -} - -fn get_i32( bytes: &mut Iter<'_, u8> ) -> Result> { - let took = bytes.take( 4 ); - let took: Vec = took.cloned().collect(); - let took = i32::from_le_bytes( took[0..4].try_into()? ); - - Ok( took ) -} - -fn skip( bytes: &mut Iter<'_, u8>, by: usize ) { - for _i in 0..by { - bytes.next(); - } -} - impl DmfModule { pub fn from_file( path: &str ) -> Result> { diff --git a/src/reskit/utility.rs b/src/reskit/utility.rs index d3dd273..d266e17 100644 --- a/src/reskit/utility.rs +++ b/src/reskit/utility.rs @@ -1,7 +1,74 @@ +use std::{slice::Iter, error::Error, str::from_utf8, convert::TryInto}; + + +#[derive(Debug)] +pub enum Either< Type1, Type2 > { + Type1( Type1 ), + Type2( Type2 ) +} + pub fn print_error( error_msg: &str ) { red!( "fatal: " ); println!( "{}", error_msg ); } pub fn print_good( message: &str ) { green!( "success: " ); println!( "{}", message ); +} + +pub fn get_string( bytes: &mut Iter<'_, u8>, take: usize ) -> Result> { + let took = bytes.take( take ); + let took: Vec = took.cloned().collect(); + let took = from_utf8( &took )?.to_string(); + + Ok( took ) +} + +pub fn get_u8( bytes: &mut Iter<'_, u8> ) -> Result> { + let took = bytes.take( 1 ).cloned().next().ok_or( "invalid file: expected 1 byte" )?; + + Ok( took ) +} + +pub fn get_i8( bytes: &mut Iter<'_, u8> ) -> Result> { + let took = bytes.take( 1 ).cloned().next().ok_or( "invalid file: expected 1 byte" )?; + + Ok( took as i8 ) +} + +pub fn get_u16( bytes: &mut Iter<'_, u8> ) -> Result> { + let took = bytes.take( 2 ); + let took: Vec = took.cloned().collect(); + let took = u16::from_le_bytes( took[0..2].try_into()? ); + + Ok( took ) +} + +pub fn get_i16( bytes: &mut Iter<'_, u8> ) -> Result> { + let took = bytes.take( 2 ); + let took: Vec = took.cloned().collect(); + let took = i16::from_le_bytes( took[0..2].try_into()? ); + + Ok( took ) +} + +pub fn get_u32( bytes: &mut Iter<'_, u8> ) -> Result> { + let took = bytes.take( 4 ); + let took: Vec = took.cloned().collect(); + let took = u32::from_le_bytes( took[0..4].try_into()? ); + + Ok( took ) +} + +pub fn get_i32( bytes: &mut Iter<'_, u8> ) -> Result> { + let took = bytes.take( 4 ); + let took: Vec = took.cloned().collect(); + let took = i32::from_le_bytes( took[0..4].try_into()? ); + + Ok( took ) +} + +pub fn skip( bytes: &mut Iter<'_, u8>, by: usize ) { + for _i in 0..by { + bytes.next(); + } } \ No newline at end of file