This is an automated email from the ASF dual-hosted git repository.

fokko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-rust.git


The following commit(s) were added to refs/heads/main by this push:
     new aa53409  feat: Use macro to define from error. (#17)
aa53409 is described below

commit aa53409c36c9943b16c2799aa48405d44e3d79ca
Author: Renjie Liu <[email protected]>
AuthorDate: Thu Aug 3 14:17:35 2023 +0800

    feat: Use macro to define from error. (#17)
    
    * feat: Use macro to define from error.
    
    * fix format
---
 crates/iceberg/src/error.rs | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/crates/iceberg/src/error.rs b/crates/iceberg/src/error.rs
index 87549a9..7f47244 100644
--- a/crates/iceberg/src/error.rs
+++ b/crates/iceberg/src/error.rs
@@ -243,12 +243,22 @@ impl Error {
     }
 }
 
-impl From<std::str::Utf8Error> for Error {
-    fn from(v: std::str::Utf8Error) -> Self {
-        Self::new(ErrorKind::Unexpected, "handling invalid utf-8 
characters").with_source(v)
-    }
+macro_rules! define_from_err {
+    ($source: path, $error_kind: path, $msg: expr) => {
+        impl std::convert::From<$source> for crate::error::Error {
+            fn from(v: $source) -> Self {
+                Self::new($error_kind, $msg).with_source(v)
+            }
+        }
+    };
 }
 
+define_from_err!(
+    std::str::Utf8Error,
+    ErrorKind::Unexpected,
+    "handling invalid utf-8 characters"
+);
+
 #[cfg(test)]
 mod tests {
     use anyhow::anyhow;

Reply via email to