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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//! MSYS2 package index fetcher (Windows development).
//!
//! Fetches package metadata from the MSYS2 packages API.
//!
//! ## API Strategy
//! - **fetch**: `packages.msys2.org/api/package/{name}` - Official MSYS2 JSON API
//! - **fetch_versions**: Same API, single version
//! - **search**: `packages.msys2.org/api/search?q=`
//! - **fetch_all**: `packages.msys2.org/api/packages` (all packages)
//!
//! ## Multi-environment Support
//! ```rust,ignore
//! use normalize_packages::index::msys2::{Msys2, Msys2Env};
//!
//! // All environments (default)
//! let all = Msys2::all();
//!
//! // MinGW 64-bit only
//! let mingw64 = Msys2::mingw64();
//!
//! // Modern environments (UCRT + Clang)
//! let modern = Msys2::modern();
//! ```
use super::{IndexError, PackageIndex, PackageMeta, VersionMeta};
use std::collections::HashMap;
/// Available MSYS2 environments/subsystems.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Msys2Env {
/// MSYS - POSIX compatibility layer
Msys,
/// MinGW 32-bit - Windows native (GCC)
Mingw32,
/// MinGW 64-bit - Windows native (GCC)
Mingw64,
/// UCRT 64-bit - Windows native with Universal C Runtime
Ucrt64,
/// Clang 32-bit - Windows native (Clang/LLVM)
Clang32,
/// Clang 64-bit - Windows native (Clang/LLVM)
Clang64,
}
impl Msys2Env {
/// Get the repository name used in the API.
fn repo_name(&self) -> &'static str {
match self {
Self::Msys => "msys",
Self::Mingw32 => "mingw32",
Self::Mingw64 => "mingw64",
Self::Ucrt64 => "ucrt64",
Self::Clang32 => "clang32",
Self::Clang64 => "clang64",
}
}
/// Get the environment name for tagging.
pub fn name(&self) -> &'static str {
self.repo_name()
}
/// All available environments.
pub fn all() -> &'static [Msys2Env] {
&[
Self::Msys,
Self::Mingw32,
Self::Mingw64,
Self::Ucrt64,
Self::Clang32,
Self::Clang64,
]
}
/// MinGW environments (traditional GCC toolchains).
pub fn mingw() -> &'static [Msys2Env] {
&[Self::Mingw32, Self::Mingw64]
}
/// MinGW 64-bit only.
pub fn mingw64() -> &'static [Msys2Env] {
&[Self::Mingw64]
}
/// UCRT environments (modern Windows runtime).
pub fn ucrt() -> &'static [Msys2Env] {
&[Self::Ucrt64]
}
/// Clang/LLVM environments.
pub fn clang() -> &'static [Msys2Env] {
&[Self::Clang32, Self::Clang64]
}
/// Modern environments (UCRT + Clang).
pub fn modern() -> &'static [Msys2Env] {
&[Self::Ucrt64, Self::Clang32, Self::Clang64]
}
/// 64-bit environments only.
pub fn x64() -> &'static [Msys2Env] {
&[Self::Mingw64, Self::Ucrt64, Self::Clang64]
}
}
/// MSYS2 package index fetcher with configurable environments.
pub struct Msys2 {
envs: Vec<Msys2Env>,
}
impl Msys2 {
/// MSYS2 packages API base URL.
const API_BASE: &'static str = "https://packages.msys2.org/api";
/// Create a fetcher with all environments.
pub fn all() -> Self {
Self {
envs: Msys2Env::all().to_vec(),
}
}
/// Create a fetcher with MinGW 64-bit only.
pub fn mingw64() -> Self {
Self {
envs: Msys2Env::mingw64().to_vec(),
}
}
/// Create a fetcher with MinGW environments.
pub fn mingw() -> Self {
Self {
envs: Msys2Env::mingw().to_vec(),
}
}
/// Create a fetcher with UCRT environment.
pub fn ucrt() -> Self {
Self {
envs: Msys2Env::ucrt().to_vec(),
}
}
/// Create a fetcher with modern environments.
pub fn modern() -> Self {
Self {
envs: Msys2Env::modern().to_vec(),
}
}
/// Create a fetcher with 64-bit environments.
pub fn x64() -> Self {
Self {
envs: Msys2Env::x64().to_vec(),
}
}
/// Create a fetcher with custom environment selection.
pub fn with_envs(envs: &[Msys2Env]) -> Self {
Self {
envs: envs.to_vec(),
}
}
/// Check if a package's repo matches any configured environment.
fn matches_env(&self, repo: Option<&str>) -> bool {
match repo {
Some(r) => self.envs.iter().any(|e| e.repo_name() == r),
None => true, // If no repo info, include it
}
}
}
impl PackageIndex for Msys2 {
fn ecosystem(&self) -> &'static str {
"msys2"
}
fn display_name(&self) -> &'static str {
"MSYS2 (Windows)"
}
fn fetch(&self, name: &str) -> Result<PackageMeta, IndexError> {
let url = format!("{}/search?query={}", Self::API_BASE, name);
let response: serde_json::Value = ureq::get(&url).call()?.into_json()?;
// Check for exact match first
let pkg = if let Some(exact) = response["results"]["exact"].as_object() {
let repo = exact.get("repo").and_then(|r| r.as_str());
if self.matches_env(repo) {
Some((exact.clone(), repo.map(String::from)))
} else {
None
}
} else {
None
};
let (pkg, repo) = if let Some((p, r)) = pkg {
(p, r)
} else if let Some(others) = response["results"]["other"].as_array() {
// Find first match in other results that matches our environments
others
.iter()
.find_map(|p| {
let repo = p["repo"].as_str();
if (p["name"].as_str() == Some(name) || p["realname"].as_str() == Some(name))
&& self.matches_env(repo)
{
p.as_object()
.map(|obj| (obj.clone(), repo.map(String::from)))
} else {
None
}
})
.ok_or_else(|| IndexError::NotFound(name.to_string()))?
} else {
return Err(IndexError::NotFound(name.to_string()));
};
let mut extra = HashMap::new();
if let Some(r) = &repo {
extra.insert(
"source_repo".to_string(),
serde_json::Value::String(r.clone()),
);
}
// Extract license from nested array
let license = pkg
.get("licenses")
.and_then(|l| l.as_array())
.and_then(|arr| arr.first())
.and_then(|inner| inner.as_array())
.and_then(|arr| arr.first())
.and_then(|l| l.as_str())
.map(String::from);
// Collect keywords from groups
let keywords: Vec<String> = pkg
.get("groups")
.and_then(|g| g.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str().map(String::from))
.collect()
})
.unwrap_or_default();
Ok(PackageMeta {
name: pkg
.get("realname")
.or(pkg.get("name"))
.and_then(|n| n.as_str())
.unwrap_or(name)
.to_string(),
version: pkg
.get("version")
.and_then(|v| v.as_str())
.unwrap_or("unknown")
.to_string(),
description: pkg
.get("descriptions")
.and_then(|d| d.as_str())
.map(String::from),
homepage: pkg.get("url").and_then(|u| u.as_str()).map(String::from),
repository: pkg
.get("source_url")
.and_then(|u| u.as_str())
.map(String::from),
license,
binaries: Vec::new(),
keywords,
maintainers: Vec::new(),
published: None,
downloads: None,
archive_url: None,
checksum: None,
extra,
})
}
fn fetch_versions(&self, name: &str) -> Result<Vec<VersionMeta>, IndexError> {
// MSYS2 API only provides current version
let meta = self.fetch(name)?;
let repo = meta
.extra
.get("source_repo")
.and_then(|v| v.as_str())
.unwrap_or("unknown");
Ok(vec![VersionMeta {
version: format!("{} ({})", meta.version, repo),
released: None,
yanked: false,
}])
}
fn search(&self, query: &str) -> Result<Vec<PackageMeta>, IndexError> {
let url = format!("{}/search?query={}", Self::API_BASE, query);
let response: serde_json::Value = ureq::get(&url).call()?.into_json()?;
let mut packages = Vec::new();
// Add exact match if present and matches environment
if let Some(exact) = response["results"]["exact"].as_object() {
let repo = exact.get("repo").and_then(|r| r.as_str());
if self.matches_env(repo)
&& let Some(pkg) = parse_msys2_package(exact, repo)
{
packages.push(pkg);
}
}
// Add other matches that match our environments
if let Some(others) = response["results"]["other"].as_array() {
for other in others {
if let Some(obj) = other.as_object() {
let repo = obj.get("repo").and_then(|r| r.as_str());
if self.matches_env(repo)
&& let Some(pkg) = parse_msys2_package(obj, repo)
{
packages.push(pkg);
}
}
}
}
Ok(packages)
}
}
fn parse_msys2_package(
pkg: &serde_json::Map<String, serde_json::Value>,
repo: Option<&str>,
) -> Option<PackageMeta> {
let license = pkg
.get("licenses")
.and_then(|l| l.as_array())
.and_then(|arr| arr.first())
.and_then(|inner| inner.as_array())
.and_then(|arr| arr.first())
.and_then(|l| l.as_str())
.map(String::from);
let keywords: Vec<String> = pkg
.get("groups")
.and_then(|g| g.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str().map(String::from))
.collect()
})
.unwrap_or_default();
let mut extra = HashMap::new();
if let Some(r) = repo {
extra.insert(
"source_repo".to_string(),
serde_json::Value::String(r.to_string()),
);
}
Some(PackageMeta {
name: pkg
.get("realname")
.or(pkg.get("name"))
.and_then(|n| n.as_str())?
.to_string(),
version: pkg
.get("version")
.and_then(|v| v.as_str())
.unwrap_or("unknown")
.to_string(),
description: pkg
.get("descriptions")
.and_then(|d| d.as_str())
.map(String::from),
homepage: pkg.get("url").and_then(|u| u.as_str()).map(String::from),
repository: pkg
.get("source_url")
.and_then(|u| u.as_str())
.map(String::from),
license,
binaries: Vec::new(),
keywords,
maintainers: Vec::new(),
published: None,
downloads: None,
archive_url: None,
checksum: None,
extra,
})
}