normalize-package-index 0.3.2

Package index ingestion from distro and language registries
Documentation
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Void Linux package index fetcher (xbps).
//!
//! Fetches package metadata from Void Linux repositories.
//!
//! ## API Strategy
//! - **fetch**: Searches cached `repo-default.voidlinux.org/.../x86_64-repodata` (zstd tar + XML plist)
//! - **fetch_versions**: Loads from all configured repos
//! - **search**: Filters cached repodata
//! - **fetch_all**: Full repodata (cached 1 hour, ~20MB uncompressed per repo)
//!
//! ## Multi-repo Support
//! ```rust,ignore
//! use normalize_packages::index::void::{Void, VoidRepo};
//!
//! // All repos (default)
//! let all = Void::all();
//!
//! // x86_64 glibc only
//! let x64 = Void::with_repos(&[VoidRepo::X86_64, VoidRepo::X86_64Nonfree]);
//!
//! // musl variants
//! let musl = Void::musl();
//! ```

use super::{IndexError, PackageIndex, PackageMeta, VersionMeta};
use crate::cache;
use rayon::prelude::*;
use std::collections::HashMap;
use std::io::Read;
use std::time::Duration;

/// Cache TTL for Void package index (1 hour).
const CACHE_TTL: Duration = Duration::from_secs(60 * 60);

/// Void Linux repository base URL.
const VOID_MIRROR: &str = "https://repo-default.voidlinux.org/current";

/// Available Void Linux repositories.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum VoidRepo {
    // === x86_64 glibc ===
    /// x86_64 glibc main repository
    X86_64,
    /// x86_64 glibc nonfree repository
    X86_64Nonfree,

    // === x86_64 musl ===
    /// x86_64 musl main repository
    X86_64Musl,
    /// x86_64 musl nonfree repository
    X86_64MuslNonfree,

    // === aarch64 glibc ===
    /// aarch64 glibc main repository
    Aarch64,
    /// aarch64 glibc nonfree repository
    Aarch64Nonfree,

    // === aarch64 musl ===
    /// aarch64 musl main repository
    Aarch64Musl,
    /// aarch64 musl nonfree repository
    Aarch64MuslNonfree,
}

impl VoidRepo {
    /// Get the repository URL.
    fn url(&self) -> String {
        match self {
            Self::X86_64 => format!("{}/x86_64-repodata", VOID_MIRROR),
            Self::X86_64Nonfree => format!("{}/nonfree/x86_64-repodata", VOID_MIRROR),
            Self::X86_64Musl => format!("{}/musl/x86_64-repodata", VOID_MIRROR),
            Self::X86_64MuslNonfree => format!("{}/musl/nonfree/x86_64-repodata", VOID_MIRROR),
            Self::Aarch64 => format!("{}/aarch64-repodata", VOID_MIRROR),
            Self::Aarch64Nonfree => format!("{}/nonfree/aarch64-repodata", VOID_MIRROR),
            Self::Aarch64Musl => format!("{}/musl/aarch64-repodata", VOID_MIRROR),
            Self::Aarch64MuslNonfree => format!("{}/musl/nonfree/aarch64-repodata", VOID_MIRROR),
        }
    }

    /// Get the repository name for tagging.
    pub fn name(&self) -> &'static str {
        match self {
            Self::X86_64 => "x86_64",
            Self::X86_64Nonfree => "x86_64-nonfree",
            Self::X86_64Musl => "x86_64-musl",
            Self::X86_64MuslNonfree => "x86_64-musl-nonfree",
            Self::Aarch64 => "aarch64",
            Self::Aarch64Nonfree => "aarch64-nonfree",
            Self::Aarch64Musl => "aarch64-musl",
            Self::Aarch64MuslNonfree => "aarch64-musl-nonfree",
        }
    }

    /// All available repositories.
    pub fn all() -> &'static [VoidRepo] {
        &[
            Self::X86_64,
            Self::X86_64Nonfree,
            Self::X86_64Musl,
            Self::X86_64MuslNonfree,
            Self::Aarch64,
            Self::Aarch64Nonfree,
            Self::Aarch64Musl,
            Self::Aarch64MuslNonfree,
        ]
    }

    /// x86_64 glibc repositories.
    pub fn x86_64() -> &'static [VoidRepo] {
        &[Self::X86_64, Self::X86_64Nonfree]
    }

    /// x86_64 musl repositories.
    pub fn x86_64_musl() -> &'static [VoidRepo] {
        &[Self::X86_64Musl, Self::X86_64MuslNonfree]
    }

    /// All musl repositories.
    pub fn musl() -> &'static [VoidRepo] {
        &[
            Self::X86_64Musl,
            Self::X86_64MuslNonfree,
            Self::Aarch64Musl,
            Self::Aarch64MuslNonfree,
        ]
    }

    /// All glibc repositories.
    pub fn glibc() -> &'static [VoidRepo] {
        &[
            Self::X86_64,
            Self::X86_64Nonfree,
            Self::Aarch64,
            Self::Aarch64Nonfree,
        ]
    }

    /// Free (non-proprietary) repositories only.
    pub fn free() -> &'static [VoidRepo] {
        &[
            Self::X86_64,
            Self::X86_64Musl,
            Self::Aarch64,
            Self::Aarch64Musl,
        ]
    }
}

