//! Configuration file structure. (TODO, docs) use std::{ fmt::Debug, fs::File, io::{self, Read, Seek}, net::{Ipv4Addr, SocketAddr, SocketAddrV4}, }; use serde::Deserialize; use crate::serdes::{DatabasePaths, Group}; #[derive(Debug, Default, Deserialize)] #[serde(default)] pub struct Unix { pub groups: Vec, pub magic_paths: DatabasePaths, } #[derive(Debug, Deserialize)] #[serde(default)] pub struct Server { pub listen: SocketAddr, } impl Default for Server { fn default() -> Self { Self { listen: SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 8080)), } } } #[derive(Debug, Default, Deserialize)] #[serde(default)] pub struct Config { pub unix: Unix, pub server: Server, } #[derive(thiserror::Error, Debug)] pub enum ConfigParseError { #[error(transparent)] Io(#[from] io::Error), #[error(transparent)] Parse(#[from] toml::de::Error), } impl Config { /// # Errors /// /// For IO errors reading the file or parsing errors. /// /// # Panics /// /// If file's u64 length might not fit in memory. pub fn from_toml_file(f: &mut File) -> Result { let mut buf = Vec::new(); if let Ok(len) = f.stream_len() { buf.reserve_exact(len.try_into().expect("file's u64 len to fit in memory")); } f.read_to_end(&mut buf)?; Ok(toml::from_slice(&buf)?) } }