byte_array_ops/
errors.rs

1//! This module encapsulates all errors that core can throw
2
3use core::error::Error;
4use core::fmt::{Display, Formatter};
5
6#[derive(Debug, PartialEq, Eq)]
7pub enum ByteArraySecurityError {
8    DataRemnanceRisk, //TODO make this show the address of the pointer
9}
10#[derive(Debug, PartialEq, Eq)]
11pub enum ByteArrayError {
12    InvalidHexChar(char),
13    InvalidBinaryChar(char),
14    EmptyInput,
15    SecurityError(ByteArraySecurityError),
16}
17
18impl Display for ByteArrayError {
19    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
20        match self {
21            ByteArrayError::InvalidHexChar(c) => write!(f, "Invalid hex character: '{}'", c),
22            ByteArrayError::InvalidBinaryChar(c) => write!(f, "Invalid binary character: '{}'", c),
23            ByteArrayError::EmptyInput => write!(f, "Invalid empty input string"),
24            ByteArrayError::SecurityError(sec_error) => {
25                let base_msg = "Operational error due to security risk";
26                match sec_error {
27                    ByteArraySecurityError::DataRemnanceRisk => {
28                        write!(f, "{base_msg}: Data Remnance Risk subverting secure wiping")
29                    }
30                }
31            }
32        }
33    }
34}
35
36impl From<ByteArraySecurityError> for ByteArrayError {
37    fn from(value: ByteArraySecurityError) -> Self {
38        ByteArrayError::SecurityError(value)
39    }
40}
41
42impl Error for ByteArrayError {}