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
use std::error::Error as ErrorTrait;
use std::{fmt, result};
pub type Result<T> = result::Result<T, Error>;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Error {
NotAlphaNumeric(String),
Length(String, usize, usize),
NotNumeric(String),
NotLuhn10(String),
InvalidAmount,
CardExists,
ProjectDoesNotExist,
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::NotAlphaNumeric(ref s) => write!(fmt, "{} should be alphanumeric \
and contain only underscores or dashes.", s),
Error::Length(ref s, min, max) => write!(fmt, "{} must be between {} and {} characters.", s, min, max),
Error::NotNumeric(ref s) => write!(fmt, "{} must be numeric.", s),
Error::NotLuhn10(ref s) => write!(fmt, "{} failed the Luhn-10 test.", s),
Error::InvalidAmount => write!(fmt, "{}", self.description()),
Error::ProjectDoesNotExist => write!(fmt, "{}", self.description()),
Error::CardExists => write!(fmt, "{}", self.description()),
}
}
}
impl ErrorTrait for Error {
fn description(&self) -> &str {
match *self {
Error::NotAlphaNumeric(_) => "Argument should be alphanumeric \
and contain only underscores or dashes.",
Error::Length(..) => "Argument length was not within the desired bounds.",
Error::NotNumeric(_) => "Argument must be numeric.",
Error::NotLuhn10(_) => "Argument failed the Luhn-10 test.",
Error::InvalidAmount => "Amounts must be greater than 0 dollars.",
Error::ProjectDoesNotExist => "The project you are looking for does not exist. Go make it!",
Error::CardExists => "The credit card number has already been used to back this project.",
}
}
fn cause(&self) -> Option<&ErrorTrait> {
None
}
}