Switch to using thiserror

This commit is contained in:
Nick Fitzgerald
2021-09-28 13:09:24 -07:00
committed by Chris Fallin
parent b93304b327
commit 38da2cee3e
3 changed files with 68 additions and 38 deletions

View File

@@ -72,6 +72,7 @@ name = "isle"
version = "0.1.0"
dependencies = [
"log",
"thiserror",
]
[[package]]
@@ -99,12 +100,41 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "proc-macro2"
version = "1.0.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9f5105d4fdaab20335ca9565e106a5d9b82b6219b5ba735731124ac6711d23d"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "syn"
version = "1.0.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5239bc68e0fef57495900cfea4e8dc75596d9a319d7e16b1e0a440d24e6fe0a0"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "textwrap"
version = "0.11.0"
@@ -114,12 +144,38 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "thiserror"
version = "1.0.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "602eca064b2d83369e2b2f34b09c70b605402801927c65c11071ac911d299b88"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bad553cc2c78e8de258400763a647e80e6d1b31ee237275d756f6836d204494c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "vec_map"
version = "0.8.2"

View File

@@ -7,3 +7,4 @@ license = "Apache-2.0 WITH LLVM-exception"
[dependencies]
log = "0.4"
thiserror = "1.0.29"

View File

@@ -1,12 +1,18 @@
//! Error types.
use std::sync::Arc;
use crate::lexer::Pos;
use std::fmt;
/// Errors produced by ISLE.
#[derive(Clone, Debug)]
#[derive(thiserror::Error, Clone, Debug)]
pub enum Error {
/// An I/O error.
#[error(transparent)]
IoError(Arc<std::io::Error>),
/// The input ISLE source has an error.
#[error("{}:{}:{}: {}", .filename, .pos.line, .pos.col, .msg)]
CompileError {
/// The error message.
msg: String,
@@ -15,43 +21,10 @@ pub enum Error {
/// The position within the file that the error occurs at.
pos: Pos,
},
/// An error from elsewhere in the system.
SystemError {
/// The error message.
msg: String,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&Error::CompileError {
ref msg,
ref filename,
pos,
} => {
write!(f, "{}:{}:{}: error: {}", filename, pos.line, pos.col, msg)
}
&Error::SystemError { ref msg } => {
write!(f, "{}", msg)
}
}
}
}
impl std::error::Error for Error {}
impl std::convert::From<std::fmt::Error> for Error {
fn from(e: std::fmt::Error) -> Error {
Error::SystemError {
msg: format!("{}", e),
}
}
}
impl std::convert::From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Error {
Error::SystemError {
msg: format!("{}", e),
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::IoError(Arc::new(e))
}
}