👕 refactor: merge the servers' config file and runtime representations

main
_ 2020-12-12 15:57:22 +00:00
parent b43106393a
commit bf8e483d16
2 changed files with 4 additions and 19 deletions

View File

@ -78,6 +78,7 @@ pub mod file {
#[derive (Deserialize)] #[derive (Deserialize)]
pub struct Server { pub struct Server {
// This is duplicated in the hashmap, but it's not a problem
pub name: String, pub name: String,
pub tripcode: BlakeHashWrapper, pub tripcode: BlakeHashWrapper,
pub display_name: Option <String>, pub display_name: Option <String>,
@ -106,33 +107,17 @@ pub mod file {
// Stuff we actually need at runtime // Stuff we actually need at runtime
pub struct Server {
pub tripcode: blake3::Hash,
pub display_name: Option <String>,
}
pub struct Config { pub struct Config {
pub servers: HashMap <String, Server>, pub servers: HashMap <String, file::Server>,
pub iso: file::Isomorphic, pub iso: file::Isomorphic,
} }
impl TryFrom <file::Server> for Server {
type Error = ConfigError;
fn try_from (f: file::Server) -> Result <Self, Self::Error> {
Ok (Self {
tripcode: *f.tripcode,
display_name: f.display_name,
})
}
}
impl TryFrom <file::Config> for Config { impl TryFrom <file::Config> for Config {
type Error = ConfigError; type Error = ConfigError;
fn try_from (f: file::Config) -> Result <Self, Self::Error> { fn try_from (f: file::Config) -> Result <Self, Self::Error> {
let servers = f.servers.into_iter () let servers = f.servers.into_iter ()
.map (|server| Ok::<_, ConfigError> ((server.name.clone (), server.try_into ()?))); .map (|server| Ok::<_, ConfigError> ((server.name.clone (), server)));
let servers = itertools::process_results (servers, |i| HashMap::from_iter (i))?; let servers = itertools::process_results (servers, |i| HashMap::from_iter (i))?;

View File

@ -62,7 +62,7 @@ pub async fn handle_listen (
error! ("Denied http_listen for non-existent server name {}", watcher_code); error! ("Denied http_listen for non-existent server name {}", watcher_code);
return trip_error (); return trip_error ();
}, },
Some (x) => (*x).tripcode, Some (x) => *(*x).tripcode,
} }
}; };
let actual_tripcode = blake3::hash (api_key); let actual_tripcode = blake3::hash (api_key);