Module kv

Module kv 

Source
Expand description

Type-safe KEY=VALUE parsing for various pkgsrc formats.

This module provides the Kv derive macro and supporting types for parsing various pkgsrc formats that use KEY=VALUE pairs, including:

Types such as PkgName only need to implement the FromKv trait to be used directly.

Multi-line variables such as DESCRIPTION in pkg_summary(5) are supported by adding the #[kv(multiline)] attribute which will append each line to a Vec.

Single-line variables where it makes sense to split the input such as CATEGORIES can do so easily by declaring themselves as Vec.

§Example

use indoc::indoc;
use pkgsrc::{PkgName, kv::Kv};

#[derive(Kv, Debug, PartialEq)]
#[kv(allow_unknown)]
struct Package {
    pkgname: PkgName,
    size_pkg: u64,
    categories: Vec<String>,
    #[kv(variable = "DESCRIPTION", multiline)]
    desc: Vec<String>,
    // There is no known multi-line variable that also contains multiple
    // values per line, this is purely to show how one might be handled if
    // necessary, though it would be strongly recommended against.
    #[kv(multiline)]
    all_depends: Vec<Vec<String>>,
}

let input = indoc! {"
    PKGNAME=mktool-1.4.2
    SIZE_PKG=6999600
    CATEGORIES=devel pkgtools
    DESCRIPTION=This is a highly-performant collection of utilities that provide
    DESCRIPTION=alternate implementations for parts of the pkgsrc mk infrastructure.
    UNKNOWN=Without allow_unknown this would trigger parse failure.
    ALL_DEPENDS=cwrappers>=20150314:../../pkgtools/cwrappers
    ALL_DEPENDS=checkperms>=1.1:../../sysutils/checkperms rust>=1.74.0:../../lang/rust
"};

let pkg = Package::parse(input)?;
assert_eq!(pkg.pkgname, "mktool-1.4.2");
assert_eq!(pkg.size_pkg, 6999600);
assert_eq!(pkg.categories, vec!["devel", "pkgtools"]);
assert!(pkg.desc[1].starts_with("alternate implementations "));
assert_eq!(pkg.all_depends.len(), 2);
assert_eq!(pkg.all_depends[0].len(), 1);
assert_eq!(pkg.all_depends[1].len(), 2);

Structs§

Span
A byte offset and length in the input, for error reporting.

Enums§

Error
Errors that can occur during parsing.

Traits§

FromKv
Trait for types that can be parsed from a KEY=VALUE string.

Type Aliases§

Result
A Result type alias using Error.

Derive Macros§

Kv
Derive macro for parsing KEY=VALUE formatted input.