Skip to content

Commit

Permalink
Merge branch 'main' into integration_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai authored Oct 27, 2023
2 parents 7a94bbf + c057e22 commit 822afa2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 40 deletions.
19 changes: 8 additions & 11 deletions brro-compressor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,14 @@ fn process_directory(arguments: &Args) -> Result<(), Box<dyn Error>> {

std::fs::create_dir_all(&base_dir)?;
//read
let files = bro_reader::dir_reader(&arguments.input)?;
// TODO: This should be calling `process_single_file` and avoid code duplication

for (index, data) in files.contents.iter().enumerate() {
let arr: &[u8] = data;
// TODO: This should be calling `process_single_file` and avoid code duplication
for file in bro_reader::dir_reader(&arguments.input)? {
//decompress
let decompressed_data = decompress_data(arr);
let decompressed_data = decompress_data(&file.contents);
//write
let path = base_dir.join(
files.original_paths[index]
file.original_path
.file_name()
.ok_or("path has no file name")?,
);
Expand All @@ -68,13 +66,12 @@ fn process_directory(arguments: &Args) -> Result<(), Box<dyn Error>> {
let base_dir = arguments.input.with_file_name(new_name);

std::fs::create_dir_all(&base_dir)?;
//read
let files = wav_reader::dir_reader(&arguments.input)?;

for (index, (vec_data, tag)) in files.contents.into_iter().enumerate() {
let compressed_data = compress_data(&vec_data, &tag, arguments);
//read
for file in wav_reader::dir_reader(&arguments.input)? {
let compressed_data = compress_data(&file.contents, &file.tag, arguments);
let mut path = base_dir.join(
files.original_paths[index]
file.original_path
.file_name()
.ok_or("path has no file name")?,
);
Expand Down
39 changes: 23 additions & 16 deletions brro-compressor/src/utils/readers/bro_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,33 @@ fn process_bro_file(file_path: &Path) -> io::Result<Vec<u8>> {
Ok(contents)
}

pub struct Files {
pub contents: Vec<Vec<u8>>,
pub original_paths: Vec<PathBuf>,
pub struct BroFile {
pub contents: Vec<u8>,
pub original_path: PathBuf,
}

// Function to read and process files in a directory
pub fn dir_reader(directory_path: &Path) -> io::Result<Files> {
let mut contents: Vec<Vec<u8>> = Vec::new();
let mut original_paths: Vec<PathBuf> = Vec::new();
pub fn dir_reader(directory_path: &Path) -> io::Result<Vec<BroFile>> {
let mut files = vec![];

// Iterate through entries (files and subdirectories) in the given directory
for entry in fs::read_dir(directory_path)? {
let file_path = entry?.path();

contents.push(read_file(&file_path)?);
original_paths.push(file_path);
files.push(BroFile {
contents: read_file(&file_path)?,
original_path: file_path,
})
}
Ok(Files {contents, original_paths})

Ok(files)
}

pub fn read_file(file_path: &Path) -> Result<Vec<u8>, Error> {
if is_bro_file(file_path)? {
// If it's a WAV file, process it using the process_wav_file function
Ok(process_bro_file(file_path)?)
}
else {
} else {
Err(Error::new(io::ErrorKind::Other, "File is not a bro file"))
}
}
Expand All @@ -50,7 +51,6 @@ fn is_bro_file(file_path: &Path) -> io::Result<bool> {
Ok(header.starts_with(b"BRRO"))
}


/// Read a file by chunks and processes the chunks
pub fn process_by_chunk(file_path: &Path) -> Result<(), std::io::Error> {
let mut file = std::fs::File::open(file_path)?;
Expand All @@ -61,10 +61,17 @@ pub fn process_by_chunk(file_path: &Path) -> Result<(), std::io::Error> {

loop {
let mut chunk = Vec::with_capacity(chunk_size);
let n = file.by_ref().take(chunk_size as u64).read_to_end(&mut chunk)?;
if n == 0 { break; }
let n = file
.by_ref()
.take(chunk_size as u64)
.read_to_end(&mut chunk)?;
if n == 0 {
break;
}
list_of_chunks.push(chunk);
if n < chunk_size { break; }
if n < chunk_size {
break;
}
}
Ok(())
}
}
27 changes: 14 additions & 13 deletions brro-compressor/src/utils/readers/wav_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ fn process_wav_file(file_path: &Path) -> io::Result<(Vec<f64>, MetricTag)> {
fn process_raw_file(file_path: &Path) -> io::Result<(Vec<f64>, MetricTag)> {
todo!("Handle RAW file processing here (for example, decoding or encoding): {file_path:?}");
}
// TODO: refactor this into a Vec<File>
pub struct Files {
pub contents: Vec<(Vec<f64>, MetricTag)>,
pub original_paths: Vec<PathBuf>,

pub struct WavFile {
pub contents: Vec<f64>,
pub tag: MetricTag,
pub original_path: PathBuf,
}

/// Read a file by chunks and processes the chunks
Expand All @@ -59,19 +60,19 @@ pub fn process_by_chunk(file_path: &Path) -> Result<(), std::io::Error> {
}

// Function to read and process files in a directory
pub fn dir_reader(directory_path: &Path) -> io::Result<Files> {
let mut contents: Vec<(Vec<f64>, MetricTag)> = Vec::new();

let mut original_paths: Vec<PathBuf> = Vec::new();

// Iterate through entries (files and subdirectories) in the given directory
pub fn dir_reader(directory_path: &Path) -> io::Result<Vec<WavFile>> {
let mut files = vec!();
for entry in fs::read_dir(directory_path)? {
let file_path = entry?.path();

contents.push(read_file(&file_path)?);
original_paths.push(file_path);
let (contents, tag) = read_file(&file_path)?;
files.push(WavFile {
contents,
tag,
original_path: file_path,
})
}
Ok(Files {contents, original_paths})
Ok(files)
}

pub fn read_file(file_path: &Path) -> Result<(Vec<f64>, MetricTag), Error> {
Expand Down

0 comments on commit 822afa2

Please sign in to comment.