use std::boxed::Box;
use std::convert::Into;
use std::error;
use std::fmt;
use std::marker::{Send, Sync};
use rulinalg;
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
error: Box<error::Error + Send + Sync>,
}
#[derive(Debug)]
pub enum ErrorKind {
InvalidParameters,
InvalidData,
InvalidState,
UntrainedModel,
LinearAlgebra
}
impl Error {
pub fn new<E>(kind: ErrorKind, error: E) -> Error
where E: Into<Box<error::Error + Send + Sync>>
{
Error {
kind: kind,
error: error.into(),
}
}
pub fn new_untrained() -> Error {
Error::new(ErrorKind::UntrainedModel, "The model has not been trained.")
}
pub fn kind(&self) -> &ErrorKind {
&self.kind
}
}
impl From<rulinalg::error::Error> for Error {
fn from(e: rulinalg::error::Error) -> Error {
Error::new(ErrorKind::LinearAlgebra, <rulinalg::error::Error as error::Error>::description(&e))
}
}
impl error::Error for Error {
fn description(&self) -> &str {
self.error.description()
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.error.fmt(f)
}
}