rusty_machine/learning/
error.rs1use std::boxed::Box;
4use std::convert::Into;
5use std::error;
6use std::fmt;
7use std::marker::{Send, Sync};
8
9use rulinalg;
10
11#[derive(Debug)]
13pub struct Error {
14 kind: ErrorKind,
15 error: Box<error::Error + Send + Sync>,
16}
17
18#[derive(Debug)]
23pub enum ErrorKind {
24 InvalidParameters,
26 InvalidData,
28 InvalidState,
30 UntrainedModel,
32 LinearAlgebra
34}
35
36impl Error {
37 pub fn new<E>(kind: ErrorKind, error: E) -> Error
39 where E: Into<Box<error::Error + Send + Sync>>
40 {
41 Error {
42 kind: kind,
43 error: error.into(),
44 }
45 }
46
47 pub fn new_untrained() -> Error {
51 Error::new(ErrorKind::UntrainedModel, "The model has not been trained.")
52 }
53
54 pub fn kind(&self) -> &ErrorKind {
56 &self.kind
57 }
58}
59
60impl From<rulinalg::error::Error> for Error {
61 fn from(e: rulinalg::error::Error) -> Error {
62 Error::new(ErrorKind::LinearAlgebra, <rulinalg::error::Error as error::Error>::description(&e))
63 }
64}
65
66impl error::Error for Error {
67 fn description(&self) -> &str {
68 self.error.description()
69 }
70}
71
72impl fmt::Display for Error {
73 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
74 self.error.fmt(f)
75 }
76}