dcrypt_symmetric/error/
mod.rs

1//! Error handling for symmetric cryptographic operations
2//!
3//! This module provides a simplified error handling layer that uses the API error system
4//! and adds conversions for symmetric-specific error types.
5
6#![cfg_attr(not(feature = "std"), no_std)]
7
8// Re-export the primary API error system
9pub use dcrypt_api::error::{validate, Error, Result};
10pub use dcrypt_api::error::{ResultExt, SecureErrorHandling, ERROR_REGISTRY};
11
12// Import for conversions
13use dcrypt_algorithms::error::Error as PrimitiveError;
14
15// Helper functions to convert errors (instead of From impls which violate orphan rules)
16
17/// Convert a PrimitiveError to an API Error
18pub fn from_primitive_error(err: PrimitiveError) -> Error {
19    match err {
20        PrimitiveError::Authentication { algorithm } => Error::AuthenticationFailed {
21            context: algorithm,
22            #[cfg(feature = "std")]
23            message: "authentication tag verification failed".to_string(),
24        },
25        PrimitiveError::Other(msg) => Error::Other {
26            context: "primitive operation",
27            #[cfg(feature = "std")]
28            message: msg.to_string(),
29        },
30        _ => Error::Other {
31            context: "primitive operation",
32            #[cfg(feature = "std")]
33            message: format!("Primitive error: {}", err),
34        },
35    }
36}
37
38/// Convert an IO error to an API Error
39#[cfg(feature = "std")]
40pub fn from_io_error(err: std::io::Error) -> Error {
41    Error::Other {
42        context: "I/O operation",
43        message: err.to_string(),
44    }
45}
46
47// Extension trait to make conversions more ergonomic
48pub trait SymmetricResultExt<T> {
49    /// Convert a Result with PrimitiveError to a Result with API Error
50    fn map_primitive_err(self) -> Result<T>;
51
52    /// Convert a Result with IO Error to a Result with API Error
53    #[cfg(feature = "std")]
54    fn map_io_err(self) -> Result<T>;
55}
56
57impl<T> SymmetricResultExt<T> for core::result::Result<T, PrimitiveError> {
58    fn map_primitive_err(self) -> Result<T> {
59        self.map_err(from_primitive_error)
60    }
61
62    #[cfg(feature = "std")]
63    fn map_io_err(self) -> Result<T> {
64        // This implementation will never be called since PrimitiveError != std::io::Error
65        // But we need it to satisfy the trait
66        unreachable!("map_io_err called on PrimitiveError result")
67    }
68}
69
70#[cfg(feature = "std")]
71impl<T> SymmetricResultExt<T> for core::result::Result<T, std::io::Error> {
72    fn map_primitive_err(self) -> Result<T> {
73        // This implementation will never be called since std::io::Error != PrimitiveError
74        // But we need it to satisfy the trait
75        unreachable!("map_primitive_err called on std::io::Error result")
76    }
77
78    fn map_io_err(self) -> Result<T> {
79        self.map_err(from_io_error)
80    }
81}
82
83// Also implement for api::Error results (like SecretBytes operations)
84impl<T> SymmetricResultExt<T> for core::result::Result<T, dcrypt_api::error::Error> {
85    fn map_primitive_err(self) -> Result<T> {
86        // Already the right type, just pass through
87        self
88    }
89
90    #[cfg(feature = "std")]
91    fn map_io_err(self) -> Result<T> {
92        // Already the right type, just pass through
93        self
94    }
95}
96
97// Specialized result types for different operations
98pub type CipherResult<T> = Result<T>;
99pub type AeadResult<T> = Result<T>;
100pub type StreamResult<T> = Result<T>;
101
102// Helper functions for common validation patterns in symmetric crypto
103
104/// Validate stream state
105pub fn validate_stream_state(
106    condition: bool,
107    operation: &'static str,
108    details: &'static str,
109) -> Result<()> {
110    validate::parameter(condition, operation, details)
111}
112
113/// Validate format/serialization with consistent context
114pub fn validate_format(
115    condition: bool,
116    context: &'static str,
117    details: &'static str,
118) -> Result<()> {
119    validate::parameter(condition, context, details)
120}
121
122/// Validate key derivation parameters
123pub fn validate_key_derivation(
124    condition: bool,
125    algorithm: &'static str,
126    details: &'static str,
127) -> Result<()> {
128    validate::parameter(condition, algorithm, details)
129}