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 {
18 path: PathBuf,
20 },
21
22 #[error("required file not found: {file} in {folder}")]
24 FileNotFound {
25 file: String,
27 folder: PathBuf,
29 },
30
31 #[error("key file not found in tdata folder")]
33 KeyFileNotFound,
34
35 #[error("decryption failed: wrong passcode or corrupted data")]
37 DecryptionFailed,
38
39 #[error("tdata is password-protected, passcode required")]
41 PasscodeRequired,
42
43 #[error("invalid passcode")]
45 InvalidPasscode,
46
47 #[error("QDataStream parse error: {message}")]
49 QDataStreamError {
50 message: String,
52 },
53
54 #[error("unexpected end of data at offset {offset}")]
56 UnexpectedEof {
57 offset: u64,
59 },
60
61 #[error("invalid UTF-16 string data")]
63 InvalidUtf16,
64
65 #[error("invalid data format: {message}")]
67 InvalidFormat {
68 message: String,
70 },
71
72 #[error("no accounts found in tdata")]
74 NoAccounts,
75
76 #[error("account index {index} out of range (max: {max})")]
78 AccountIndexOutOfRange {
79 index: usize,
81 max: usize,
83 },
84
85 #[error("checksum mismatch: data may be corrupted")]
87 ChecksumMismatch,
88
89 #[error("unsupported tdata version: {version}")]
91 UnsupportedVersion {
92 version: u32,
94 },
95
96 #[error("failed to extract auth key: {reason}")]
98 AuthKeyExtractionFailed {
99 reason: String,
101 },
102}
103
104impl Error {
105 pub fn qdatastream(msg: impl Into<String>) -> Self {
107 Self::QDataStreamError { message: msg.into() }
108 }
109
110 pub fn invalid_format(msg: impl Into<String>) -> Self {
112 Self::InvalidFormat { message: msg.into() }
113 }
114
115 pub fn auth_key_failed(reason: impl Into<String>) -> Self {
117 Self::AuthKeyExtractionFailed { reason: reason.into() }
118 }
119}