add serialize for server config structs

main
_ 2021-04-27 14:31:58 -05:00
parent 4a73f48c68
commit 8f334f57a3
2 changed files with 35 additions and 2 deletions

View File

@ -15,10 +15,25 @@ use crate::{
},
};
/// Machine-editable configs.
/// These are stored in the `data` directory and shouldn't be touched by
/// humans. ptth_relay will re-write them while it's running.
mod machine_editable {
use serde::{Deserialize, Serialize};
use super::file::Server;
#[derive (Deserialize, Serialize)]
pub struct Config {
pub servers: Option <Vec <Server>>,
}
}
/// Config fields as they are loaded from the config file
pub mod file {
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use crate::key_validity::{
BlakeHashWrapper,
@ -26,7 +41,7 @@ pub mod file {
Valid30Days,
};
#[derive (Deserialize)]
#[derive (Deserialize, Serialize)]
pub struct Server {
/// This is duplicated in the hashmap, but it's not a problem
pub name: String,

View File

@ -12,6 +12,7 @@ use serde::{
},
Deserialize,
Deserializer,
Serialize,
};
#[derive (Copy, Clone, PartialEq, Eq)]
@ -70,6 +71,13 @@ impl <'de> Deserialize <'de> for BlakeHashWrapper {
}
}
impl Serialize for BlakeHashWrapper {
fn serialize <S: serde::Serializer> (&self, serializer: S) -> Result <S::Ok, S::Error>
{
serializer.serialize_str (&self.encode_base64 ())
}
}
pub struct Valid7Days;
pub struct Valid30Days;
//pub struct Valid90Days;
@ -166,9 +174,19 @@ impl <V: MaxValidDuration> ScraperKey <V> {
#[cfg (test)]
mod tests {
use chrono::{Utc};
use serde_json::json;
use super::*;
use KeyValidity::*;
#[test]
fn roundtrip_tripcode () {
let tripcode = "m8vG/sQnsn/87CQ5Ob6wMJeAnMKtXYfCBLNZ1SrSkvI=";
let j = json! ({
"tripcode": tripcode,
});
}
#[test]
fn duration_negative () {
let zero_time = Utc::now ();