use std::fs::File;
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use crate::proxy_error::ProxyResult;
#[derive(Serialize, Deserialize)]
pub struct Config {
pub bind_address: String,
pub bind_port: u16,
}
impl Config {
pub fn initialize(file: PathBuf) -> ProxyResult<Self> {
if !file.exists() {
let file = File::create(file.clone())?;
serde_yaml::to_writer(file, &Self::default())?;
}
Self::load_from_file(file)
}
fn load_from_file(file: PathBuf) -> ProxyResult<Self> {
let file = File::open(file)?;
Ok(serde_yaml::from_reader(file)?)
}
}
impl Default for Config {
fn default() -> Self {
Self {
bind_address: String::from("[::]"),
bind_port: 5354,
}
}
}