Add sv-parser-error
This commit is contained in:
parent
e1d80ab007
commit
6428d5ae1a
@ -1,6 +1,7 @@
|
||||
[workspace]
|
||||
members = [
|
||||
"sv-parser",
|
||||
"sv-parser-error",
|
||||
"sv-parser-macros",
|
||||
"sv-parser-parser",
|
||||
"sv-parser-pp",
|
||||
|
10
sv-parser-error/Cargo.toml
Normal file
10
sv-parser-error/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "sv-parser-error"
|
||||
version = "0.1.0"
|
||||
authors = ["dalance <dalance@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
failure = "*"
|
71
sv-parser-error/src/lib.rs
Normal file
71
sv-parser-error/src/lib.rs
Normal file
@ -0,0 +1,71 @@
|
||||
use failure::{Backtrace, Context, Fail};
|
||||
use std::fmt;
|
||||
use std::fmt::Display;
|
||||
use std::io::Error as IOError;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#[derive(Fail, Debug)]
|
||||
pub enum ErrorKind {
|
||||
#[fail(display = "IO error")]
|
||||
Io,
|
||||
#[fail(display = "Parse error")]
|
||||
Parse,
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Error {
|
||||
inner: Context<ErrorKind>,
|
||||
}
|
||||
|
||||
impl Fail for Error {
|
||||
fn cause(&self) -> Option<&dyn Fail> {
|
||||
self.inner.cause()
|
||||
}
|
||||
|
||||
fn backtrace(&self) -> Option<&Backtrace> {
|
||||
self.inner.backtrace()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
Display::fmt(&self.inner, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn new(inner: Context<ErrorKind>) -> Error {
|
||||
Error { inner }
|
||||
}
|
||||
|
||||
pub fn kind(&self) -> &ErrorKind {
|
||||
self.inner.get_context()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ErrorKind> for Error {
|
||||
fn from(kind: ErrorKind) -> Error {
|
||||
Error {
|
||||
inner: Context::new(kind),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Context<ErrorKind>> for Error {
|
||||
fn from(inner: Context<ErrorKind>) -> Error {
|
||||
Error { inner }
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
impl From<IOError> for Error {
|
||||
fn from(error: IOError) -> Error {
|
||||
Error {
|
||||
inner: error.context(ErrorKind::Io),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user