engram_rs/
error.rs

1use std::io;
2use thiserror::Error;
3
4/// Result type for engram operations
5pub type Result<T> = std::result::Result<T, EngramError>;
6
7/// Unified error type for all engram operations
8#[derive(Debug, Error)]
9pub enum EngramError {
10    // Archive errors
11    #[error("Invalid archive format: {0}")]
12    InvalidFormat(String),
13
14    #[error("Invalid magic number in archive header")]
15    InvalidMagic,
16
17    #[error("Unsupported archive version: {0}")]
18    UnsupportedVersion(u16),
19
20    #[error("File not found in archive: {0}")]
21    FileNotFound(String),
22
23    #[error("Invalid compression method: {0}")]
24    InvalidCompression(u8),
25
26    #[error("Compression failed: {0}")]
27    CompressionFailed(String),
28
29    #[error("Decompression failed: {0}")]
30    DecompressionFailed(String),
31
32    #[error("CRC mismatch: expected {expected:08x}, got {actual:08x}")]
33    CrcMismatch { expected: u32, actual: u32 },
34
35    // VFS errors
36    #[error("Database not found in archive: {0}")]
37    DatabaseNotFound(String),
38
39    #[error("Failed to extract database: {0}")]
40    ExtractionFailed(String),
41
42    #[error("SQLite error: {0}")]
43    SqliteError(#[from] rusqlite::Error),
44
45    // Manifest errors
46    #[error("Invalid manifest: {0}")]
47    InvalidManifest(String),
48
49    #[error("Manifest not found in archive")]
50    ManifestNotFound,
51
52    #[error("Failed to parse manifest: {0}")]
53    ManifestParseFailed(String),
54
55    #[error("Permission denied: {0}")]
56    PermissionDenied(String),
57
58    #[error("Invalid capability: {0}")]
59    InvalidCapability(String),
60
61    // Signature errors
62    #[error("Signature verification failed: {0}")]
63    SignatureVerificationFailed(String),
64
65    #[error("Invalid signature format")]
66    InvalidSignature,
67
68    #[error("Signature not found")]
69    SignatureNotFound,
70
71    #[error("Invalid public key")]
72    InvalidPublicKey,
73
74    #[error("Invalid secret key")]
75    InvalidSecretKey,
76
77    // Encryption errors
78    #[error("Encryption failed")]
79    EncryptionFailed,
80
81    #[error("Decryption failed")]
82    DecryptionFailed,
83
84    #[error("Missing decryption key for encrypted archive")]
85    MissingDecryptionKey,
86
87    #[error("Invalid encryption mode for this operation")]
88    InvalidEncryptionMode,
89
90    #[error("Invalid nonce size or format")]
91    InvalidNonce,
92
93    // I/O errors
94    #[error("I/O error: {0}")]
95    Io(#[from] io::Error),
96
97    #[error("Path error: {0}")]
98    PathError(String),
99
100    // Serialization errors
101    #[error("JSON error: {0}")]
102    JsonError(#[from] serde_json::Error),
103
104    #[error("TOML error: {0}")]
105    TomlError(String),
106
107    // General errors
108    #[error("Internal error: {0}")]
109    Internal(String),
110
111    #[error("{0}")]
112    Other(String),
113}
114
115impl From<toml::de::Error> for EngramError {
116    fn from(err: toml::de::Error) -> Self {
117        EngramError::TomlError(err.to_string())
118    }
119}
120
121impl From<toml::ser::Error> for EngramError {
122    fn from(err: toml::ser::Error) -> Self {
123        EngramError::TomlError(err.to_string())
124    }
125}
126
127impl From<ed25519_dalek::SignatureError> for EngramError {
128    fn from(err: ed25519_dalek::SignatureError) -> Self {
129        EngramError::SignatureVerificationFailed(err.to_string())
130    }
131}