Browse Source

Add description strings for some errors

master
Matteo Cypriani 3 years ago
parent
commit
5a6feec1d6
4 changed files with 32 additions and 28 deletions
  1. +4
    -2
      src/cue_sheet.rs
  2. +18
    -19
      src/error.rs
  3. +6
    -4
      src/metadata/mod.rs
  4. +4
    -3
      src/raw_audio.rs

+ 4
- 2
src/cue_sheet.rs View File

@@ -51,14 +51,16 @@ pub fn write_cue_sheet(img_path: &String, metadata: &NrgMetadata)
let img_name = PathBuf::from(img_path);
let img_name = match img_name.file_name() {
Some(name) => name,
None => return Err(NrgError::FileName),
None => return Err(NrgError::FileName(img_path.clone())),
};

// Set the cue sheet file's name
let mut cue_name = PathBuf::from(img_name);
if cue_name.extension().unwrap_or(OsStr::new("")) == "cue" {
// img_path's extension was already .cue: problem!
return Err(NrgError::FileName);
return Err(NrgError::FileName("Input and output file are identical"
.to_string()));

}
cue_name.set_extension("cue");



+ 18
- 19
src/error.rs View File

@@ -32,11 +32,10 @@ use std::io;
pub enum NrgError {
Io(io::Error),
String(ffi::IntoStringError),
NrgFormat,
NrgFormatV1,
NrgChunkId,
NrgFormat(String),
NrgChunkId(String),
NoNrgCue,
FileName,
FileName(String),
AudioReadError,
AudioWriteError,
}
@@ -46,13 +45,15 @@ impl fmt::Display for NrgError {
match *self {
NrgError::Io(ref err) => err.fmt(f),
NrgError::String(ref err) => err.fmt(f),
NrgError::NrgFormat => write!(f, "NRG format unknown."),
NrgError::NrgFormatV1 => write!(f, "NRG v1 format is not handled."),
NrgError::NrgChunkId => write!(f, "NRG chunk ID unknown."),
NrgError::NoNrgCue => write!(f, "NRG cue sheet chunk absent."),
NrgError::FileName => write!(f, "Invalid file name."),
NrgError::AudioReadError => write!(f, "Error reading raw audio."),
NrgError::AudioWriteError => write!(f, "Error writing raw audio."),
NrgError::NrgFormat(ref err) =>
write!(f, "NRG format error: {}", err),
NrgError::NrgChunkId(ref err) =>
write!(f, "NRG chunk ID unknown: {}", err),
NrgError::NoNrgCue => write!(f, "NRG cue sheet chunk absent"),
NrgError::FileName(ref err) =>
write!(f, "Invalid file name: {}", err),
NrgError::AudioReadError => write!(f, "Error reading raw audio"),
NrgError::AudioWriteError => write!(f, "Error writing raw audio"),
}
}
}
@@ -62,11 +63,10 @@ impl Error for NrgError {
match *self {
NrgError::Io(ref err) => err.description(),
NrgError::String(ref err) => err.description(),
NrgError::NrgFormat => "NRG format",
NrgError::NrgFormatV1 => "NRG format v1",
NrgError::NrgChunkId => "NRG chunk ID",
NrgError::NrgFormat(_) => "NRG format",
NrgError::NrgChunkId(_) => "NRG chunk ID",
NrgError::NoNrgCue => "No NRG cue",
NrgError::FileName => "File name",
NrgError::FileName(_) => "File name",
NrgError::AudioReadError => "Audio read error",
NrgError::AudioWriteError => "Audio write error",
}
@@ -76,11 +76,10 @@ impl Error for NrgError {
match *self {
NrgError::Io(ref err) => Some(err),
NrgError::String(ref err) => Some(err),
NrgError::NrgFormat => None,
NrgError::NrgFormatV1 => None,
NrgError::NrgChunkId => None,
NrgError::NrgFormat(_) => None,
NrgError::NrgChunkId(_) => None,
NrgError::NoNrgCue => None,
NrgError::FileName => None,
NrgError::FileName(_) => None,
NrgError::AudioReadError => None,
NrgError::AudioWriteError => None,
}


+ 6
- 4
src/metadata/mod.rs View File

@@ -56,7 +56,8 @@ pub fn read_nrg_metadata(fd: &mut File) -> Result<NrgMetadata, NrgError> {
nm.nrg_version = try!(read_nrg_version(fd, nm.file_size));
if nm.nrg_version != 2 {
// We handle only NRG v2
return Err(NrgError::NrgFormatV1);
return Err(NrgError::NrgFormat(
"NRG v1 format is not handled".to_string()));
}

// Read the first chunk offset
@@ -78,7 +79,8 @@ pub fn read_nrg_metadata(fd: &mut File) -> Result<NrgMetadata, NrgError> {
fn read_nrg_version(fd: &mut File, file_size: u64) -> Result<u8, NrgError> {
if file_size < 12 {
// Input file too small
return Err(NrgError::NrgFormat);
return Err(NrgError::NrgFormat(
"Input file is to small to be an NRG image".to_string()));
}

// In NRG v2, the main footer is on the last 12 bytes
@@ -95,7 +97,7 @@ fn read_nrg_version(fd: &mut File, file_size: u64) -> Result<u8, NrgError> {
return Ok(1); // NRG v1
}

Err(NrgError::NrgFormat) // Unknown format
Err(NrgError::NrgFormat("Unknown format".to_string()))
}


@@ -131,7 +133,7 @@ fn read_nrg_chunks(fd: &mut File, nm: &mut NrgMetadata) -> Result<(), NrgError>
try!(skip_chunk(fd));
nm.skipped_chunks.push(chunk_id);
},
_ => { println!("{}", chunk_id); return Err(NrgError::NrgChunkId); }, //fixme
_ => return Err(NrgError::NrgChunkId(chunk_id)),
}
}
Ok(())


+ 4
- 3
src/raw_audio.rs View File

@@ -96,12 +96,13 @@ pub fn extract_nrg_raw_audio(in_fd: &mut File,
fn make_output_file_name(img_path: &String) -> Result<String, NrgError> {
let mut name = PathBuf::from(img_path);
name.set_extension("raw");
let name = PathBuf::from(name);
let name = try!(name.file_name().ok_or(NrgError::FileName));
let name = try!(name.file_name().ok_or(
NrgError::FileName(name.to_string_lossy().into_owned())));

// Make sure the new name and the original name are different
if name == img_path.as_str() {
return Err(NrgError::FileName);
return Err(NrgError::FileName("Input and output file are identical"
.to_string()));
}

Ok(name.to_string_lossy().into_owned())


Loading…
Cancel
Save