| Crates.io | kv-parser |
| lib.rs | kv-parser |
| version | 0.2.0 |
| created_at | 2021-09-30 21:24:19.316659+00 |
| updated_at | 2026-01-04 13:04:12.346508+00 |
| description | A simple parser of key-value-files as hash maps |
| homepage | |
| repository | https://gitlab.com/porky11/kv-parser |
| max_upload_size | |
| id | 458908 |
| size | 6,835 |
A Rust library for parsing simple key-value text files into HashMap<Box<str>, Box<str>>.
The parser reads text files where each non-empty line contains a key-value pair separated by whitespace:
Add to your Cargo.toml:
[dependencies]
kv-parser = "0.1.0"
use std::path::Path;
use kv_parser::{Error, file_to_key_value_map};
fn main() -> Result<(), Error> {
let path = Path::new("savedata.sav");
let save_data = file_to_key_value_map(path)?;
if let Some(level) = save_data.get("current-level") {
println!("Current level: {level}");
}
Ok(())
}
The function returns Result<HashMap<Box<str>, Box<str>>, Error> where Error can be:
Error::OpeningFile - Failed to open the fileError::ReadingFile - I/O error while reading linesError::MultipleKeys(key) - Duplicate key found in fileGiven a file savedata.sav:
current-level vulcano
start-point 3
player-upgrades super-jump climbing lava-suit bombs
The parsed result:
use std::{collections::HashMap, path::Path};
use kv_parser::file_to_key_value_map;
let mut expected = HashMap::new();
expected.insert("current-level".into(), "vulcano".into());
expected.insert("start-point".into(), "3".into());
expected.insert(
"player-upgrades".into(),
"super-jump climbing lava-suit bombs".into(),
);
let result = file_to_key_value_map(Path::new("savedata.sav"))?;
assert_eq!(expected, result);
Box<str> instead of String for reduced memory overheadBufReader