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
use std::{
error::Error,
fmt::{Debug, Display, Formatter, Result as FmtResult},
};
#[derive(Debug, PartialEq, Eq)]
pub enum ArnError {
InvalidAccountId(String),
InvalidArn(String),
InvalidPartition(String),
InvalidRegion(String),
InvalidResource(String),
InvalidScheme(String),
InvalidService(String),
}
impl Error for ArnError {}
impl Display for ArnError {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
match self {
Self::InvalidAccountId(account_id) => write!(f, "Invalid account id: {:#?}", account_id),
Self::InvalidArn(arn) => write!(f, "Invalid ARN: {:#?}", arn),
Self::InvalidPartition(partition) => write!(f, "Invalid partition: {:#?}", partition),
Self::InvalidRegion(region) => write!(f, "Invalid region: {:#?}", region),
Self::InvalidResource(resource) => write!(f, "Invalid resource: {:#?}", resource),
Self::InvalidScheme(scheme) => write!(f, "Invalid scheme: {:#?}", scheme),
Self::InvalidService(service) => write!(f, "Invalid service name: {:#?}", service),
}
}
}
#[cfg(test)]
mod tests {
use super::ArnError;
#[test]
fn check_derived() {
let errors = vec![
ArnError::InvalidAccountId("1234".to_string()),
ArnError::InvalidArn("arn:aws:iam::1234:role/role-name".to_string()),
ArnError::InvalidPartition("aws".to_string()),
ArnError::InvalidRegion("us-east-1".to_string()),
ArnError::InvalidResource("role/role-name".to_string()),
ArnError::InvalidScheme("arn".to_string()),
ArnError::InvalidService("iam".to_string()),
];
for i in 0..errors.len() {
for j in 0..errors.len() {
if i == j {
assert_eq!(errors[i], errors[j]);
} else {
assert_ne!(errors[i], errors[j]);
}
}
}
let _ = format!("{:?}", errors[0]);
}
#[test]
fn check_resource() {
let err = ArnError::InvalidResource("".to_string());
assert_eq!(err.to_string().as_str(), "Invalid resource: \"\"");
}
}