use crate::blob::{Blob, BlobInfo, PutOpts};
use crate::config_store::ConfigStore;
use crate::error::{ForgeError, Result};
use crate::kv::{Kv, SetOpts};
use crate::pubsub::Pubsub;
use crate::queue::{DequeueOpts, EnqueueOpts, Job, JobId, Queue};
use crate::ratelimit::{Decision, FailMode, Limit, RateLimit};
use async_trait::async_trait;
use bytes::Bytes;
use futures_util::stream::{BoxStream, StreamExt};
use serde::Serialize;
use serde::de::DeserializeOwned;
use std::marker::PhantomData;
fn ser<T: Serialize>(value: &T, what: &str) -> Result<Vec<u8>> {
serde_json::to_vec(value)
.map_err(|e| ForgeError::invalid(format!("could not serialize {what}: {e}")))
}
fn de_slice<T: DeserializeOwned>(bytes: &[u8], what: &str) -> Result<T> {
serde_json::from_slice(bytes)
.map_err(|e| ForgeError::invalid(format!("could not deserialize {what}: {e}")))
}
#[derive(Debug, Clone)]
pub struct KvKey<T> {
key: String,
_marker: PhantomData<fn() -> T>,
}
impl<T> KvKey<T> {
pub fn new(key: impl Into<String>) -> Self {
Self {
key: key.into(),
_marker: PhantomData,
}
}
pub fn as_str(&self) -> &str {
&self.key
}
}
#[async_trait]
pub trait KvTyped: Kv {
async fn get_typed<T: DeserializeOwned + Send>(&self, key: &KvKey<T>) -> Result<Option<T>> {
match self.get(key.as_str()).await? {
Some(b) => de_slice(&b, "kv value").map(Some),
None => Ok(None),
}
}
async fn set_typed<T: Serialize + Send + Sync>(
&self,
key: &KvKey<T>,
value: &T,
opts: SetOpts,
) -> Result<bool> {
self.set(key.as_str(), Bytes::from(ser(value, "kv value")?), opts)
.await
}
async fn delete_typed<T: Send + Sync>(&self, key: &KvKey<T>) -> Result<bool> {
self.delete(key.as_str()).await
}
}
impl<K: Kv + ?Sized> KvTyped for K {}
#[derive(Debug, Clone)]
pub struct ConfigKey<T> {
key: String,
default: T,
}
impl<T> ConfigKey<T> {
pub fn new(key: impl Into<String>, default: T) -> Self {
Self {
key: key.into(),
default,
}
}
pub fn as_str(&self) -> &str {
&self.key
}
pub fn default_value(&self) -> &T {
&self.default
}
}
#[async_trait]
pub trait ConfigTyped: ConfigStore {
async fn get_typed<T: DeserializeOwned + Send + Sync>(
&self,
key: &ConfigKey<T>,
) -> Result<Option<T>> {
match self.get_raw(key.as_str()).await? {
Some(raw) => serde_json::from_str(&raw).map(Some).map_err(|e| {
ForgeError::invalid(format!("could not deserialize config value: {e}"))
}),
None => Ok(None),
}
}
async fn get_or_default<T: DeserializeOwned + Clone + Send + Sync>(
&self,
key: &ConfigKey<T>,
) -> Result<T> {
Ok(self
.get_typed(key)
.await?
.unwrap_or_else(|| key.default_value().clone()))
}
async fn set_typed<T: Serialize + Send + Sync>(
&self,
key: &ConfigKey<T>,
value: &T,
) -> Result<()> {
let raw = serde_json::to_string(value)
.map_err(|e| ForgeError::invalid(format!("could not serialize config value: {e}")))?;
self.set_raw(key.as_str(), &raw).await
}
}
impl<C: ConfigStore + ?Sized> ConfigTyped for C {}
#[derive(Debug, Clone, Copy)]
pub struct QueueName<T> {
name: &'static str,
_marker: PhantomData<fn() -> T>,
}
impl<T> QueueName<T> {
pub const fn new(name: &'static str) -> Self {
Self {
name,
_marker: PhantomData,
}
}
pub const fn as_str(&self) -> &'static str {
self.name
}
}
pub trait QueuePayload: Serialize + DeserializeOwned + Send + Sync + Sized {
const QUEUE: QueueName<Self>;
const MAX_ATTEMPTS: u32 = 5;
fn enqueue_opts() -> EnqueueOpts {
EnqueueOpts::new().with_max_attempts(Self::MAX_ATTEMPTS)
}
}
#[derive(Debug, Clone)]
pub struct TypedJob<P> {
pub payload: P,
job: Job,
}
impl<P> TypedJob<P> {
pub fn job(&self) -> &Job {
&self.job
}
}
#[async_trait]
pub trait QueueTyped: Queue {
async fn enqueue_typed<P: QueuePayload>(&self, payload: &P) -> Result<JobId> {
self.enqueue_typed_with(payload, P::enqueue_opts()).await
}
async fn enqueue_typed_with<P: QueuePayload>(
&self,
payload: &P,
opts: EnqueueOpts,
) -> Result<JobId> {
self.enqueue(
P::QUEUE.as_str(),
Bytes::from(ser(payload, "queue payload")?),
opts,
)
.await
}
async fn dequeue_typed<P: QueuePayload>(
&self,
opts: DequeueOpts,
) -> Result<Option<TypedJob<P>>> {
match self.dequeue(P::QUEUE.as_str(), opts).await? {
Some(job) => {
let payload = de_slice::<P>(&job.payload, "queue payload")?;
Ok(Some(TypedJob { payload, job }))
}
None => Ok(None),
}
}
}
impl<Q: Queue + ?Sized> QueueTyped for Q {}
pub trait RateSubject {
fn rate_subject(&self) -> String;
}
impl<T: std::fmt::Display + ?Sized> RateSubject for T {
fn rate_subject(&self) -> String {
self.to_string()
}
}
pub struct RateBucket<S: ?Sized> {
bucket: &'static str,
limit: Limit,
fail: FailMode,
_marker: PhantomData<fn(&S)>,
}
impl<S: RateSubject + ?Sized> RateBucket<S> {
pub const fn new(bucket: &'static str, limit: Limit, fail: FailMode) -> Self {
Self {
bucket,
limit,
fail,
_marker: PhantomData,
}
}
pub async fn check(&self, rl: &dyn RateLimit, subject: &S) -> Result<Decision> {
rl.check_with(self.bucket, &subject.rate_subject(), self.limit, self.fail)
.await
}
}
#[derive(Debug, Clone)]
pub struct BlobKey<K> {
key: String,
_marker: PhantomData<fn() -> K>,
}
impl<K> BlobKey<K> {
pub fn new(key: impl Into<String>) -> Self {
Self {
key: key.into(),
_marker: PhantomData,
}
}
pub fn as_str(&self) -> &str {
&self.key
}
pub async fn put(&self, blob: &dyn Blob, data: Bytes, opts: PutOpts) -> Result<()> {
blob.put(&self.key, data, opts).await
}
pub async fn get(&self, blob: &dyn Blob) -> Result<Option<Bytes>> {
blob.get(&self.key).await
}
pub async fn head(&self, blob: &dyn Blob) -> Result<Option<BlobInfo>> {
blob.head(&self.key).await
}
pub async fn delete(&self, blob: &dyn Blob) -> Result<()> {
blob.delete(&self.key).await
}
}
#[derive(Debug, Clone)]
pub struct Topic<E> {
topic: String,
_marker: PhantomData<fn() -> E>,
}
impl<E> Topic<E> {
pub fn new(topic: impl Into<String>) -> Self {
Self {
topic: topic.into(),
_marker: PhantomData,
}
}
pub fn as_str(&self) -> &str {
&self.topic
}
}
pub type TypedSubscription<E> = BoxStream<'static, Result<E>>;
#[async_trait]
pub trait PubsubTyped: Pubsub {
async fn publish_typed<E: Serialize + Send + Sync>(
&self,
topic: &Topic<E>,
event: &E,
) -> Result<()> {
self.publish(topic.as_str(), Bytes::from(ser(event, "event")?))
.await
}
async fn subscribe_typed<E: DeserializeOwned + 'static>(
&self,
topic: &Topic<E>,
) -> Result<TypedSubscription<E>> {
let sub = self.subscribe(topic.as_str()).await?;
Ok(sub
.map(|item| item.and_then(|bytes| de_slice::<E>(&bytes, "event")))
.boxed())
}
}
impl<P: Pubsub + ?Sized> PubsubTyped for P {}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
use serde::Deserialize;
use std::time::Duration;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct SendEmail {
to: String,
}
impl QueuePayload for SendEmail {
const QUEUE: QueueName<Self> = QueueName::new("emails");
const MAX_ATTEMPTS: u32 = 3;
}
#[test]
fn queue_payload_binds_name_and_defaults() {
assert_eq!(SendEmail::QUEUE.as_str(), "emails");
assert_eq!(SendEmail::enqueue_opts().max_attempts, 3);
}
#[test]
fn keys_carry_their_strings() {
let k: KvKey<u64> = KvKey::new("counter:1");
assert_eq!(k.as_str(), "counter:1");
let c = ConfigKey::new("max_upload", 1024u64);
assert_eq!(c.as_str(), "max_upload");
assert_eq!(*c.default_value(), 1024);
let t: Topic<SendEmail> = Topic::new("emails.events");
assert_eq!(t.as_str(), "emails.events");
let b: BlobKey<()> = BlobKey::new("avatars/u1.png");
assert_eq!(b.as_str(), "avatars/u1.png");
}
#[test]
fn rate_bucket_is_const_constructible() {
const LOGIN: RateBucket<str> = RateBucket::new(
"login",
Limit {
max: 5,
per: Duration::from_secs(60),
algo: crate::ratelimit::Algo::TokenBucket,
},
FailMode::Closed,
);
assert_eq!(LOGIN.bucket, "login");
}
}