Refactor
parent
aea97be053
commit
d21d16605c
|
@ -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<Sample>
|
||||
}
|
||||
|
||||
fn get_string( bytes: &mut Iter<'_, u8>, take: usize ) -> Result<String, Box<dyn Error>> {
|
||||
let took = bytes.take( take );
|
||||
let took: Vec<u8> = took.cloned().collect();
|
||||
let took = from_utf8( &took )?.to_string();
|
||||
|
||||
Ok( took )
|
||||
}
|
||||
|
||||
fn get_u8( bytes: &mut Iter<'_, u8> ) -> Result<u8, Box<dyn Error>> {
|
||||
let took = bytes.take( 1 ).cloned().next().ok_or( "invalid file: expected 1 byte" )?;
|
||||
|
||||
Ok( took )
|
||||
}
|
||||
|
||||
fn get_i8( bytes: &mut Iter<'_, u8> ) -> Result<i8, Box<dyn Error>> {
|
||||
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<u16, Box<dyn Error>> {
|
||||
let took = bytes.take( 2 );
|
||||
let took: Vec<u8> = took.cloned().collect();
|
||||
let took = u16::from_le_bytes( took[0..2].try_into()? );
|
||||
|
||||
Ok( took )
|
||||
}
|
||||
|
||||
fn get_i16( bytes: &mut Iter<'_, u8> ) -> Result<i16, Box<dyn Error>> {
|
||||
let took = bytes.take( 2 );
|
||||
let took: Vec<u8> = took.cloned().collect();
|
||||
let took = i16::from_le_bytes( took[0..2].try_into()? );
|
||||
|
||||
Ok( took )
|
||||
}
|
||||
|
||||
fn get_u32( bytes: &mut Iter<'_, u8> ) -> Result<u32, Box<dyn Error>> {
|
||||
let took = bytes.take( 4 );
|
||||
let took: Vec<u8> = took.cloned().collect();
|
||||
let took = u32::from_le_bytes( took[0..4].try_into()? );
|
||||
|
||||
Ok( took )
|
||||
}
|
||||
|
||||
fn get_i32( bytes: &mut Iter<'_, u8> ) -> Result<i32, Box<dyn Error>> {
|
||||
let took = bytes.take( 4 );
|
||||
let took: Vec<u8> = 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<DmfModule, Box<dyn Error>> {
|
||||
|
|
|
@ -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<String, Box<dyn Error>> {
|
||||
let took = bytes.take( take );
|
||||
let took: Vec<u8> = took.cloned().collect();
|
||||
let took = from_utf8( &took )?.to_string();
|
||||
|
||||
Ok( took )
|
||||
}
|
||||
|
||||
pub fn get_u8( bytes: &mut Iter<'_, u8> ) -> Result<u8, Box<dyn Error>> {
|
||||
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<i8, Box<dyn Error>> {
|
||||
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<u16, Box<dyn Error>> {
|
||||
let took = bytes.take( 2 );
|
||||
let took: Vec<u8> = 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<i16, Box<dyn Error>> {
|
||||
let took = bytes.take( 2 );
|
||||
let took: Vec<u8> = 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<u32, Box<dyn Error>> {
|
||||
let took = bytes.take( 4 );
|
||||
let took: Vec<u8> = 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<i32, Box<dyn Error>> {
|
||||
let took = bytes.take( 4 );
|
||||
let took: Vec<u8> = 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue