Skip to content
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
74 changes: 74 additions & 0 deletions src/engine/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// This file is part of rust-web/twig
//
// For the copyright and license information, please view the LICENSE
// file that was distributed with this source code.

//! 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."
}
}
}

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),
}
}
}
11 changes: 11 additions & 0 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This file is part of rust-web/twig
//
// For the copyright and license information, please view the LICENSE
// file that was distributed with this source code.

//! The Twig Engine.

pub mod error;
pub use self::error::TwigError;

pub struct Engine; // unimplemented!()
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@
//! [twigphp]: http://twig.sensiolabs.org/documentation

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

pub use self::engine::Engine;