Initial commit: Rust YpDaemon
This commit is contained in:
40
src/config.rs
Normal file
40
src/config.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Config {
|
||||
values: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let file = File::open(path)?;
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
let mut values = HashMap::new();
|
||||
|
||||
for line in reader.lines() {
|
||||
let line = line?;
|
||||
if line.trim().is_empty() || line.trim_start().starts_with('#') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some((key, value)) = line.split_once('=') {
|
||||
values.insert(key.trim().to_string(), value.trim().to_string());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Self { values })
|
||||
}
|
||||
|
||||
pub fn get(&self, key: &str) -> Result<String, String> {
|
||||
self.values
|
||||
.get(key)
|
||||
.cloned()
|
||||
.ok_or_else(|| format!("Konfigurationsschlüssel nicht gefunden: {key}"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user