1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! TaskQueue error.

use std::error::Error;
use std::fmt:: { Display, Formatter };
use std::fmt;
use std::option::Option;
use std::io;

#[derive(Debug)]
pub enum TaskQueueError {
    IllegalStartThreads {
        min: usize,
        max: usize
    },
    Io(io::Error),
    IllegalPolicyThreads {
        min:usize,
        max:usize,
        count:usize
    }
}

impl TaskQueueError {
    pub fn illegal_start_threads(min: usize, max: usize) -> TaskQueueError {
        TaskQueueError::IllegalStartThreads {
            min,
            max
        }
    }

    pub fn illegal_policy_threads(min: usize, max: usize, count: usize) -> TaskQueueError {
        TaskQueueError::IllegalPolicyThreads {
            min,
            max,
            count
        }
    }
}

impl Error for TaskQueueError {
    fn description(&self) -> &str {
        match self {
            &TaskQueueError::IllegalStartThreads { .. } => "Illegal number of threads was received",
            &TaskQueueError::Io(ref e) => e.description(),
            &TaskQueueError::IllegalPolicyThreads { .. }  => "Policy was returned illegal number of threads",

        }
    }

    fn cause(&self) -> Option<&Error> {
        match self {
            &TaskQueueError::IllegalStartThreads { .. } => None,
            &TaskQueueError::Io(ref e) => Some(e),
            &TaskQueueError::IllegalPolicyThreads { .. } => None,
        }
    }
}

impl Display for TaskQueueError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            &TaskQueueError::IllegalStartThreads { min, max } => write!(f, "Illegal number of threads was received min:{} max:{}", min, max),
            &TaskQueueError::Io(ref err) => write!(f, "IO error: {}", err),
            &TaskQueueError::IllegalPolicyThreads { min, max, count } => write!(f, "Policy returned illegal number of threads min:{} max:{} count:{}", min, max, count)
        }
    }
}

impl From<io::Error> for TaskQueueError {
    fn from(err: io::Error) -> TaskQueueError {
        TaskQueueError::Io(err)
    }
}