whitespace_conf/lib.rs
1//! Parses configuration files which are key-value pairs delimited by whitespace.
2//!
3//! ### Note
4//! Lines that start with `#` are ignored.
5//!
6//! ### Example
7//! ```rust
8//! use std::fs;
9//!
10//! fn main() {
11//! let string = fs::read_to_string("/etc/login.defs").unwrap();
12//!
13//! let defs = linux_login_defs::parse(&string);
14//!
15//! println!("UID_MIN = {:?}", defs.get("UID_MIN"));
16//! println!("UID_MAX = {:?}", defs.get("UID_MAX"));
17//! }
18//! ```
19
20use std::collections::HashMap;
21
22/// Parses configuration files which are key-value pairs delimited by whitespace.
23pub fn parse<'a>(input: &'a str) -> HashMap<&'a str, &'a str> {
24 let mut map = HashMap::new();
25
26 for mut line in input.lines() {
27 line = line.trim();
28 if line.is_empty() || line.starts_with("#") {
29 continue;
30 }
31
32 if let Some(pos) = line.find(char::is_whitespace) {
33 map.insert(&line[..pos], line[pos + 1..].trim_start());
34 }
35 }
36
37 map
38}