repodb_parser 0.3.1

Parser for Arch Linux repository DB's
Documentation
// SPDX-FileCopyrightText: 2022-2024 Michael Picht <mipi@fsfe.org>
//
// SPDX-License-Identifier: GPL-3.0-or-later

//! # repodb_parser
//!
//! repodb_parser parses [Arch Linux](https://archlinux.org) repository DB's
//! into Rust data structures. The repository DB's are expected as
//! [gzip](https://en.wikipedia.org/wiki/Gzip) or
//! [xz](https://en.wikipedia.org/wiki/XZ_Utils) compressed
//! [tar](https://en.wikipedia.org/wiki/Tar_(computing)) archives (i.e.,
//! `*.tar.gz` or `*.tar.xz` files) or symbolic links to such files.
//!

pub use crate::{pkg::Pkgs, repodb::RepoDB};
use anyhow::Context;
use std::path::Path;

pub mod dep;
mod parser;
pub mod pkg;
pub mod repodb;

/// Parses an Arch Linux repository DB and returns the content as B-tree map of
/// package meta data structures. The map is sorted by the package name in
/// ascending alphabetical order.
/// parse expects a path to a repository DB file or a symbolic link to
/// such a file. The file must be a gzip or xz compressed tar archive.
///
/// # Example
///
/// ```
/// let mut path = std::path::PathBuf::new();
/// path.push(<PATH-TO-REPOSITORY-DB>);
///
/// match repodb_parser::parse(path.as_path()) {
///     Err(err) => {
///         eprintln!("{:?}", err);
///         std::process::exit(1);
///     }
///     Ok(pkgs) => {
///         for pkg in pkgs.packages() {
///             println!("{}", pkg);
///         }
///     }
/// }
/// ```
///
pub fn parse<P>(db: P) -> anyhow::Result<Pkgs>
where
    P: AsRef<Path>,
{
    let err_msg = format!("cannot parse repository DB {}", db.as_ref().display());

    RepoDB::new(db)
        .with_context(|| err_msg.clone())?
        .packages()
        .with_context(|| err_msg.clone())
}