From be5f093c4e6445e89290b6a8e77df0357189f0ff Mon Sep 17 00:00:00 2001
From: Martin Larralde <martin.larralde@embl.de>
Date: Mon, 17 Jun 2024 19:06:47 +0200
Subject: [PATCH] Improve `lightmotif-io` error type with `Arc` for internal
 errors and support for `Clone`

---
 lightmotif-io/src/error.rs        | 11 ++++++-----
 lightmotif-io/src/jaspar/mod.rs   |  2 +-
 lightmotif-io/src/jaspar16/mod.rs |  2 +-
 3 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/lightmotif-io/src/error.rs b/lightmotif-io/src/error.rs
index bfd5a2f..38944ab 100644
--- a/lightmotif-io/src/error.rs
+++ b/lightmotif-io/src/error.rs
@@ -1,13 +1,14 @@
 use std::fmt::Display;
 use std::fmt::Formatter;
+use std::sync::Arc;
 
 use nom::error::Error as NomError;
 
-#[derive(Debug)]
+#[derive(Clone, Debug)]
 pub enum Error {
     InvalidData,
-    Io(std::io::Error),
-    Nom(NomError<String>),
+    Io(Arc<std::io::Error>),
+    Nom(Arc<NomError<String>>),
 }
 
 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 {
     fn from(error: std::io::Error) -> Self {
-        Error::Io(error)
+        Error::Io(Arc::new(error))
     }
 }
 
 impl From<NomError<&'_ str>> for Error {
     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)))
     }
 }
 
diff --git a/lightmotif-io/src/jaspar/mod.rs b/lightmotif-io/src/jaspar/mod.rs
index 5154cd9..4c69a67 100644
--- a/lightmotif-io/src/jaspar/mod.rs
+++ b/lightmotif-io/src/jaspar/mod.rs
@@ -96,7 +96,7 @@ impl<B: BufRead> Iterator for Reader<B> {
                 let text = match std::str::from_utf8(bytes) {
                     Ok(text) => text,
                     Err(_) => {
-                        return Some(Err(Error::Io(std::io::Error::new(
+                        return Some(Err(Error::from(std::io::Error::new(
                             std::io::ErrorKind::InvalidData,
                             "decoding error",
                         ))));
diff --git a/lightmotif-io/src/jaspar16/mod.rs b/lightmotif-io/src/jaspar16/mod.rs
index 702f42f..e1ec1d5 100644
--- a/lightmotif-io/src/jaspar16/mod.rs
+++ b/lightmotif-io/src/jaspar16/mod.rs
@@ -94,7 +94,7 @@ impl<B: BufRead, A: Alphabet> Iterator for Reader<B, A> {
                 let text = match std::str::from_utf8(bytes) {
                     Ok(text) => text,
                     Err(_) => {
-                        return Some(Err(Error::Io(std::io::Error::new(
+                        return Some(Err(Error::from(std::io::Error::new(
                             std::io::ErrorKind::InvalidData,
                             "decoding error",
                         ))));
-- 
GitLab