Skip to content
Snippets Groups Projects
Commit be5f093c authored by Martin Larralde's avatar Martin Larralde
Browse files

Improve `lightmotif-io` error type with `Arc` for internal errors and support for `Clone`

parent 4f4bb3f3
No related branches found
No related tags found
No related merge requests found
use std::fmt::Display; use std::fmt::Display;
use std::fmt::Formatter; use std::fmt::Formatter;
use std::sync::Arc;
use nom::error::Error as NomError; use nom::error::Error as NomError;
#[derive(Debug)] #[derive(Clone, Debug)]
pub enum Error { pub enum Error {
InvalidData, InvalidData,
Io(std::io::Error), Io(Arc<std::io::Error>),
Nom(NomError<String>), Nom(Arc<NomError<String>>),
} }
impl From<lightmotif::err::InvalidData> for Error { impl From<lightmotif::err::InvalidData> for Error {
...@@ -18,13 +19,13 @@ impl From<lightmotif::err::InvalidData> for Error { ...@@ -18,13 +19,13 @@ impl From<lightmotif::err::InvalidData> for Error {
impl From<std::io::Error> for Error { impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self { fn from(error: std::io::Error) -> Self {
Error::Io(error) Error::Io(Arc::new(error))
} }
} }
impl From<NomError<&'_ str>> for Error { impl From<NomError<&'_ str>> for Error {
fn from(error: NomError<&'_ str>) -> Self { fn from(error: NomError<&'_ str>) -> Self {
Error::Nom(NomError::new(error.input.to_string(), error.code)) Error::Nom(Arc::new(NomError::new(error.input.to_string(), error.code)))
} }
} }
......
...@@ -96,7 +96,7 @@ impl<B: BufRead> Iterator for Reader<B> { ...@@ -96,7 +96,7 @@ impl<B: BufRead> Iterator for Reader<B> {
let text = match std::str::from_utf8(bytes) { let text = match std::str::from_utf8(bytes) {
Ok(text) => text, Ok(text) => text,
Err(_) => { Err(_) => {
return Some(Err(Error::Io(std::io::Error::new( return Some(Err(Error::from(std::io::Error::new(
std::io::ErrorKind::InvalidData, std::io::ErrorKind::InvalidData,
"decoding error", "decoding error",
)))); ))));
......
...@@ -94,7 +94,7 @@ impl<B: BufRead, A: Alphabet> Iterator for Reader<B, A> { ...@@ -94,7 +94,7 @@ impl<B: BufRead, A: Alphabet> Iterator for Reader<B, A> {
let text = match std::str::from_utf8(bytes) { let text = match std::str::from_utf8(bytes) {
Ok(text) => text, Ok(text) => text,
Err(_) => { Err(_) => {
return Some(Err(Error::Io(std::io::Error::new( return Some(Err(Error::from(std::io::Error::new(
std::io::ErrorKind::InvalidData, std::io::ErrorKind::InvalidData,
"decoding error", "decoding error",
)))); ))));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment