pacsea/util/config.rs
1//! Configuration file parsing utilities.
2//!
3//! This module provides helpers for parsing configuration files with common
4//! patterns like comment skipping and key-value parsing.
5
6/// What: Check if a line should be skipped (empty or comment).
7///
8/// Inputs:
9/// - `line`: Line to check
10///
11/// Output:
12/// - `true` if the line should be skipped, `false` otherwise
13///
14/// Details:
15/// - Skips empty lines and lines starting with `#`, `//`, or `;`
16#[must_use]
17pub fn skip_comment_or_empty(line: &str) -> bool {
18 let trimmed = line.trim();
19 trimmed.is_empty()
20 || trimmed.starts_with('#')
21 || trimmed.starts_with("//")
22 || trimmed.starts_with(';')
23}
24
25/// What: Parse a key-value pair from a line.
26///
27/// Inputs:
28/// - `line`: Line containing key=value format
29///
30/// Output:
31/// - `Some((key, value))` if parsing succeeds, `None` otherwise
32///
33/// Details:
34/// - Splits on the first `=` character
35/// - Trims whitespace from both key and value
36#[must_use]
37pub fn parse_key_value(line: &str) -> Option<(String, String)> {
38 let trimmed = line.trim();
39 if !trimmed.contains('=') {
40 return None;
41 }
42 let mut parts = trimmed.splitn(2, '=');
43 let key = parts.next()?.trim().to_string();
44 let value = parts.next()?.trim().to_string();
45 Some((key, value))
46}