use bytes::Bytes;
use std::convert::Infallible;
use crate::error::Error;
use crate::types::{MetaResponse, StatLine, StatsStream, ValueEntry, ValuesStream};
pub enum Response {
Stored,
NotStored,
Exists,
NotFound,
Deleted,
Touched,
Ok,
Numeric(u64),
Value(ValueEntry),
Values(Vec<ValueEntry>),
ValuesStream(Box<dyn ValuesStream + Send>),
Meta(MetaResponse),
Stats(Vec<StatLine>),
StatsStream(Box<dyn StatsStream + Send>),
Version(Bytes),
Noop,
Error(Error),
}
pub trait IntoResponse {
fn into_response(self) -> Response;
}
impl IntoResponse for Response {
fn into_response(self) -> Response {
self
}
}
impl IntoResponse for Error {
fn into_response(self) -> Response {
Response::Error(self)
}
}
impl IntoResponse for Infallible {
fn into_response(self) -> Response {
match self {}
}
}
impl<T> IntoResponse for Result<T, Error>
where
T: IntoResponse,
{
fn into_response(self) -> Response {
match self {
Ok(value) => value.into_response(),
Err(err) => Response::Error(err),
}
}
}
impl IntoResponse for u64 {
fn into_response(self) -> Response {
Response::Numeric(self)
}
}
impl IntoResponse for ValueEntry {
fn into_response(self) -> Response {
Response::Value(self)
}
}
impl IntoResponse for Vec<ValueEntry> {
fn into_response(self) -> Response {
Response::Values(self)
}
}
impl IntoResponse for MetaResponse {
fn into_response(self) -> Response {
Response::Meta(self)
}
}
impl IntoResponse for Vec<StatLine> {
fn into_response(self) -> Response {
Response::Stats(self)
}
}
#[derive(Debug, Clone, Copy)]
pub struct Stored;
impl IntoResponse for Stored {
fn into_response(self) -> Response {
Response::Stored
}
}
#[derive(Debug, Clone, Copy)]
pub struct NotStored;
impl IntoResponse for NotStored {
fn into_response(self) -> Response {
Response::NotStored
}
}
#[derive(Debug, Clone, Copy)]
pub struct Exists;
impl IntoResponse for Exists {
fn into_response(self) -> Response {
Response::Exists
}
}
#[derive(Debug, Clone, Copy)]
pub struct NotFound;
impl IntoResponse for NotFound {
fn into_response(self) -> Response {
Response::NotFound
}
}
#[derive(Debug, Clone, Copy)]
pub struct Deleted;
impl IntoResponse for Deleted {
fn into_response(self) -> Response {
Response::Deleted
}
}
#[derive(Debug, Clone, Copy)]
pub struct Touched;
impl IntoResponse for Touched {
fn into_response(self) -> Response {
Response::Touched
}
}
#[derive(Debug, Clone, Copy)]
pub struct OkResponse;
impl IntoResponse for OkResponse {
fn into_response(self) -> Response {
Response::Ok
}
}
#[derive(Debug, Clone)]
pub struct Version(pub Bytes);
impl IntoResponse for Version {
fn into_response(self) -> Response {
Response::Version(self.0)
}
}