26 lines
659 B
Plaintext
26 lines
659 B
Plaintext
use serde::{Deserialize, Serialize};
|
|
use std::fs;
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub struct Config {
|
|
pub host: String,
|
|
pub port: u16,
|
|
pub debug: bool,
|
|
}
|
|
|
|
impl Config {
|
|
// Чтение из YAML
|
|
pub fn load(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
|
let contents = fs::read_to_string(path)?;
|
|
let config: Config = serde_yaml::from_str(&contents)?;
|
|
Ok(config)
|
|
}
|
|
|
|
// Запись в YAML
|
|
pub fn save(&self, path: &str) -> Result<(), Box<dyn std::error::Error>> {
|
|
let yaml_string = serde_yaml::to_string(self)?;
|
|
fs::write(path, yaml_string)?;
|
|
Ok(())
|
|
}
|
|
}
|