dcrypt_symmetric/error/
mod.rs1#![cfg_attr(not(feature = "std"), no_std)]
7
8pub use dcrypt_api::error::{validate, Error, Result};
10pub use dcrypt_api::error::{ResultExt, SecureErrorHandling, ERROR_REGISTRY};
11
12use dcrypt_algorithms::error::Error as PrimitiveError;
14
15pub 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#[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
47pub trait SymmetricResultExt<T> {
49 fn map_primitive_err(self) -> Result<T>;
51
52 #[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 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 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
83impl<T> SymmetricResultExt<T> for core::result::Result<T, dcrypt_api::error::Error> {
85 fn map_primitive_err(self) -> Result<T> {
86 self
88 }
89
90 #[cfg(feature = "std")]
91 fn map_io_err(self) -> Result<T> {
92 self
94 }
95}
96
97pub type CipherResult<T> = Result<T>;
99pub type AeadResult<T> = Result<T>;
100pub type StreamResult<T> = Result<T>;
101
102pub 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
113pub fn validate_format(
115 condition: bool,
116 context: &'static str,
117 details: &'static str,
118) -> Result<()> {
119 validate::parameter(condition, context, details)
120}
121
122pub fn validate_key_derivation(
124 condition: bool,
125 algorithm: &'static str,
126 details: &'static str,
127) -> Result<()> {
128 validate::parameter(condition, algorithm, details)
129}