scratchstack_arn/
error.rs

1use std::{
2    error::Error,
3    fmt::{Debug, Display, Formatter, Result as FmtResult},
4};
5
6/// Errors that can be raise during the parsing of ARNs.
7#[derive(Debug, PartialEq, Eq)]
8pub enum ArnError {
9    /// Invalid AWS account id. The argument contains the specified account id.
10    InvalidAccountId(String),
11
12    /// Invalid or malformed ARN. The argument contains the specified ARN.
13    InvalidArn(String),
14
15    /// Invalid partition. The argument contains the specified partition.
16    InvalidPartition(String),
17
18    /// Invalid region. The argument contains the specified region.
19    InvalidRegion(String),
20
21    /// Invalid resource. The argument contains the specified resource.
22    InvalidResource(String),
23
24    /// Invalid scheme. The scheme must be `arn`. The argument contains the specified scheme.
25    InvalidScheme(String),
26
27    /// Invalid service. The argument contains the specified service.
28    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        // Ensure we can debug print the error.
74        let _ = format!("{:?}", errors[0]);
75    }
76
77    #[test]
78    fn check_resource() {
79        // We do not construct InvalidResource currently.
80        let err = ArnError::InvalidResource("".to_string());
81        assert_eq!(err.to_string().as_str(), "Invalid resource: \"\"");
82    }
83}
84// end tests -- do not delete; needed for coverage.