#[doc(hidden)]
pub mod erased_store;
#[cfg(feature = "memory-store")]
#[doc(hidden)]
pub mod memory_store;
use std::{convert::Infallible, future::Ready, marker::PhantomData};
pub use erased_store::ErasedStore;
#[cfg(feature = "memory-store")]
#[cfg_attr(docsrs, doc(cfg(feature = "memory-store")))]
pub use memory_store::MemoryStore;
use chrono::{DateTime, Utc};
use crate::User;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Session<UserId> {
pub id: u128,
pub expires: DateTime<Utc>,
pub user_id: UserId,
}
pub trait Store: Send + Sync + 'static {
type Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>;
type User: User;
type Get: Future<Output = Result<Option<Session<<Self::User as User>::Id>>, Self::Error>>
+ Send
+ 'static;
type Create: Future<Output = Result<(), CreateSessionError<Self::Error>>> + Send + 'static;
type Revoke: Future<Output = Result<(), Self::Error>> + Send + 'static;
fn get(&self, id: u128) -> Self::Get;
fn create(&self, session: Session<<Self::User as User>::Id>) -> Self::Create;
fn revoke(&self, session: Session<<Self::User as User>::Id>) -> Self::Revoke;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, thiserror::Error)]
pub enum CreateSessionError<T> {
#[error("a session with the specified id already exists")]
AlreadyExists,
#[error(transparent)]
Other(#[from] T),
}
pub struct NoopStore<User>(PhantomData<User>);
impl<User> std::fmt::Debug for NoopStore<User> {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("NoopStore")
.field(&::core::any::type_name::<User>())
.finish()
}
}
impl<User> NoopStore<User> {
#[inline]
pub const fn new() -> Self {
Self(PhantomData)
}
}
impl<User> Default for NoopStore<User> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<User> Store for NoopStore<User>
where
User: crate::User,
{
type Error = Infallible;
type User = User;
type Get = Ready<Result<Option<Session<User::Id>>, Infallible>>;
type Create = Ready<Result<(), CreateSessionError<Infallible>>>;
type Revoke = Ready<Result<(), Infallible>>;
#[inline]
fn get(&self, _id: u128) -> Self::Get {
::core::future::ready(Ok(None))
}
#[inline]
fn create(&self, _session: Session<User::Id>) -> Self::Create {
::core::future::ready(Ok(()))
}
#[inline]
fn revoke(&self, _session: Session<User::Id>) -> Self::Revoke {
::core::future::ready(Ok(()))
}
}