Skip to content

use https://crates.io/crates/quick-error/ #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
[package]
name = "twig"
version = "0.1.0"
authors = ["Rust Twig Team", "Nerijus Arlauskas <[email protected]>", "Colin Kiegel <[email protected]>"]
description = "Twig templating engine for Rust; work in progress."
documentation = ""
repository = "https://github.com/rust-web/twig"
authors = [
"Rust Twig Team",
"Nerijus Arlauskas <[email protected]>",
"Colin Kiegel <[email protected]>"
]
license = "BSD-3-Clause"
name = "twig"
repository = "https://github.com/rust-web/twig"
version = "0.1.0"

[dependencies]
quick-error = "0.*"
80 changes: 25 additions & 55 deletions src/engine/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,39 @@
//! Typisation of syntax errors.

use std::error::Error;
use std::fmt::{self, Display};
use std::convert::From;

pub type LoaderError = Box<Error>; // unimplemented!()
pub type ParserError = Box<Error>; // unimplemented!()
pub type LexerError = Box<Error>; // unimplemented!()
pub type ExtensionRegistryError = Box<Error>; // unimplemented!()


#[derive(Debug)]
pub enum TwigError {
Loader(LoaderError),
Lexer(LexerError),
Parser(ParserError),
ExtensionRegistry(ExtensionRegistryError),
}

impl From<LoaderError> for TwigError {
fn from(err: LoaderError) -> TwigError {
TwigError::Loader(err)
}
}

// Due to the `dummy` error impls we can not define different From-impls
//
// impl From<LexerError> for TwigError {
// fn from(err: LexerError) -> TwigError {
// TwigError::Lexer(err)
// }
// }
//
// impl From<ParserError> for TwigError {
// fn from(err: ParserError) -> TwigError {
// TwigError::Parser(err)
// }
// }
//
// impl From<ExtensionRegistryError> for TwigError {
// fn from(err: ExtensionRegistryError) -> TwigError {
// TwigError::ExtensionRegistry(err)
// }
// }

impl Error for TwigError {
fn description(&self) -> &str {
match *self {
TwigError::Loader(..) => "Twig loader error.",
TwigError::Lexer(..) => "Twig lexer error.",
TwigError::Parser(..) => "Twig parser error.",
TwigError::ExtensionRegistry(..) => "Twig extension registry error."
quick_error! {
#[derive(Debug)]
pub enum TwigError {
Loader(cause: LoaderError) {
description("Twig loader error")
display(me) -> ("{}: {}", me.description(), cause)
from()
cause(cause.as_ref())
}
}
}

impl Display for TwigError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{}", self.description()));

match *self {
TwigError::Loader(ref e) => Display::fmt(e,f),
TwigError::Lexer(ref e) => Display::fmt(e,f),
TwigError::Parser(ref e) => Display::fmt(e,f),
TwigError::ExtensionRegistry(ref e) => Display::fmt(e,f),
Lexer(cause: LexerError) {
description("Twig lexer error")
display(me) -> ("{}: {}", me.description(), cause)
//from()
cause(cause.as_ref())
}
Parser(cause: ParserError) {
description("Twig parser error")
display(me) -> ("{}: {}", me.description(), cause)
//from()
cause(cause.as_ref())
}
ExtensionRegistry(cause: ExtensionRegistryError) {
description("Twig extension registry error")
display(me) -> ("{}: {}", me.description(), cause)
//from()
cause(cause.as_ref())
}
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
//! [changelog]: https://github.com/rust-web/twig/blob/master/CHANGELOG.md
//! [twigphp]: http://twig.sensiolabs.org/documentation

#[macro_use] extern crate quick_error;

#[macro_use] pub mod api;
pub mod engine;

Expand Down