only use std duration in config

This commit is contained in:
Hannaeko 2022-04-27 19:54:40 +02:00
parent 344b7e07a2
commit a83a099f34
2 changed files with 10 additions and 20 deletions

View file

@ -1,8 +1,7 @@
use std::net::SocketAddr;
use std::time::Duration as StdDuration;
use std::time::Duration;
use serde::{Deserialize, Deserializer};
use chrono::Duration as ChronoDuration;
use crate::models::name::SerdeName;
use crate::dns::TsigAlgorithm;
@ -18,7 +17,7 @@ pub struct Config {
pub struct DnsClientConfig {
pub server: SocketAddr,
#[serde(deserialize_with = "from_std_duration")]
pub timeout: StdDuration,
pub timeout: Duration,
pub tsig: Option<TsigConfig>,
}
@ -33,12 +32,12 @@ pub struct TsigConfig {
#[derive(Debug, Deserialize)]
pub struct WebAppConfig {
#[serde(deserialize_with = "from_chrono_duration")]
pub token_duration: ChronoDuration,
#[serde(deserialize_with = "from_std_duration")]
pub token_duration: Duration,
}
fn from_std_duration<'de, D>(deserializer: D) -> Result<StdDuration, D::Error>
fn from_std_duration<'de, D>(deserializer: D) -> Result<Duration, D::Error>
where D: Deserializer<'de>
{
use serde::de::Error;
@ -46,16 +45,6 @@ fn from_std_duration<'de, D>(deserializer: D) -> Result<StdDuration, D::Error>
.and_then(|string| humantime::parse_duration(&string).map_err(|err| Error::custom(err.to_string())))
}
fn from_chrono_duration<'de, D>(deserializer: D) -> Result<ChronoDuration, D::Error>
where D: Deserializer<'de>
{
use serde::de::Error;
String::deserialize(deserializer)
.and_then(|string| humantime::parse_duration(&string).map_err(|err| Error::custom(err.to_string())))
.and_then(|duration| ChronoDuration::from_std(duration).map_err(|err| Error::custom(err.to_string())))
}
fn from_base64<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where D: Deserializer<'de>
{

View file

@ -1,7 +1,8 @@
use std::time::Duration as StdDuration;
use serde::{Serialize, Deserialize};
use chrono::naive::serde::ts_seconds::serialize as ts_seconds_naive;
use chrono::{Duration, NaiveDateTime, Utc, DateTime};
use chrono::{Duration as ChronoDuration, NaiveDateTime, Utc, DateTime};
use diesel::prelude::*;
use rand::Rng;
use rand::rngs::OsRng;
@ -55,10 +56,10 @@ impl Session {
})
}
pub fn new(conn: &diesel::SqliteConnection, user_info: &UserInfo, token_duration: Duration) -> Result<Session, UserError> {
pub fn new(conn: &diesel::SqliteConnection, user_info: &UserInfo, token_duration: StdDuration) -> Result<Session, UserError> {
use crate::schema::session::dsl::*;
let expires = Utc::now() + token_duration;
let expires = Utc::now() + ChronoDuration::from_std(token_duration).unwrap();
let user_session = Session {
session_id: Session::generate_id(),