use std::error::{Error};
use std::fmt;
use clang_sys::*;
use libc::{c_longlong};
use utility::{FromError};
macro_rules! error {
(
$(#[$meta:meta])*
pub enum $name:ident: $underlying:ty {
$(#[$variantdoc:meta] $variant:ident = ($error:pat, $message:expr)), +,
}
) => {
$(#[$meta])*
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum $name {
$(#[$variantdoc] $variant), +
}
impl Error for $name { }
impl From<$name> for String {
fn from(error: $name) -> String {
error.to_string()
}
}
impl FromError<$underlying> for $name {
fn from_error(error: $underlying) -> Result<(), $name> {
match error {
$($error => Err($name::$variant)), +,
_ => Ok(()),
}
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
$($name::$variant => write!(f, $message)), +
}
}
}
};
}
error! {
pub enum AlignofError: c_longlong {
Dependent = (-3, "the type is a dependent type"),
Incomplete = (-2, "the type is an incomplete type"),
}
}
error! {
pub enum OffsetofError: c_longlong {
Dependent = (-3, "the record type is a dependent type"),
Incomplete = (-2, "the record type is an incomplete type"),
Name = (-5, "the record type does not contain a field with the supplied name"),
Parent = (-1, "the record type has an invalid parent declaration"),
Undeduced = (-6, "the type is undeduced"),
}
}
error! {
pub enum SaveError: CXSaveError {
Errors = (CXSaveError_InvalidTU, "errors in the translation unit prevented saving"),
Unknown = (CXSaveError_Unknown, "an unknown error occurred"),
}
}
error! {
pub enum SizeofError: c_longlong {
Dependent = (-3, "the type is a dependent type"),
Incomplete = (-2, "the type is an incomplete type"),
VariableSize = (-4, "the type is a variable size type"),
InvalidFieldName = (-5, "invalid field name"),
}
}
error! {
pub enum SourceError: CXErrorCode {
AstDeserialization = (CXError_ASTReadError, "AST deserialization failed"),
Crash = (CXError_Crashed, "`libclang` crashed"),
Unknown = (CXError_Failure, "an unknown error occurred"),
}
}