Skip to main content

age_setup/errors/
mod.rs

1//! Error handling types.
2
3pub mod buildings;
4pub mod security;
5pub mod validation;
6
7pub use buildings::GenerationError;
8pub use security::SecurityError;
9pub use validation::ValidationError;
10
11/// Main error type for the crate.
12///
13/// Encompasses all possible errors during key generation, validation, or security operations.
14#[derive(Debug, thiserror::Error)]
15pub enum Error {
16    /// Error during key generation (e.g., internal library failure).
17    #[error("Key generation failed: {0}")]
18    Generation(#[from] GenerationError),
19    /// Error validating public key format.
20    #[error("Validation failed: {0}")]
21    Validation(#[from] ValidationError),
22    /// Error during security operations (e.g., memory wipe).
23    #[error("Security operation failed: {0}")]
24    Security(#[from] SecurityError),
25}
26
27/// Result type alias using `Error` from this crate.
28pub type Result<T> = std::result::Result<T, Error>;
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn test_error_from_generation() {
36        let gen_err = GenerationError::IdentityCreationFailed;
37        let err: Error = gen_err.into();
38        assert!(matches!(err, Error::Generation(_)));
39    }
40
41    #[test]
42    fn test_error_from_validation() {
43        let val_err = ValidationError::invalid_public_key("test");
44        let err: Error = val_err.into();
45        assert!(matches!(err, Error::Validation(_)));
46    }
47
48    #[test]
49    fn test_error_from_security() {
50        let sec_err = SecurityError::MemoryWipeFailed;
51        let err: Error = sec_err.into();
52        assert!(matches!(err, Error::Security(_)));
53    }
54
55    #[test]
56    fn test_error_display() {
57        let err = Error::Generation(GenerationError::IdentityCreationFailed);
58        assert_eq!(
59            format!("{}", err),
60            "Key generation failed: Age identity generation failed: internal library error"
61        );
62    }
63}