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 { path: PathBuf },
18
19    /// Required file is missing from tdata folder
20    #[error("required file not found: {file} in {folder}")]
21    FileNotFound { file: String, folder: PathBuf },
22
23    /// Key file (key_data, key_datas) not found
24    #[error("key file not found in tdata folder")]
25    KeyFileNotFound,
26
27    /// Failed to decrypt tdata - wrong passcode or corrupted data
28    #[error("decryption failed: wrong passcode or corrupted data")]
29    DecryptionFailed,
30
31    /// The tdata folder is password-protected but no passcode provided
32    #[error("tdata is password-protected, passcode required")]
33    PasscodeRequired,
34
35    /// Invalid passcode provided
36    #[error("invalid passcode")]
37    InvalidPasscode,
38
39    /// QDataStream parsing error
40    #[error("QDataStream parse error: {message}")]
41    QDataStreamError { message: String },
42
43    /// Unexpected end of data while parsing
44    #[error("unexpected end of data at offset {offset}")]
45    UnexpectedEof { offset: u64 },
46
47    /// Invalid UTF-16 string data
48    #[error("invalid UTF-16 string data")]
49    InvalidUtf16,
50
51    /// Invalid data format or structure
52    #[error("invalid data format: {message}")]
53    InvalidFormat { message: String },
54
55    /// No accounts found in tdata
56    #[error("no accounts found in tdata")]
57    NoAccounts,
58
59    /// Account index out of range
60    #[error("account index {index} out of range (max: {max})")]
61    AccountIndexOutOfRange { index: usize, max: usize },
62
63    /// MD5 checksum mismatch in encrypted data
64    #[error("checksum mismatch: data may be corrupted")]
65    ChecksumMismatch,
66
67    /// Unsupported tdata version
68    #[error("unsupported tdata version: {version}")]
69    UnsupportedVersion { version: u32 },
70
71    /// Auth key extraction failed
72    #[error("failed to extract auth key: {reason}")]
73    AuthKeyExtractionFailed { reason: String },
74}
75
76impl Error {
77    /// Create a QDataStream error with a message
78    pub fn qdatastream(msg: impl Into<String>) -> Self {
79        Self::QDataStreamError {
80            message: msg.into(),
81        }
82    }
83
84    /// Create an invalid format error with a message
85    pub fn invalid_format(msg: impl Into<String>) -> Self {
86        Self::InvalidFormat {
87            message: msg.into(),
88        }
89    }
90
91    /// Create an auth key extraction error
92    pub fn auth_key_failed(reason: impl Into<String>) -> Self {
93        Self::AuthKeyExtractionFailed {
94            reason: reason.into(),
95        }
96    }
97}