memcached-async 0.0.2

Asynchronous memcached protocol parser
Documentation
use bytes::Bytes;
use std::convert::Infallible;

use crate::error::Error;
use crate::types::{MetaResponse, StatLine, StatsStream, ValueEntry, ValuesStream};

/// Protocol-agnostic response type.
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),
}

/// Convert handler output into a Response.
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)
    }
}

/// Stored response marker.
#[derive(Debug, Clone, Copy)]
pub struct Stored;

impl IntoResponse for Stored {
    fn into_response(self) -> Response {
        Response::Stored
    }
}

/// Not-stored response marker.
#[derive(Debug, Clone, Copy)]
pub struct NotStored;

impl IntoResponse for NotStored {
    fn into_response(self) -> Response {
        Response::NotStored
    }
}

/// Exists response marker.
#[derive(Debug, Clone, Copy)]
pub struct Exists;

impl IntoResponse for Exists {
    fn into_response(self) -> Response {
        Response::Exists
    }
}

/// Not-found response marker.
#[derive(Debug, Clone, Copy)]
pub struct NotFound;

impl IntoResponse for NotFound {
    fn into_response(self) -> Response {
        Response::NotFound
    }
}

/// Deleted response marker.
#[derive(Debug, Clone, Copy)]
pub struct Deleted;

impl IntoResponse for Deleted {
    fn into_response(self) -> Response {
        Response::Deleted
    }
}

/// Touched response marker.
#[derive(Debug, Clone, Copy)]
pub struct Touched;

impl IntoResponse for Touched {
    fn into_response(self) -> Response {
        Response::Touched
    }
}

/// Ok response marker.
#[derive(Debug, Clone, Copy)]
pub struct OkResponse;

impl IntoResponse for OkResponse {
    fn into_response(self) -> Response {
        Response::Ok
    }
}

/// Version response wrapper.
#[derive(Debug, Clone)]
pub struct Version(pub Bytes);

impl IntoResponse for Version {
    fn into_response(self) -> Response {
        Response::Version(self.0)
    }
}