⭐ new (ptth_relay): add serde deserializer for blake3 hashes
parent
cc96af6110
commit
9bc4b57058
|
@ -4,18 +4,68 @@
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
convert::{TryFrom, TryInto},
|
convert::{TryFrom, TryInto},
|
||||||
|
fmt,
|
||||||
iter::FromIterator,
|
iter::FromIterator,
|
||||||
|
ops::Deref,
|
||||||
path::Path,
|
path::Path,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use serde::{
|
||||||
|
Deserialize,
|
||||||
|
Deserializer,
|
||||||
|
de::{
|
||||||
|
self,
|
||||||
|
Visitor,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
use crate::errors::ConfigError;
|
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
|
// Stuff we need to load from the config file and use to
|
||||||
// set up the HTTP server
|
// set up the HTTP server
|
||||||
|
|
||||||
pub mod file {
|
pub mod file {
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
#[derive (Deserialize)]
|
#[derive (Deserialize)]
|
||||||
pub struct Server {
|
pub struct Server {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
@ -31,6 +81,8 @@ pub mod file {
|
||||||
pub enable_dev_mode: bool,
|
pub enable_dev_mode: bool,
|
||||||
#[serde (default)]
|
#[serde (default)]
|
||||||
pub enable_scraper_auth: bool,
|
pub enable_scraper_auth: bool,
|
||||||
|
|
||||||
|
pub dev_scraper_key: Option <BlakeHashWrapper>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (Deserialize)]
|
#[derive (Deserialize)]
|
||||||
|
|
Loading…
Reference in New Issue