Skip to content

Commit

Permalink
fix: throw error instead of panicking when expected minitdf file form…
Browse files Browse the repository at this point in the history
…ats are not found in provided path.
  • Loading branch information
sebastian-wehner-bruker committed Nov 6, 2023
1 parent 8b2588b commit 09fa88d
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/file_readers/spectrum_readers/mini_tdf_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use {
rayon::prelude::*,
std::path::PathBuf,
};
use crate::file_readers::FileFormatError;

#[derive(Debug, Default, Clone)]
pub struct MiniTDFReader {
Expand All @@ -24,15 +25,21 @@ pub struct MiniTDFReader {
frame_reader: BinFileReader,
}

fn find_ms2spectrum_file(ms2_dir_path: &str, extension: String) -> String {
fn find_ms2spectrum_file(ms2_dir_path: &str, extension: String) -> Result<String, FileFormatError> {
let files = fs::read_dir(ms2_dir_path).unwrap();
for file in files {
let filename = file.unwrap().path().file_name().unwrap().to_str().unwrap().to_owned();
if filename.ends_with( std::format!("ms2spectrum.{}", extension).as_str()) {
return filename
return Ok(filename)
}
}
panic!("No '*.ms2spectrum.{}' file found in '{}'", extension, ms2_dir_path)
let err = match extension.as_str() {
"parquet" => FileFormatError::MetadataFilesAreMissing,
"bin" => FileFormatError::BinaryFilesAreMissing,
_ => FileFormatError::BinaryFilesAreMissing
};
println!("{}", format!("No '*.ms2spectrum.{}' file found in '{}'", extension, ms2_dir_path));
return Err(err);
}


Expand All @@ -48,7 +55,7 @@ impl MiniTDFReader {

fn read_parquet_file_name(&mut self) {
let mut path: PathBuf = PathBuf::from(&self.path_name);
let ms2_parquet_file = find_ms2spectrum_file(&self.path_name, "parquet".to_owned());
let ms2_parquet_file = find_ms2spectrum_file(&self.path_name, "parquet".to_owned()).unwrap();
path.push(ms2_parquet_file);
self.parquet_file_name = path.to_string_lossy().into_owned();
}
Expand All @@ -59,7 +66,7 @@ impl MiniTDFReader {
}
fn set_spectrum_reader(&mut self) {
let mut path: PathBuf = PathBuf::from(&self.path_name);
let ms2_bin_file = find_ms2spectrum_file(&self.path_name, "bin".to_owned());
let ms2_bin_file = find_ms2spectrum_file(&self.path_name, "bin".to_owned()).unwrap();
path.push(ms2_bin_file);
let file_name: String = path.to_string_lossy().into_owned();
self.frame_reader =
Expand Down

0 comments on commit 09fa88d

Please sign in to comment.