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
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
use curl::Error as CurlError;
use std::io::Error as IOError;
use std::fmt::{Formatter,Debug,Display,Error as FormatterError};
use std::error::Error as TraitError;
use std::convert::From;

/// Errors during starting download manager or processing request.
pub enum Error<E> {
	/// Unable to start thread.
	ThreadStartError { error: IOError },

	/// Curl error - configuring or processing request.
	/// First parameter - curl error.
	Curl { error: CurlError },

	/// Error within event-loop.
	/// First parameter - description.
	/// Second parameter - debug message.
	EventLoop { description: String, debug_message: String },

	/// User defined error while Initializing
	Initialize { error: E },
}

impl <E> Debug for Error<E> {
	#[allow(unused)]
	fn fmt(&self, f: &mut Formatter) -> Result<(), FormatterError> {
		match self {
			&Error::ThreadStartError {ref error} => {
				write!(f, "Unable to start thread: {:?}", error)
			},
			&Error::Curl {ref error} => {
				write!(f, "Curl error: {:?}", error)
			},
			&Error::EventLoop {ref description,ref debug_message} => {
				write!(f, "Event loop error: {}", debug_message)
			},
			&Error::Initialize {..} => {
				write!(f, "Initialize error")
			},
		}
	}
}

impl <E> Display for Error<E> {
	fn fmt(&self, f: &mut Formatter) -> Result<(), FormatterError> {
		write!(f,"{:?}",self)
	}
}

impl <E>TraitError for Error<E> {
	#[allow(unused)]
	fn description(&self) -> &str {
		match self {
			&Error::ThreadStartError {..} => {
				"Unable to start thread"
			},
			&Error::Curl {..} => {
				"Curl error"
			},
			&Error::EventLoop {ref description,ref debug_message} => {
				&description
			},
			&Error::Initialize {..} => {
				"Initialize error"
			},
		}
	}

    fn cause(&self) -> Option<&TraitError> {
    	match self {
			&Error::ThreadStartError {ref error} => {
				Some(error)
			},
			&Error::Curl {ref error} => {
				Some(error)
			},
			&Error::EventLoop {..} => {
				None
			},
			&Error::Initialize {..} => {
				None
			},
		}
    }
}


impl <E>From<CurlError> for Error<E> {
	fn from(error: CurlError) -> Self {
		Error::Curl {
			error: error
		}
	}
}


// impl <E: !CurlError>From<E> for Error<E> where E: !CurlError {
// 	fn from(error: E) -> Self {
// 		Error::Initialize {
// 			error: error
// 		}
// 	}
// }