Skip to main content

webgates_core/
verification_result.rs

1//! Verification result values for core authentication flows.
2//!
3//! This module is intentionally small. It gives you a simple success-or-
4//! unauthorized outcome for credential verification while keeping infrastructural
5//! failures separate in your `Result` error type.
6
7/// Result of a credential or secret verification step.
8///
9/// Use this enum when verification has two logical outcomes:
10///
11/// - the supplied secret is valid
12/// - the supplied secret is not authorized
13///
14/// Infrastructure failures such as storage outages or backend errors should be
15/// reported separately in your surrounding `Result` type.
16///
17/// # Conversions
18/// - `VerificationResult::from(true)` returns [`VerificationResult::Ok`]
19/// - `VerificationResult::from(false)` returns [`VerificationResult::Unauthorized`]
20/// - `bool::from(VerificationResult::Ok)` returns `true`
21/// - `bool::from(VerificationResult::Unauthorized)` returns `false`
22///
23/// # Example
24/// ```
25/// use webgates_core::verification_result::VerificationResult;
26///
27/// let result = VerificationResult::from(true);
28///
29/// assert_eq!(result, VerificationResult::Ok);
30/// assert!(bool::from(result));
31/// ```
32#[derive(Eq, PartialEq, Debug, Clone, Copy)]
33pub enum VerificationResult {
34    /// Verification succeeded.
35    Ok,
36    /// Verification failed with an unauthorized outcome.
37    Unauthorized,
38}
39
40impl From<bool> for VerificationResult {
41    fn from(value: bool) -> Self {
42        if value { Self::Ok } else { Self::Unauthorized }
43    }
44}
45
46impl From<VerificationResult> for bool {
47    fn from(result: VerificationResult) -> Self {
48        matches!(result, VerificationResult::Ok)
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::VerificationResult;
55
56    #[test]
57    fn converts_from_bool() {
58        assert_eq!(VerificationResult::from(true), VerificationResult::Ok);
59        assert_eq!(
60            VerificationResult::from(false),
61            VerificationResult::Unauthorized
62        );
63    }
64
65    #[test]
66    fn converts_to_bool() {
67        assert!(bool::from(VerificationResult::Ok));
68        assert!(!bool::from(VerificationResult::Unauthorized));
69    }
70
71    #[test]
72    fn variants_remain_distinct() {
73        assert_ne!(VerificationResult::Ok, VerificationResult::Unauthorized);
74    }
75}