From ddf905b4db89b0408937caffe4f11626f2ef1034 Mon Sep 17 00:00:00 2001 From: tmontaigu Date: Tue, 17 Jan 2023 14:12:36 +0100 Subject: [PATCH] chore(rust): impl std::error::Error for CompilerError All types which are errors should impl the std::error::Error trait. So that for example they could be put inside a `Box`. --- compiler/lib/Bindings/Rust/src/compiler.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/compiler/lib/Bindings/Rust/src/compiler.rs b/compiler/lib/Bindings/Rust/src/compiler.rs index 275db6218..f9d7c1d66 100644 --- a/compiler/lib/Bindings/Rust/src/compiler.rs +++ b/compiler/lib/Bindings/Rust/src/compiler.rs @@ -4,10 +4,27 @@ use crate::mlir::ffi; use std::os::raw::c_char; use std::{ffi::CStr, path::Path}; -#[derive(Debug)] pub struct CompilerError(String); -/// Retreive buffer of the error message from a C struct. +// Manual implementation to use pretty formatting of line-breaks +// contained in the String. +impl std::fmt::Debug for CompilerError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { + writeln!(f, "CompilerError {{")?; + writeln!(f, "{:#}", self.0)?; + writeln!(f, "}}") + } +} + +impl std::fmt::Display for CompilerError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { + ::fmt(self, f) + } +} + +impl std::error::Error for CompilerError {} + +/// Retrieve buffer of the error message from a C struct. trait CStructErrorMsg { fn error_msg(&self) -> *const i8; }