1use std::path::PathBuf;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, thiserror::Error)]
10pub enum Error {
11 #[error("IO error: {0}")]
13 Io(#[from] std::io::Error),
14
15 #[error("tdata folder not found: {path}")]
17 FolderNotFound { path: PathBuf },
18
19 #[error("required file not found: {file} in {folder}")]
21 FileNotFound { file: String, folder: PathBuf },
22
23 #[error("key file not found in tdata folder")]
25 KeyFileNotFound,
26
27 #[error("decryption failed: wrong passcode or corrupted data")]
29 DecryptionFailed,
30
31 #[error("tdata is password-protected, passcode required")]
33 PasscodeRequired,
34
35 #[error("invalid passcode")]
37 InvalidPasscode,
38
39 #[error("QDataStream parse error: {message}")]
41 QDataStreamError { message: String },
42
43 #[error("unexpected end of data at offset {offset}")]
45 UnexpectedEof { offset: u64 },
46
47 #[error("invalid UTF-16 string data")]
49 InvalidUtf16,
50
51 #[error("invalid data format: {message}")]
53 InvalidFormat { message: String },
54
55 #[error("no accounts found in tdata")]
57 NoAccounts,
58
59 #[error("account index {index} out of range (max: {max})")]
61 AccountIndexOutOfRange { index: usize, max: usize },
62
63 #[error("checksum mismatch: data may be corrupted")]
65 ChecksumMismatch,
66
67 #[error("unsupported tdata version: {version}")]
69 UnsupportedVersion { version: u32 },
70
71 #[error("failed to extract auth key: {reason}")]
73 AuthKeyExtractionFailed { reason: String },
74}
75
76impl Error {
77 pub fn qdatastream(msg: impl Into<String>) -> Self {
79 Self::QDataStreamError {
80 message: msg.into(),
81 }
82 }
83
84 pub fn invalid_format(msg: impl Into<String>) -> Self {
86 Self::InvalidFormat {
87 message: msg.into(),
88 }
89 }
90
91 pub fn auth_key_failed(reason: impl Into<String>) -> Self {
93 Self::AuthKeyExtractionFailed {
94 reason: reason.into(),
95 }
96 }
97}