Skip to content

Commit

Permalink
Implement error handling on writer
Browse files Browse the repository at this point in the history
Although possibly overkill for this library, this implements an Error
hierarchy for the Writer to *return* any errors in formatting or io like
converting numbers. Previously the writer would panic, which is
non-ideal in application code.
  • Loading branch information
elrnv committed Mar 11, 2019
1 parent 54d9507 commit d0a4f97
Show file tree
Hide file tree
Showing 3 changed files with 341 additions and 213 deletions.
16 changes: 12 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub type IOBuffer = buffer::DataBuffer;
#[derive(Debug)]
pub enum Error {
IO(io::Error),
Write(writer::Error),
Parse(nom::ErrorKind<u32>),
Unknown,
}
Expand All @@ -46,6 +47,13 @@ impl From<Error> for io::Error {
}
}

impl From<writer::Error> for Error {
fn from(e: writer::Error) -> Error {
Error::Write(e)
}
}


/// Helper function that implements the actual importing routine.
fn import_impl<F>(file_path: &Path, parse: F) -> Result<model::Vtk, Error>
where
Expand Down Expand Up @@ -121,7 +129,7 @@ pub fn export(data: model::Vtk, file_path: &Path) -> Result<(), Error> {
use writer::WriteVtk;

let mut file = ::std::fs::File::create(file_path)?;
file.write_all(Vec::<u8>::new().write_vtk(data).as_slice())?;
file.write_all(Vec::<u8>::new().write_vtk(data)?.as_slice())?;
Ok(())
}

Expand All @@ -131,7 +139,7 @@ pub fn export_le(data: model::Vtk, file_path: &Path) -> Result<(), Error> {
use writer::WriteVtk;

let mut file = ::std::fs::File::create(file_path)?;
file.write_all(Vec::<u8>::new().write_vtk_le(data).as_slice())?;
file.write_all(Vec::<u8>::new().write_vtk_le(data)?.as_slice())?;
Ok(())
}

Expand All @@ -141,7 +149,7 @@ pub fn export_be(data: model::Vtk, file_path: &Path) -> Result<(), Error> {
use writer::WriteVtk;

let mut file = ::std::fs::File::create(file_path)?;
file.write_all(Vec::<u8>::new().write_vtk_be(data).as_slice())?;
file.write_all(Vec::<u8>::new().write_vtk_be(data)?.as_slice())?;
Ok(())
}

Expand Down Expand Up @@ -172,7 +180,7 @@ pub fn export_ascii(data: model::Vtk, file_path: &Path) -> Result<(), Error> {
use writer::WriteVtk;

let mut out_str = String::new();
out_str.write_vtk(data);
out_str.write_vtk(data)?;
{
let mut file = File::create(file_path)?;
file.write_all(out_str.as_bytes())?;
Expand Down
Loading

0 comments on commit d0a4f97

Please sign in to comment.