41 lines
1.0 KiB
Rust
41 lines
1.0 KiB
Rust
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}"))
|
|
}
|
|
}
|
|
|
|
|