use super::types::{Error, Result};
pub fn parameter<T>(condition: bool, context: &'static str, reason: &'static str) -> Result<T>
where
T: Default,
{
if !condition {
return Err(Error::InvalidParameter {
context,
#[cfg(feature = "std")]
message: reason.to_string(),
});
}
Ok(T::default())
}
pub fn check_parameter(condition: bool, context: &'static str, reason: &'static str) -> Result<()> {
if !condition {
return Err(Error::InvalidParameter {
context,
#[cfg(feature = "std")]
message: reason.to_string(),
});
}
Ok(())
}
pub fn length(context: &'static str, actual: usize, expected: usize) -> Result<()> {
if actual != expected {
return Err(Error::InvalidLength {
context,
expected,
actual,
});
}
Ok(())
}
pub fn min_length(context: &'static str, actual: usize, min: usize) -> Result<()> {
if actual < min {
return Err(Error::InvalidLength {
context,
expected: min,
actual,
});
}
Ok(())
}
pub fn max_length(context: &'static str, actual: usize, max: usize) -> Result<()> {
if actual > max {
return Err(Error::InvalidLength {
context,
expected: max,
actual,
});
}
Ok(())
}
pub fn range_length(context: &'static str, actual: usize, min: usize, max: usize) -> Result<()> {
if actual < min || actual > max {
return Err(Error::InvalidParameter {
context,
#[cfg(feature = "std")]
message: format!("length must be between {} and {}", min, max),
});
}
Ok(())
}
pub fn authentication(is_valid: bool, context: &'static str) -> Result<()> {
if !is_valid {
return Err(Error::AuthenticationFailed {
context,
#[cfg(feature = "std")]
message: "authentication failed".to_string(),
});
}
Ok(())
}
pub fn key(is_valid: bool, context: &'static str, reason: &'static str) -> Result<()> {
if !is_valid {
return Err(Error::InvalidKey {
context,
#[cfg(feature = "std")]
message: reason.to_string(),
});
}
Ok(())
}
pub fn signature(is_valid: bool, context: &'static str, reason: &'static str) -> Result<()> {
if !is_valid {
return Err(Error::InvalidSignature {
context,
#[cfg(feature = "std")]
message: reason.to_string(),
});
}
Ok(())
}
pub fn ciphertext(is_valid: bool, context: &'static str, reason: &'static str) -> Result<()> {
if !is_valid {
return Err(Error::InvalidCiphertext {
context,
#[cfg(feature = "std")]
message: reason.to_string(),
});
}
Ok(())
}
pub fn not_implemented(feature: &'static str) -> Error {
Error::NotImplemented { feature }
}