use chrono::Duration;
use serde::{de::DeserializeOwned, Serialize};
use std::borrow::Cow;
use std::hash::Hash;
#[derive(Clone)]
pub struct AuthConfig<Type>
where
Type: Eq + Default + Clone + Send + Sync + Hash + Serialize + DeserializeOwned + 'static,
{
pub(crate) cache: bool,
pub(crate) anonymous_user_id: Option<Type>,
pub(crate) session_id: Cow<'static, str>,
pub(crate) max_age: Duration,
}
impl<Type> std::fmt::Debug for AuthConfig<Type>
where
Type: Eq + Default + Clone + Send + Sync + Hash + Serialize + DeserializeOwned + 'static,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AuthConfig")
.field("cache", &self.cache)
.field("session_id", &self.session_id)
.field("max_age", &self.max_age)
.finish()
}
}
impl<Type> AuthConfig<Type>
where
Type: Eq + Default + Clone + Send + Sync + Hash + Serialize + DeserializeOwned + 'static,
{
#[inline]
pub fn new() -> Self {
Default::default()
}
#[must_use]
pub fn set_cache(mut self, cache: bool) -> Self {
self.cache = cache;
self
}
#[must_use]
pub fn with_anonymous_user_id(mut self, id: Option<Type>) -> Self {
self.anonymous_user_id = id;
self
}
#[must_use]
pub fn with_max_age(mut self, time: Duration) -> Self {
self.max_age = time;
self
}
#[must_use]
pub fn with_session_id(mut self, session_id: impl Into<Cow<'static, str>>) -> Self {
self.session_id = session_id.into();
self
}
}
impl<Type> Default for AuthConfig<Type>
where
Type: Eq + Default + Clone + Send + Sync + Hash + Serialize + DeserializeOwned + 'static,
{
fn default() -> Self {
Self {
cache: true,
session_id: "user_auth_session_id".into(),
max_age: Duration::try_hours(6).unwrap_or_default(),
anonymous_user_id: None,
}
}
}