1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//! Unified error type for arch-toolkit.
use thiserror::Error;
/// Unified error type for all arch-toolkit operations.
///
/// This error type covers all possible failure modes across different modules,
/// providing clear, actionable error messages.
#[derive(Error, Debug)]
pub enum ArchToolkitError {
/// Network or HTTP request error.
///
/// Note: For AUR operations, prefer using operation-specific error variants
/// (`SearchFailed`, `InfoFailed`, `CommentsFailed`, `PkgbuildFailed`) to preserve context.
/// This variant is retained for client initialization and non-AUR operations.
#[cfg(feature = "aur")]
#[error("Network error: {0}")]
Network(reqwest::Error),
/// AUR search operation failed.
#[cfg(feature = "aur")]
#[error("AUR search failed for query '{query}': {source}")]
SearchFailed {
/// The search query that failed.
query: String,
/// The underlying network error.
#[source]
source: reqwest::Error,
},
/// AUR info fetch operation failed.
#[cfg(feature = "aur")]
#[error("AUR info fetch failed for packages [{packages}]: {source}")]
InfoFailed {
/// Comma-separated list of package names that failed.
packages: String,
/// The underlying network error.
#[source]
source: reqwest::Error,
},
/// AUR comments fetch operation failed.
#[cfg(feature = "aur")]
#[error("AUR comments fetch failed for package '{package}': {source}")]
CommentsFailed {
/// The package name that failed.
package: String,
/// The underlying network error.
#[source]
source: reqwest::Error,
},
/// PKGBUILD fetch operation failed.
#[cfg(feature = "aur")]
#[error("PKGBUILD fetch failed for package '{package}': {source}")]
PkgbuildFailed {
/// The package name that failed.
package: String,
/// The underlying network error.
#[source]
source: reqwest::Error,
},
/// JSON parsing error.
#[error("JSON parsing error: {0}")]
Json(#[from] serde_json::Error),
/// File I/O error with path context.
#[error("I/O error at '{path}': {source}")]
Io {
/// The file path where the I/O operation failed.
path: String,
/// The underlying I/O error.
#[source]
source: std::io::Error,
},
/// Custom parsing error with message.
#[error("Parse error: {0}")]
Parse(String),
/// Rate limiting error with optional retry-after information.
#[error("Rate limited by server{0}", .retry_after.map(|s| format!(" (retry after {s}s)")).unwrap_or_default())]
RateLimited {
/// Optional retry-after value in seconds from server.
retry_after: Option<u64>,
},
/// Package not found (enhanced with package name).
#[error("Package '{package}' not found")]
PackageNotFound {
/// The package name that was not found.
package: String,
},
/// Invalid input parameter.
#[error("Invalid input: {0}")]
InvalidInput(String),
/// Empty input provided where a value is required.
#[error("Empty {field}: {message}")]
EmptyInput {
/// The field name that is empty.
field: String,
/// Detailed message about why the field cannot be empty.
message: String,
},
/// Package name contains invalid characters or format.
#[error("Invalid package name '{name}': {reason}")]
InvalidPackageName {
/// The invalid package name.
name: String,
/// Reason why the package name is invalid.
reason: String,
},
/// Search query validation failed.
#[error("Invalid search query: {reason}")]
InvalidSearchQuery {
/// Reason why the search query is invalid.
reason: String,
},
/// Input exceeds the maximum byte length.
#[error("{field} exceeds maximum length of {max_length} bytes (got {actual_length})")]
InputTooLong {
/// The field name that is too long.
field: String,
/// Maximum allowed length in bytes.
max_length: usize,
/// Actual length of the input in bytes.
actual_length: usize,
},
}
#[cfg(feature = "aur")]
impl ArchToolkitError {
/// What: Create a `SearchFailed` error with query context.
///
/// Inputs:
/// - `query`: The search query that failed
/// - `source`: The underlying network error
///
/// Output:
/// - `ArchToolkitError::SearchFailed` variant
///
/// Details:
/// - Convenience constructor for search operation errors
/// - Preserves both the query and the underlying error
#[must_use]
pub fn search_failed(query: impl Into<String>, source: reqwest::Error) -> Self {
Self::SearchFailed {
query: query.into(),
source,
}
}
/// What: Create an `InfoFailed` error with package names context.
///
/// Inputs:
/// - `packages`: Slice of package names that failed
/// - `source`: The underlying network error
///
/// Output:
/// - `ArchToolkitError::InfoFailed` variant
///
/// Details:
/// - Convenience constructor for info operation errors
/// - Formats package names as comma-separated string
/// - Preserves both the package names and the underlying error
#[must_use]
pub fn info_failed(packages: &[&str], source: reqwest::Error) -> Self {
Self::InfoFailed {
packages: packages.join(", "),
source,
}
}
/// What: Create a `CommentsFailed` error with package name context.
///
/// Inputs:
/// - `package`: The package name that failed
/// - `source`: The underlying network error
///
/// Output:
/// - `ArchToolkitError::CommentsFailed` variant
///
/// Details:
/// - Convenience constructor for comments operation errors
/// - Preserves both the package name and the underlying error
#[must_use]
pub fn comments_failed(package: impl Into<String>, source: reqwest::Error) -> Self {
Self::CommentsFailed {
package: package.into(),
source,
}
}
/// What: Create a `PkgbuildFailed` error with package name context.
///
/// Inputs:
/// - `package`: The package name that failed
/// - `source`: The underlying network error
///
/// Output:
/// - `ArchToolkitError::PkgbuildFailed` variant
///
/// Details:
/// - Convenience constructor for pkgbuild operation errors
/// - Preserves both the package name and the underlying error
#[must_use]
pub fn pkgbuild_failed(package: impl Into<String>, source: reqwest::Error) -> Self {
Self::PkgbuildFailed {
package: package.into(),
source,
}
}
}
impl ArchToolkitError {
/// What: Create an `Io` error with file path context.
///
/// Inputs:
/// - `path`: The file path where the I/O operation failed
/// - `source`: The underlying I/O error
///
/// Output:
/// - `ArchToolkitError::Io` variant
///
/// Details:
/// - Convenience constructor for file I/O errors
/// - Preserves both the path and the underlying error
#[must_use]
pub fn io(path: impl Into<String>, source: std::io::Error) -> Self {
Self::Io {
path: path.into(),
source,
}
}
}
/// Result type alias for arch-toolkit operations.
pub type Result<T> = std::result::Result<T, ArchToolkitError>;