Skip to main content

dcrypt_api/error/
validate.rs

1//! Validation utilities for cryptographic operations
2
3use super::types::{Error, Result};
4
5/// Validate a parameter condition
6pub fn parameter<T>(condition: bool, context: &'static str, reason: &'static str) -> Result<T>
7where
8    T: Default,
9{
10    #[cfg(not(feature = "std"))]
11    let _ = reason;
12
13    if !condition {
14        return Err(Error::InvalidParameter {
15            context,
16            #[cfg(feature = "std")]
17            message: reason.to_string(),
18        });
19    }
20    Ok(T::default())
21}
22
23/// Just check a parameter condition
24pub fn check_parameter(condition: bool, context: &'static str, reason: &'static str) -> Result<()> {
25    #[cfg(not(feature = "std"))]
26    let _ = reason;
27
28    if !condition {
29        return Err(Error::InvalidParameter {
30            context,
31            #[cfg(feature = "std")]
32            message: reason.to_string(),
33        });
34    }
35    Ok(())
36}
37
38/// Validate an exact length
39pub fn length(context: &'static str, actual: usize, expected: usize) -> Result<()> {
40    if actual != expected {
41        return Err(Error::InvalidLength {
42            context,
43            expected,
44            actual,
45        });
46    }
47    Ok(())
48}
49
50/// Validate a minimum length
51pub fn min_length(context: &'static str, actual: usize, min: usize) -> Result<()> {
52    if actual < min {
53        return Err(Error::InvalidLength {
54            context,
55            expected: min,
56            actual,
57        });
58    }
59    Ok(())
60}
61
62/// Validate a maximum length
63pub fn max_length(context: &'static str, actual: usize, max: usize) -> Result<()> {
64    if actual > max {
65        return Err(Error::InvalidLength {
66            context,
67            expected: max,
68            actual,
69        });
70    }
71    Ok(())
72}
73
74/// Validate length is within range (inclusive)
75pub fn range_length(context: &'static str, actual: usize, min: usize, max: usize) -> Result<()> {
76    if actual < min || actual > max {
77        return Err(Error::InvalidParameter {
78            context,
79            #[cfg(feature = "std")]
80            message: format!("length must be between {} and {}", min, max),
81        });
82    }
83    Ok(())
84}
85
86/// Validate authentication result
87pub fn authentication(is_valid: bool, context: &'static str) -> Result<()> {
88    if !is_valid {
89        return Err(Error::AuthenticationFailed {
90            context,
91            #[cfg(feature = "std")]
92            message: "authentication failed".to_string(),
93        });
94    }
95    Ok(())
96}
97
98/// Validate key format or content
99pub fn key(is_valid: bool, context: &'static str, reason: &'static str) -> Result<()> {
100    #[cfg(not(feature = "std"))]
101    let _ = reason;
102
103    if !is_valid {
104        return Err(Error::InvalidKey {
105            context,
106            #[cfg(feature = "std")]
107            message: reason.to_string(),
108        });
109    }
110    Ok(())
111}
112
113/// Validate signature format or content
114pub fn signature(is_valid: bool, context: &'static str, reason: &'static str) -> Result<()> {
115    #[cfg(not(feature = "std"))]
116    let _ = reason;
117
118    if !is_valid {
119        return Err(Error::InvalidSignature {
120            context,
121            #[cfg(feature = "std")]
122            message: reason.to_string(),
123        });
124    }
125    Ok(())
126}
127
128/// Validate ciphertext format or content
129pub fn ciphertext(is_valid: bool, context: &'static str, reason: &'static str) -> Result<()> {
130    #[cfg(not(feature = "std"))]
131    let _ = reason;
132
133    if !is_valid {
134        return Err(Error::InvalidCiphertext {
135            context,
136            #[cfg(feature = "std")]
137            message: reason.to_string(),
138        });
139    }
140    Ok(())
141}
142
143/// Create a not implemented error
144pub fn not_implemented(feature: &'static str) -> Error {
145    Error::NotImplemented { feature }
146}