Skip to content

Commit

Permalink
feat: implement Display trait for IrError
Browse files Browse the repository at this point in the history
  • Loading branch information
nfejzic committed Nov 17, 2021
1 parent 1db1944 commit 3ff2c77
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/middleend/ir_error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
use std::fmt;

#[derive(Debug)]
pub struct IrError {
pub tablename: String,
pub column: String,
pub message: String,
}

impl fmt::Display for IrError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("Error in communication with IR."))?;

f.write_fmt(format_args!(
"\nError occured in column {} of table {}.",
self.column, self.tablename
))?;

let prefix = "Message: ";

let msg: String = self
.message
.lines()
.enumerate()
.map(|(i, line)| {
if i > 0 {
" ".repeat(prefix.len()) + line
} else {
String::from(line)
}
})
.collect();

f.write_fmt(format_args!("{}{}", prefix, msg))?;

Ok(())
}
}

0 comments on commit 3ff2c77

Please sign in to comment.