#![warn(missing_docs)]
use actix::dev::ToEnvelope;
use actix::prelude::*;
use thiserror::Error;
pub trait Backend
where
Self: Actor + Handler<Set> + Handler<Get> + Handler<Lock> + Handler<Delete>,
{
type Actor: Actor<Context = <Self as Backend>::Context>
+ Handler<Set>
+ Handler<Get>
+ Handler<Lock>
+ Handler<Delete>;
type Context: ActorContext
+ ToEnvelope<Self::Actor, Get>
+ ToEnvelope<Self::Actor, Set>
+ ToEnvelope<Self::Actor, Lock>
+ ToEnvelope<Self::Actor, Delete>;
}
#[derive(Debug, Error)]
pub enum BackendError {
#[error(transparent)]
InternalError(Box<dyn std::error::Error + Send>),
#[error(transparent)]
ConnectionError(Box<dyn std::error::Error + Send>),
}
#[derive(Message, Debug, Clone, PartialEq)]
#[rtype(result = "Result<Option<Vec<u8>>, BackendError>")]
pub struct Get {
pub key: String,
}
#[derive(Message, Debug, Clone, PartialEq)]
#[rtype(result = "Result<String, BackendError>")]
pub struct Set {
pub key: String,
pub value: Vec<u8>,
pub ttl: Option<u32>,
}
#[derive(Debug, PartialEq)]
pub enum DeleteStatus {
Deleted(u32),
Missing,
}
#[derive(Message, Debug, Clone, PartialEq)]
#[rtype(result = "Result<DeleteStatus, BackendError>")]
pub struct Delete {
pub key: String,
}
#[derive(Message, Debug, Clone, PartialEq)]
#[rtype(result = "Result<LockStatus, BackendError>")]
pub struct Lock {
pub key: String,
pub ttl: u32,
}
#[derive(Debug, PartialEq)]
pub enum LockStatus {
Acquired,
Locked,
}