// False positive with itertools::process_results #![allow (clippy::redundant_closure)] use std::{ collections::HashMap, convert::{TryFrom, TryInto}, iter::FromIterator, path::Path, }; use crate::errors::ConfigError; // Stuff we need to load from the config file and use to // set up the HTTP server pub mod file { use serde::Deserialize; #[derive (Deserialize)] pub struct Server { pub name: String, pub tripcode: String, pub display_name: Option , } // Stuff that's identical between the file and the runtime structures #[derive (Default, Deserialize)] pub struct Isomorphic { #[serde (default)] pub enable_dev_mode: bool, #[serde (default)] pub enable_scraper_auth: bool, } #[derive (Deserialize)] pub struct Config { pub port: Option , pub servers: Vec , #[serde (flatten)] pub iso: Isomorphic, } } // Stuff we actually need at runtime pub struct Server { pub tripcode: blake3::Hash, pub display_name: Option , } pub struct Config { pub servers: HashMap , pub iso: file::Isomorphic, } impl TryFrom for Server { type Error = ConfigError; fn try_from (f: file::Server) -> Result { let bytes: Vec = base64::decode (f.tripcode)?; let bytes: [u8; 32] = (&bytes [..]).try_into ().map_err (|_| ConfigError::TripcodeBadLength)?; let tripcode = blake3::Hash::from (bytes); Ok (Self { tripcode, display_name: f.display_name, }) } } impl TryFrom for Config { type Error = ConfigError; fn try_from (f: file::Config) -> Result { let servers = f.servers.into_iter () .map (|server| Ok::<_, ConfigError> ((server.name.clone (), server.try_into ()?))); let servers = itertools::process_results (servers, |i| HashMap::from_iter (i))?; Ok (Self { servers, iso: f.iso, }) } } impl Config { pub async fn from_file (path: &Path) -> Result { use tokio::prelude::*; let mut f = tokio::fs::File::open (path).await?; let mut buffer = vec! [0_u8; 4096]; let bytes_read = f.read (&mut buffer).await?; buffer.truncate (bytes_read); let config_s = String::from_utf8 (buffer)?; let new_config: file::Config = toml::from_str (&config_s)?; Self::try_from (new_config) } }