Skip to main content

ldap_client_proto/
result_code.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3/// One row per RFC 4511 appendix A result code: the variant, its number and
4/// its RFC name. The enum, both conversions and `Display` come from the same rows.
5macro_rules! result_codes {
6    ($($variant:ident = $code:literal $name:literal,)*) => {
7        #[derive(Debug, Clone, Copy, PartialEq, Eq)]
8        #[non_exhaustive]
9        pub enum ResultCode {
10            $($variant,)*
11            Unknown(i32),
12        }
13
14        impl ResultCode {
15            pub fn from_i64(code: i64) -> Self {
16                let Ok(code) = i32::try_from(code) else {
17                    return Self::Unknown(i32::MAX);
18                };
19                match code {
20                    $($code => Self::$variant,)*
21                    n => Self::Unknown(n),
22                }
23            }
24
25            pub fn code(self) -> i32 {
26                match self {
27                    $(Self::$variant => $code,)*
28                    Self::Unknown(n) => n,
29                }
30            }
31
32            fn name(self) -> &'static str {
33                match self {
34                    $(Self::$variant => $name,)*
35                    Self::Unknown(_) => "unknown",
36                }
37            }
38        }
39    };
40}
41
42result_codes! {
43    Success = 0 "success",
44    OperationsError = 1 "operationsError",
45    ProtocolError = 2 "protocolError",
46    TimeLimitExceeded = 3 "timeLimitExceeded",
47    SizeLimitExceeded = 4 "sizeLimitExceeded",
48    CompareFalse = 5 "compareFalse",
49    CompareTrue = 6 "compareTrue",
50    AuthMethodNotSupported = 7 "authMethodNotSupported",
51    StrongerAuthRequired = 8 "strongerAuthRequired",
52    Referral = 10 "referral",
53    AdminLimitExceeded = 11 "adminLimitExceeded",
54    UnavailableCriticalExtension = 12 "unavailableCriticalExtension",
55    ConfidentialityRequired = 13 "confidentialityRequired",
56    SaslBindInProgress = 14 "saslBindInProgress",
57    NoSuchAttribute = 16 "noSuchAttribute",
58    UndefinedAttributeType = 17 "undefinedAttributeType",
59    InappropriateMatching = 18 "inappropriateMatching",
60    ConstraintViolation = 19 "constraintViolation",
61    AttributeOrValueExists = 20 "attributeOrValueExists",
62    InvalidAttributeSyntax = 21 "invalidAttributeSyntax",
63    NoSuchObject = 32 "noSuchObject",
64    AliasProblem = 33 "aliasProblem",
65    InvalidDnSyntax = 34 "invalidDNSyntax",
66    AliasDereferencingProblem = 36 "aliasDereferencingProblem",
67    InappropriateAuthentication = 48 "inappropriateAuthentication",
68    InvalidCredentials = 49 "invalidCredentials",
69    InsufficientAccessRights = 50 "insufficientAccessRights",
70    Busy = 51 "busy",
71    Unavailable = 52 "unavailable",
72    UnwillingToPerform = 53 "unwillingToPerform",
73    LoopDetect = 54 "loopDetect",
74    NamingViolation = 64 "namingViolation",
75    ObjectClassViolation = 65 "objectClassViolation",
76    NotAllowedOnNonLeaf = 66 "notAllowedOnNonLeaf",
77    NotAllowedOnRdn = 67 "notAllowedOnRDN",
78    EntryAlreadyExists = 68 "entryAlreadyExists",
79    ObjectClassModsProhibited = 69 "objectClassModsProhibited",
80    AffectsMultipleDsas = 71 "affectsMultipleDSAs",
81    Other = 80 "other",
82}
83
84impl ResultCode {
85    pub fn is_success(&self) -> bool {
86        matches!(self, Self::Success | Self::CompareFalse | Self::CompareTrue)
87    }
88
89    pub fn is_credential_error(&self) -> bool {
90        matches!(
91            self,
92            Self::InvalidCredentials
93                | Self::InappropriateAuthentication
94                | Self::InsufficientAccessRights
95                | Self::AuthMethodNotSupported
96                | Self::StrongerAuthRequired
97        )
98    }
99
100    pub fn is_transient(&self) -> bool {
101        matches!(
102            self,
103            Self::Busy | Self::Unavailable | Self::AdminLimitExceeded | Self::Other
104        )
105    }
106
107    pub fn is_referral(&self) -> bool {
108        matches!(self, Self::Referral)
109    }
110
111    pub fn is_configuration_error(&self) -> bool {
112        matches!(
113            self,
114            Self::InvalidDnSyntax
115                | Self::NoSuchObject
116                | Self::UndefinedAttributeType
117                | Self::InappropriateMatching
118        )
119    }
120}
121
122impl std::fmt::Display for ResultCode {
123    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
124        write!(f, "{} ({})", self.name(), self.code())
125    }
126}