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<dyn std::error::Error>`.
This commit is contained in:
tmontaigu
2023-01-17 14:12:36 +01:00
parent 95d49f4657
commit ddf905b4db

View File

@@ -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> {
<Self as std::fmt::Debug>::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;
}