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