⭐ new (ptth_relay): add serde deserializer for blake3 hashes
parent
cc96af6110
commit
9bc4b57058
|
@ -4,18 +4,68 @@
|
|||
use std::{
|
||||
collections::HashMap,
|
||||
convert::{TryFrom, TryInto},
|
||||
fmt,
|
||||
iter::FromIterator,
|
||||
ops::Deref,
|
||||
path::Path,
|
||||
};
|
||||
|
||||
use serde::{
|
||||
Deserialize,
|
||||
Deserializer,
|
||||
de::{
|
||||
self,
|
||||
Visitor,
|
||||
},
|
||||
};
|
||||
|
||||
use crate::errors::ConfigError;
|
||||
|
||||
pub struct BlakeHashWrapper (blake3::Hash);
|
||||
|
||||
impl Deref for BlakeHashWrapper {
|
||||
type Target = blake3::Hash;
|
||||
|
||||
fn deref (&self) -> &<Self as Deref>::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
struct BlakeHashVisitor;
|
||||
|
||||
impl <'de> Visitor <'de> for BlakeHashVisitor {
|
||||
type Value = blake3::Hash;
|
||||
|
||||
fn expecting (&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str ("a 32-byte blake3 hash, encoded as base64")
|
||||
}
|
||||
|
||||
fn visit_str <E: de::Error> (self, value: &str)
|
||||
-> Result <Self::Value, E>
|
||||
{
|
||||
let bytes: Vec <u8> = base64::decode (value).map_err (|_| E::custom (format! ("str is not base64: {}", value)))?;
|
||||
let bytes: [u8; 32] = (&bytes [..]).try_into ().map_err (|_| E::custom (format! ("decode base64 is not 32 bytes long: {}", value)))?;
|
||||
|
||||
let tripcode = blake3::Hash::from (bytes);
|
||||
|
||||
Ok (tripcode)
|
||||
}
|
||||
}
|
||||
|
||||
impl <'de> Deserialize <'de> for BlakeHashWrapper {
|
||||
fn deserialize <D: Deserializer <'de>> (deserializer: D) -> Result <Self, D::Error> {
|
||||
Ok (BlakeHashWrapper (deserializer.deserialize_str (BlakeHashVisitor)?))
|
||||
}
|
||||
}
|
||||
|
||||
// Stuff we need to load from the config file and use to
|
||||
// set up the HTTP server
|
||||
|
||||
pub mod file {
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive (Deserialize)]
|
||||
pub struct Server {
|
||||
pub name: String,
|
||||
|
@ -31,6 +81,8 @@ pub mod file {
|
|||
pub enable_dev_mode: bool,
|
||||
#[serde (default)]
|
||||
pub enable_scraper_auth: bool,
|
||||
|
||||
pub dev_scraper_key: Option <BlakeHashWrapper>,
|
||||
}
|
||||
|
||||
#[derive (Deserialize)]
|
||||
|
|
Loading…
Reference in New Issue