use core::num::NonZeroUsize;
use smol_bytes::Utf8Bytes;
pub const DEFAULT_KEEP: NonZeroUsize = NonZeroUsize::new(1).expect("1 is not zero");
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum GenerationsEngine {
Sqlite,
Postgres,
Mysql,
}
impl GenerationsEngine {
pub const ALL: [Self; 3] = [Self::Sqlite, Self::Postgres, Self::Mysql];
#[inline]
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Sqlite => "sqlite",
Self::Postgres => "postgres",
Self::Mysql => "mysql",
}
}
#[inline]
#[must_use]
pub const fn feature(self) -> &'static str {
self.as_str()
}
}
impl core::str::FromStr for GenerationsEngine {
type Err = UnknownGenerationsEngine;
fn from_str(word: &str) -> Result<Self, Self::Err> {
Self::ALL
.into_iter()
.find(|engine| engine.as_str() == word)
.ok_or_else(|| UnknownGenerationsEngine {
word: Utf8Bytes::from(word),
})
}
}
impl core::fmt::Display for GenerationsEngine {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnknownGenerationsEngine {
word: Utf8Bytes,
}
impl UnknownGenerationsEngine {
#[inline]
#[must_use]
pub fn word(&self) -> &str {
self.word.as_str()
}
}
impl core::fmt::Display for UnknownGenerationsEngine {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"`{word}` is not an engine the generations seat builds — it takes `sqlite`, `postgres` or \
`mysql`. A document store sweeps by membership rather than by an anchored `DELETE`, so it is \
a `GenerationsStore` of your own handed to `Curator::new` rather than a word here",
word = self.word,
)
}
}
impl core::error::Error for UnknownGenerationsEngine {}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct GenerationsOptions {
#[cfg_attr(feature = "serde", serde(default))]
dsn: Option<Utf8Bytes>,
#[cfg_attr(feature = "serde", serde(default = "default_keep"))]
keep: NonZeroUsize,
}
impl GenerationsOptions {
#[inline]
#[must_use]
pub const fn new() -> Self {
Self {
dsn: None,
keep: DEFAULT_KEEP,
}
}
#[inline]
#[must_use]
pub fn dsn(&self) -> Option<&str> {
self.dsn.as_ref().map(Utf8Bytes::as_str)
}
#[inline]
#[must_use]
pub fn with_dsn(mut self, dsn: impl Into<Utf8Bytes>) -> Self {
self.set_dsn(Some(dsn.into()));
self
}
#[inline]
pub fn set_dsn(&mut self, dsn: Option<Utf8Bytes>) -> &mut Self {
self.dsn = dsn;
self
}
#[inline]
#[must_use]
pub const fn keep(&self) -> NonZeroUsize {
self.keep
}
#[inline]
#[must_use]
pub const fn with_keep(mut self, keep: NonZeroUsize) -> Self {
self.keep = keep;
self
}
#[inline]
pub const fn set_keep(&mut self, keep: NonZeroUsize) -> &mut Self {
self.keep = keep;
self
}
}
impl Default for GenerationsOptions {
#[inline]
fn default() -> Self {
Self::new()
}
}
fn default_keep() -> NonZeroUsize {
DEFAULT_KEEP
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct GenerationsOverride {
#[cfg_attr(feature = "serde", serde(default))]
keep: Option<NonZeroUsize>,
}
impl GenerationsOverride {
#[inline]
#[must_use]
pub const fn new() -> Self {
Self { keep: None }
}
#[inline]
#[must_use]
pub const fn keep(&self) -> Option<NonZeroUsize> {
self.keep
}
#[inline]
#[must_use]
pub const fn with_keep(mut self, keep: NonZeroUsize) -> Self {
self.keep = Some(keep);
self
}
#[inline]
pub const fn set_keep(&mut self, keep: Option<NonZeroUsize>) -> &mut Self {
self.keep = keep;
self
}
}