/// Void Linux package index fetcher with configurable repositories.
pub struct Void {
    repos: Vec<VoidRepo>,
}

impl Void {
    /// Create a fetcher with all repositories.
    pub fn all() -> Self {
        Self {
            repos: VoidRepo::all().to_vec(),
        }
    }

    /// Create a fetcher with x86_64 glibc repositories.
    pub fn x86_64() -> Self {
        Self {
            repos: VoidRepo::x86_64().to_vec(),
        }
    }

    /// Create a fetcher with x86_64 musl repositories.
    pub fn x86_64_musl() -> Self {
        Self {
            repos: VoidRepo::x86_64_musl().to_vec(),
        }
    }

    /// Create a fetcher with all musl repositories.
    pub fn musl() -> Self {
        Self {
            repos: VoidRepo::musl().to_vec(),
        }
    }

    /// Create a fetcher with all glibc repositories.
    pub fn glibc() -> Self {
        Self {
            repos: VoidRepo::glibc().to_vec(),
        }
    }

    /// Create a fetcher with free repositories only.
    pub fn free() -> Self {
        Self {
            repos: VoidRepo::free().to_vec(),
        }
    }

    /// Create a fetcher with custom repository selection.
    pub fn with_repos(repos: &[VoidRepo]) -> Self {
        Self {
            repos: repos.to_vec(),
        }
    }

    /// Parse plist XML into packages.
    fn parse_plist(xml: &str, repo: VoidRepo) -> Result<Vec<PackageMeta>, IndexError> {
        let mut packages = Vec::new();
        let mut current_name: Option<String> = None;
        let mut in_package = false;
        let mut current_field: Option<String> = None;

        let mut version = String::new();
        let mut homepage = String::new();
        let mut description = String::new();
        let mut license = String::new();
        let mut maintainer = String::new();

        for line in xml.lines() {
            let line = line.trim();

            if line.starts_with("<key>") && line.ends_with("</key>") {
                let key = &line[5..line.len() - 6];
                if !in_package {
                    current_name = Some(key.to_string());
                    in_package = false;
                    version.clear();
                    homepage.clear();
                    description.clear();
                    license.clear();
                    maintainer.clear();
                } else {
                    current_field = Some(key.to_string());
                }
            } else if line == "<dict>" && current_name.is_some() && !in_package {
                in_package = true;
            } else if line == "</dict>" && in_package {
                if let Some(name) = current_name.take() {
                    let (pkg_name, ver) = if version.contains('-') {
                        let parts: Vec<&str> = version.rsplitn(2, '-').collect();
                        if parts.len() == 2 {
                            (parts[1].to_string(), parts[0].to_string())
                        } else {
                            (name.clone(), version.clone())
                        }
                    } else {
                        (name.clone(), version.clone())
                    };

                    let mut extra = HashMap::new();
                    extra.insert(
                        "source_repo".to_string(),
                        serde_json::Value::String(repo.name().to_string()),
                    );

                    packages.push(PackageMeta {
                        name: pkg_name,
                        version: ver,
                        description: if description.is_empty() {
                            None
                        } else {
                            Some(description.clone())
                        },
                        homepage: if homepage.is_empty() {
                            None
                        } else {
                            Some(homepage.clone())
                        },
                        repository: Some("https://github.com/void-linux/void-packages".to_string()),
                        license: if license.is_empty() {
                            None
                        } else {
                            Some(license.clone())
                        },
                        maintainers: if maintainer.is_empty() {
                            Vec::new()
                        } else {
                            vec![maintainer.clone()]
                        },
                        binaries: Vec::new(),
                        keywords: Vec::new(),
                        published: None,
                        downloads: None,
                        archive_url: None,
                        checksum: None,
                        extra,
                    });
                }
                in_package = false;
            } else if line.starts_with("<string>") && line.ends_with("</string>") {
                let value = &line[8..line.len() - 9];
                if let Some(field) = &current_field {
                    match field.as_str() {
                        "pkgver" => version = value.to_string(),
                        "homepage" => homepage = value.to_string(),
                        "short_desc" => description = value.to_string(),
                        "license" => license = value.to_string(),
                        "maintainer" => maintainer = value.to_string(),
                        _ => {}
                    }
                }
                current_field = None;
            }
        }

        Ok(packages)
    }

