Skip to main content

hermes_tdata/
error.rs

1//! Error types for hermes-tdata
2
3use std::path::PathBuf;
4
5/// Result type alias for hermes-tdata operations
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors that can occur during tdata parsing
9#[derive(Debug, thiserror::Error)]
10pub enum Error {
11    /// IO error while reading tdata files
12    #[error("IO error: {0}")]
13    Io(#[from] std::io::Error),
14
15    /// The tdata folder path does not exist
16    #[error("tdata folder not found: {path}")]
17    FolderNotFound {
18        /// Path that was not found
19        path: PathBuf,
20    },
21
22    /// Required file is missing from tdata folder
23    #[error("required file not found: {file} in {folder}")]
24    FileNotFound {
25        /// Name of the missing file
26        file: String,
27        /// Folder where file was expected
28        folder: PathBuf,
29    },
30
31    /// Key file (`key_data`, `key_datas`) not found
32    #[error("key file not found in tdata folder")]
33    KeyFileNotFound,
34
35    /// Failed to decrypt tdata - wrong passcode or corrupted data
36    #[error("decryption failed: wrong passcode or corrupted data")]
37    DecryptionFailed,
38
39    /// The tdata folder is password-protected but no passcode provided
40    #[error("tdata is password-protected, passcode required")]
41    PasscodeRequired,
42
43    /// Invalid passcode provided
44    #[error("invalid passcode")]
45    InvalidPasscode,
46
47    /// `QDataStream` parsing error
48    #[error("QDataStream parse error: {message}")]
49    QDataStreamError {
50        /// Error message describing the parse failure
51        message: String,
52    },
53
54    /// Unexpected end of data while parsing
55    #[error("unexpected end of data at offset {offset}")]
56    UnexpectedEof {
57        /// Byte offset where EOF was encountered
58        offset: u64,
59    },
60
61    /// Invalid UTF-16 string data
62    #[error("invalid UTF-16 string data")]
63    InvalidUtf16,
64
65    /// Invalid data format or structure
66    #[error("invalid data format: {message}")]
67    InvalidFormat {
68        /// Description of the format violation
69        message: String,
70    },
71
72    /// No accounts found in tdata
73    #[error("no accounts found in tdata")]
74    NoAccounts,
75
76    /// Account index out of range
77    #[error("account index {index} out of range (max: {max})")]
78    AccountIndexOutOfRange {
79        /// Requested account index
80        index: usize,
81        /// Maximum valid index
82        max: usize,
83    },
84
85    /// MD5 checksum mismatch in encrypted data
86    #[error("checksum mismatch: data may be corrupted")]
87    ChecksumMismatch,
88
89    /// Unsupported tdata version
90    #[error("unsupported tdata version: {version}")]
91    UnsupportedVersion {
92        /// The unsupported version number
93        version: u32,
94    },
95
96    /// Auth key extraction failed
97    #[error("failed to extract auth key: {reason}")]
98    AuthKeyExtractionFailed {
99        /// Reason for extraction failure
100        reason: String,
101    },
102}
103
104impl Error {
105    /// Create a `QDataStream` error with a message
106    pub fn qdatastream(msg: impl Into<String>) -> Self {
107        Self::QDataStreamError { message: msg.into() }
108    }
109
110    /// Create an invalid format error with a message
111    pub fn invalid_format(msg: impl Into<String>) -> Self {
112        Self::InvalidFormat { message: msg.into() }
113    }
114
115    /// Create an auth key extraction error
116    pub fn auth_key_failed(reason: impl Into<String>) -> Self {
117        Self::AuthKeyExtractionFailed { reason: reason.into() }
118    }
119}