Skip to content

Commit 42b95a9

Browse files
committed
Move out CompileError to core as generic form
1 parent e8230ef commit 42b95a9

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎core/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ num-bigint = { version = "0.4.3", features = ["serde"] }
1717
num-complex = { version = "0.4.0", features = ["serde"] }
1818
serde = { version = "1.0.136", features = ["derive"] }
1919
static_assertions = "1.1.0"
20+
thiserror = "1.0"

‎core/src/error.rs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,32 @@ impl<T> BaseError<T> {
5858
BaseError::from(self)
5959
}
6060
}
61+
62+
#[derive(Debug, thiserror::Error)]
63+
pub struct CompileError<T> {
64+
pub body: BaseError<T>,
65+
pub statement: Option<String>,
66+
}
67+
68+
impl<T> std::ops::Deref for CompileError<T> {
69+
type Target = BaseError<T>;
70+
fn deref(&self) -> &Self::Target {
71+
&self.body
72+
}
73+
}
74+
75+
impl<T> std::fmt::Display for CompileError<T>
76+
where
77+
T: std::fmt::Display,
78+
{
79+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
80+
let loc = self.location;
81+
if let Some(ref stmt) = self.statement {
82+
// visualize the error when location and statement are provided
83+
loc.fmt_with(f, &self.error)?;
84+
write!(f, "\n{stmt}{arrow:>pad$}", pad = loc.column(), arrow = "^")
85+
} else {
86+
loc.fmt_with(f, &self.error)
87+
}
88+
}
89+
}

‎core/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ mod location;
77
mod mode;
88

99
pub use bytecode::*;
10-
pub use error::BaseError;
10+
pub use error::{BaseError, CompileError};
1111
pub use location::Location;
1212
pub use mode::Mode;

0 commit comments

Comments
 (0)