use crate::{Type, Variant};
use crate::ty::CommonTypeInfo;
use core::fmt;
use std::error;
#[derive(Debug)]
pub enum Error {
WrongType {
wrong_ty: Type,
right_ty: Type,
},
IsDynamic,
IsStatic,
WrongArgsNum {
wrong_num: usize,
right_num: usize,
},
BorrowedValue,
UnsupportedOperation,
CantReborrow,
WrongVariant {
wrong_var: Variant,
right_var: Variant,
},
}
impl Error {
pub(crate) const fn wrong_type(wrong: Type, right: Type) -> Error {
Error::WrongType {
wrong_ty: wrong,
right_ty: right,
}
}
pub(crate) const fn wrong_args_num(wrong: usize, right: usize) -> Error {
Error::WrongArgsNum {
wrong_num: wrong,
right_num: right,
}
}
pub(crate) fn wrong_variant(wrong: &Variant, right: &Variant) -> Error {
Error::WrongVariant {
wrong_var: wrong.clone(),
right_var: right.clone(),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::WrongType { wrong_ty, right_ty } => write!(
f,
"Reflection Error: Expected type \"{}\", got type \"{}\"",
right_ty.name(),
wrong_ty.name()
),
Error::IsDynamic => write!(
f,
"Reflection Error: Attempted to call a dynamic function without a receiver to bind"
),
Error::IsStatic => write!(
f,
"Reflection Error: Attempted to call a static function with a receiver instance"
),
Error::WrongArgsNum { wrong_num, right_num } => write!(
f,
"Reflection Error: Expected {} arguments, got {}",
wrong_num,
right_num,
),
Error::BorrowedValue => write!(
f,
"Reflection Error: This operation requires an Owned Value, but the provided Value was Borrowed"
),
Error::UnsupportedOperation => write!(
f,
"Reflection Error: This operation is not supported by this object. Likely, the #[rebound] macro for this object had a `no_*` attribute"
),
Error::CantReborrow => write!(
f,
"Reflection Error: Attempted to get a mutable reference to an existing reference, or any reference to an existing mutable reference"
),
Error::WrongVariant { wrong_var, right_var } => write!(
f,
"Reflection Error: Expected enum variant \"{}\", got variant \"{}\"",
wrong_var.name(),
right_var.name()
),
}
}
}
impl error::Error for Error {}