scratchstack_arn/
error.rs1use std::{
2 error::Error,
3 fmt::{Debug, Display, Formatter, Result as FmtResult},
4};
5
6#[derive(Debug, PartialEq, Eq)]
8pub enum ArnError {
9 InvalidAccountId(String),
11
12 InvalidArn(String),
14
15 InvalidPartition(String),
17
18 InvalidRegion(String),
20
21 InvalidResource(String),
23
24 InvalidScheme(String),
26
27 InvalidService(String),
29}
30
31impl Error for ArnError {}
32
33impl Display for ArnError {
34 fn fmt(&self, f: &mut Formatter) -> FmtResult {
35 match self {
36 Self::InvalidAccountId(account_id) => write!(f, "Invalid account id: {account_id:#?}"),
37 Self::InvalidArn(arn) => write!(f, "Invalid ARN: {arn:#?}"),
38 Self::InvalidPartition(partition) => write!(f, "Invalid partition: {partition:#?}"),
39 Self::InvalidRegion(region) => write!(f, "Invalid region: {region:#?}"),
40 Self::InvalidResource(resource) => write!(f, "Invalid resource: {resource:#?}"),
41 Self::InvalidScheme(scheme) => write!(f, "Invalid scheme: {scheme:#?}"),
42 Self::InvalidService(service) => write!(f, "Invalid service name: {service:#?}"),
43 }
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use super::ArnError;
50
51 #[test]
52 fn check_derived() {
53 let errors = vec![
54 ArnError::InvalidAccountId("1234".to_string()),
55 ArnError::InvalidArn("arn:aws:iam::1234:role/role-name".to_string()),
56 ArnError::InvalidPartition("aws".to_string()),
57 ArnError::InvalidRegion("us-east-1".to_string()),
58 ArnError::InvalidResource("role/role-name".to_string()),
59 ArnError::InvalidScheme("arn".to_string()),
60 ArnError::InvalidService("iam".to_string()),
61 ];
62
63 for i in 0..errors.len() {
64 for j in 0..errors.len() {
65 if i == j {
66 assert_eq!(errors[i], errors[j]);
67 } else {
68 assert_ne!(errors[i], errors[j]);
69 }
70 }
71 }
72
73 let _ = format!("{:?}", errors[0]);
75 }
76
77 #[test]
78 fn check_resource() {
79 let err = ArnError::InvalidResource("".to_string());
81 assert_eq!(err.to_string().as_str(), "Invalid resource: \"\"");
82 }
83}
84