Derive macro for parsing KEY=VALUE formats.
This crate provides [macro@Kv] for automatically implementing parsers
for structs from KEY=VALUE formatted input.
Field Types
| Rust Type | Attribute | Behavior |
|---|---|---|
T |
Required single value | |
Option<T> |
Optional single value | |
Vec<T> |
Whitespace-separated values on single line | |
Option<Vec<T>> |
Optional whitespace-separated values | |
Vec<T> |
#[kv(multiline)] |
Multiple lines collected into Vec |
Option<Vec<T>> |
#[kv(multiline)] |
Optional multiple lines |
HashMap<String, String> |
#[kv(collect)] |
Collects unhandled keys |
Container Attributes
#[kv(allow_unknown)]- Ignore unknown keys instead of returning an error
Field Attributes
#[kv(variable = "KEY")]- Use custom key name instead of uppercased field name#[kv(multiline)]- Collect multiple lines with the same key into aVec#[kv(collect)]- Collect all unhandled keys into thisHashMap<String, String>
Duplicate Key Behavior
For non-multiline fields, duplicate keys overwrite the previous value.
For multiline fields, each occurrence appends to the Vec.
Examples
use indoc;
use ;
use PkgName;
let input = indoc! ;
let pkg = parse?;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// Missing required fields return an error.
assert!;
# Ok::
Use collect to collect unhandled keys into a HashMap, for example
when parsing +BUILD_INFO where arbitrary variables will be present:
use indoc;
use HashMap;
use ;
let input = indoc! ;
let info = parse?;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
# Ok::