use smol_bytes::Utf8Bytes;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum RetiredEngine {
Sqlite,
Postgres,
Mysql,
Mongo,
}
impl RetiredEngine {
pub const ALL: [Self; 4] = [Self::Sqlite, Self::Postgres, Self::Mysql, Self::Mongo];
#[inline]
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Sqlite => "sqlite",
Self::Postgres => "postgres",
Self::Mysql => "mysql",
Self::Mongo => "mongo",
}
}
#[inline]
#[must_use]
pub const fn feature(self) -> &'static str {
match self {
Self::Sqlite => "sqlite",
Self::Postgres => "postgres",
Self::Mysql => "mysql",
Self::Mongo => "mongo",
}
}
}
impl core::str::FromStr for RetiredEngine {
type Err = UnknownRetiredEngine;
fn from_str(word: &str) -> Result<Self, Self::Err> {
Self::ALL
.into_iter()
.find(|engine| engine.as_str() == word)
.ok_or_else(|| UnknownRetiredEngine {
word: Utf8Bytes::from(word),
})
}
}
impl core::fmt::Display for RetiredEngine {
#[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 UnknownRetiredEngine {
word: Utf8Bytes,
}
impl UnknownRetiredEngine {
#[inline]
#[must_use]
pub fn word(&self) -> &str {
self.word.as_str()
}
}
impl core::fmt::Display for UnknownRetiredEngine {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"`{word}` is not an engine the retired ledger builds — it takes `sqlite`, `postgres`, \
`mysql` or `mongo`. A store of your own is not a word here at all: implement \
`RetiredStore` and hand it to `Keeper::new`",
word = self.word,
)
}
}
impl core::error::Error for UnknownRetiredEngine {}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct RetiredOptions {
#[cfg_attr(feature = "serde", serde(default))]
dsn: Option<Utf8Bytes>,
}
impl RetiredOptions {
#[inline]
#[must_use]
pub const fn new() -> Self {
Self { dsn: None }
}
#[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
}
}