    /// Load packages from a single repository.
    fn load_repo(repo: VoidRepo) -> Result<Vec<PackageMeta>, IndexError> {
        let url = repo.url();

        let (data, _was_cached) = cache::fetch_with_cache(
            "void",
            &format!("repodata-{}", repo.name()),
            &url,
            CACHE_TTL,
        )
        .map_err(IndexError::Network)?;

        // Decompress zstd
        let decompressed = zstd::decode_all(std::io::Cursor::new(&data))
            .map_err(|e| IndexError::Decompress(e.to_string()))?;

        // Extract tar
        let mut archive = tar::Archive::new(std::io::Cursor::new(decompressed));

        for entry in archive.entries().map_err(IndexError::Io)? {
            let mut entry = entry.map_err(IndexError::Io)?;
            let path = entry.path().map_err(IndexError::Io)?;

            if path.to_string_lossy() == "index.plist" {
                let mut xml = String::new();
                entry.read_to_string(&mut xml).map_err(IndexError::Io)?;
                return Self::parse_plist(&xml, repo);
            }
        }

        Err(IndexError::Parse("index.plist not found in archive".into()))
    }

    /// Load packages from all configured repositories in parallel.
    fn load_packages(&self) -> Result<Vec<PackageMeta>, IndexError> {
        let results: Vec<_> = self
            .repos
            .par_iter()
            .map(|&repo| Self::load_repo(repo))
            .collect();

        let mut packages = Vec::new();
        for result in results {
            match result {
                Ok(pkgs) => packages.extend(pkgs),
                Err(e) => {
                    tracing::warn!("failed to load Void repo: {}", e);
                }
            }
        }

        Ok(packages)
    }
}

impl PackageIndex for Void {
    fn ecosystem(&self) -> &'static str {
        "void"
    }

    fn display_name(&self) -> &'static str {
        "Void Linux (xbps)"
    }

    fn fetch(&self, name: &str) -> Result<PackageMeta, IndexError> {
        let packages = self.load_packages()?;

        packages
            .into_iter()
            .find(|p| p.name.eq_ignore_ascii_case(name))
            .ok_or_else(|| IndexError::NotFound(name.to_string()))
    }

    fn fetch_versions(&self, name: &str) -> Result<Vec<VersionMeta>, IndexError> {
        let packages = self.load_packages()?;

        let versions: Vec<_> = packages
            .into_iter()
            .filter(|p| p.name.eq_ignore_ascii_case(name))
            .map(|p| VersionMeta {
                version: p.version,
                released: None,
                yanked: false,
            })
            .collect();

        if versions.is_empty() {
            return Err(IndexError::NotFound(name.to_string()));
        }

        Ok(versions)
    }

    fn search(&self, query: &str) -> Result<Vec<PackageMeta>, IndexError> {
        let packages = self.load_packages()?;
        let query_lower = query.to_lowercase();

        Ok(packages
            .into_iter()
            .filter(|p| {
                p.name.to_lowercase().contains(&query_lower)
                    || p.description
                        .as_ref()
                        .map(|d| d.to_lowercase().contains(&query_lower))
                        .unwrap_or(false)
            })
            .collect())
    }

    fn supports_fetch_all(&self) -> bool {
        true
    }

    fn fetch_all(&self) -> Result<Vec<PackageMeta>, IndexError> {
        self.load_packages()
    }
}