ptth/crates/ptth_relay/src/config.rs

91 lines
2.0 KiB
Rust
Raw Normal View History

2020-11-29 18:37:33 +00:00
// False positive with itertools::process_results
#![allow (clippy::redundant_closure)]
use std::{
2020-11-29 18:37:33 +00:00
collections::HashMap,
convert::{TryFrom, TryInto},
iter::FromIterator,
path::Path,
};
2020-11-29 16:58:56 +00:00
use crate::errors::ConfigError;
// Stuff we need to load from the config file and use to
// set up the HTTP server
pub mod file {
2020-11-29 18:37:33 +00:00
use std::collections::HashMap;
use serde::Deserialize;
#[derive (Deserialize)]
pub struct Server {
pub tripcode: String,
pub display_name: Option <String>,
}
#[derive (Deserialize)]
pub struct Config {
pub port: Option <u16>,
pub servers: HashMap <String, Server>,
}
}
// Stuff we actually need at runtime
pub struct Server {
pub tripcode: blake3::Hash,
pub display_name: Option <String>,
}
pub struct Config {
pub servers: HashMap <String, Server>,
}
impl TryFrom <file::Server> for Server {
type Error = ConfigError;
fn try_from (f: file::Server) -> Result <Self, Self::Error> {
let bytes: Vec <u8> = 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 <file::Config> for Config {
type Error = ConfigError;
fn try_from (f: file::Config) -> Result <Self, Self::Error> {
let servers = f.servers.into_iter ()
.map (|(k, v)| Ok::<_, ConfigError> ((k, v.try_into ()?)));
let servers = itertools::process_results (servers, |i| HashMap::from_iter (i))?;
Ok (Self {
servers,
})
}
}
impl Config {
pub async fn from_file (path: &Path) -> Result <Self, ConfigError> {
use tokio::prelude::*;
let mut f = tokio::fs::File::open (path).await?;
2020-11-29 18:37:33 +00:00
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)
}
}