refactor: update error methods

This commit is contained in:
Kristofers Solo 2025-10-13 15:02:34 +03:00
parent e36ba664c6
commit 27b31d1fcc
Signed by: kristoferssolo
GPG Key ID: 8687F2D3EEE6F0ED
3 changed files with 39 additions and 54 deletions

View File

@ -1,14 +0,0 @@
pub fn add(left: u64, right: u64) -> u64 {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}

View File

@ -25,60 +25,73 @@ pub enum CryptoError {
impl CryptoError { impl CryptoError {
/// Creates a key size error /// Creates a key size error
pub fn invalid_key_size(expected: usize, actual: usize) -> Self { #[inline]
CryptoError::InvalidKeySize { expected, actual } #[must_use]
pub const fn invalid_key_size(expected: usize, actual: usize) -> Self {
Self::InvalidKeySize { expected, actual }
} }
/// Creates a block size error /// Creates a block size error
pub fn invalid_block_size(expected: usize, actual: usize) -> Self { #[inline]
CryptoError::InvalidBlockSize { expected, actual } #[must_use]
pub const fn invalid_block_size(expected: usize, actual: usize) -> Self {
Self::InvalidBlockSize { expected, actual }
} }
/// Creates an invalid padding error /// Creates an invalid padding error
pub fn invalid_padding() -> Self { #[inline]
CryptoError::InvalidPadding #[must_use]
pub const fn invalid_padding() -> Self {
Self::InvalidPadding
} }
/// Creates a plaintext length error /// Creates a plaintext length error
pub fn invalid_plaintext_length(actual: usize) -> Self { #[inline]
CryptoError::InvalidPlaintextLength { actual } #[must_use]
pub const fn invalid_plaintext_length(actual: usize) -> Self {
Self::InvalidPlaintextLength { actual }
} }
/// Returns true if this is a key size error /// Returns true if this is a key size error
pub fn is_key_error(&self) -> bool { #[inline]
matches!(self, CryptoError::InvalidKeySize { .. }) #[must_use]
pub const fn is_key_error(&self) -> bool {
matches!(self, Self::InvalidKeySize { .. })
} }
/// Returns true if this is a block size error /// Returns true if this is a block size error
pub fn is_block_error(&self) -> bool { #[inline]
matches!(self, CryptoError::InvalidBlockSize { .. }) #[must_use]
pub const fn is_block_error(&self) -> bool {
matches!(self, Self::InvalidBlockSize { .. })
} }
/// Returns true if this is a size-related error /// Returns true if this is a size-related error
pub fn is_size_error(&self) -> bool { #[must_use]
self.is_key_error() pub const fn is_size_error(&self) -> bool {
|| self.is_block_error() self.is_key_error() || self.is_block_error() || matches!(self, Self::InvalidSize { .. })
|| matches!(self, CryptoError::InvalidSize { .. })
} }
/// Returns the expected size for size-related errors /// Returns the expected size for size-related errors
pub fn expected_size(&self) -> Option<usize> { #[must_use]
pub const fn expected_size(&self) -> Option<usize> {
match self { match self {
CryptoError::InvalidKeySize { expected, .. } => Some(*expected), Self::InvalidKeySize { expected, .. }
CryptoError::InvalidBlockSize { expected, .. } => Some(*expected), | Self::InvalidBlockSize { expected, .. }
CryptoError::InvalidSize { expected, .. } => Some(*expected), | Self::InvalidSize { expected, .. } => Some(*expected),
_ => None, _ => None,
} }
} }
/// Returns the actual size for size-related errors /// Returns the actual size for size-related errors
pub fn actual_size(&self) -> Option<usize> { #[must_use]
pub const fn actual_size(&self) -> Option<usize> {
match self { match self {
CryptoError::InvalidKeySize { actual, .. } => Some(*actual), Self::InvalidKeySize { actual, .. }
CryptoError::InvalidBlockSize { actual, .. } => Some(*actual), | Self::InvalidBlockSize { actual, .. }
CryptoError::InvalidSize { actual, .. } => Some(*actual), | Self::InvalidSize { actual, .. }
CryptoError::InvalidPlaintextLength { actual } => Some(*actual), | Self::InvalidPlaintextLength { actual } => Some(*actual),
_ => None, Self::InvalidPadding => None,
} }
} }
} }

View File

@ -1,14 +0,0 @@
pub fn add(left: u64, right: u64) -> u64 {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}