Skip to main content

Module kv

Module kv 

Source
Expand description

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

This module re-exports the runtime types from the pkgsrc-kv crate — Span, KvError, KvWarning, and the FromKv trait — which power parsing of pkgsrc formats that use KEY=VALUE pairs, including:

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

The Kv derive macro itself lives in pkgsrc-kv and is not re-exported here; add that crate as a dependency to derive Kv for your own structs. Multi-line variables such as DESCRIPTION are collected into a Vec with #[kv(multiline)], and single-line lists such as CATEGORIES by declaring the field as Vec.

§Example

use indoc::indoc;
use pkgsrc::PkgName;
use pkgsrc_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§

KvWarning
A non-fatal problem encountered while parsing.
Span
A byte offset and length in the input, for error reporting.

Enums§

KvError
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 KvError.