From b43106393a8244ad660d1efe1521aea8d70beca9 Mon Sep 17 00:00:00 2001 From: _ <> Date: Sat, 12 Dec 2020 15:50:38 +0000 Subject: [PATCH] :shirt: refactor (ptth_relay): use the new blake3 deserializer --- crates/ptth_relay/src/config.rs | 19 ++++++++++++------- src/tests.rs | 6 ++++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/crates/ptth_relay/src/config.rs b/crates/ptth_relay/src/config.rs index 4e8a707..fac50b2 100644 --- a/crates/ptth_relay/src/config.rs +++ b/crates/ptth_relay/src/config.rs @@ -23,6 +23,16 @@ use crate::errors::ConfigError; pub struct BlakeHashWrapper (blake3::Hash); +impl BlakeHashWrapper { + pub fn from_key (bytes: &[u8]) -> Self { + Self (blake3::hash (bytes)) + } + + pub fn encode_base64 (&self) -> String { + base64::encode (self.as_bytes ()) + } +} + impl Deref for BlakeHashWrapper { type Target = blake3::Hash; @@ -69,7 +79,7 @@ pub mod file { #[derive (Deserialize)] pub struct Server { pub name: String, - pub tripcode: String, + pub tripcode: BlakeHashWrapper, pub display_name: Option , } @@ -110,13 +120,8 @@ 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, + tripcode: *f.tripcode, display_name: f.display_name, }) } diff --git a/src/tests.rs b/src/tests.rs index 993faef..65d94e5 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -18,6 +18,8 @@ fn end_to_end () { use reqwest::Client; use tracing::{debug, info}; + use ptth_relay::config::BlakeHashWrapper; + // Prefer this form for tests, since all tests share one process // and we don't care if another test already installed a subscriber. @@ -28,8 +30,8 @@ fn end_to_end () { rt.block_on (async { let server_name = "aliens_wildland"; let api_key = "AnacondaHardcoverGrannyUnlatchLankinessMutate"; - let tripcode = base64::encode (blake3::hash (api_key.as_bytes ()).as_bytes ()); - debug! ("Relay is expecting tripcode {}", tripcode); + let tripcode = BlakeHashWrapper::from_key (api_key.as_bytes ()); + debug! ("Relay is expecting tripcode {}", tripcode.encode_base64 ()); let config_file = ptth_relay::config::file::Config { port: None, servers: vec! [