Skip to main content

memcached_async/
response.rs

1use bytes::Bytes;
2use std::convert::Infallible;
3
4use crate::error::Error;
5use crate::types::{MetaResponse, StatLine, StatsStream, ValueEntry, ValuesStream};
6
7/// Protocol-agnostic response type.
8pub enum Response {
9    Stored,
10    NotStored,
11    Exists,
12    NotFound,
13    Deleted,
14    Touched,
15    Numeric(u64),
16    Value(ValueEntry),
17    Values(Vec<ValueEntry>),
18    ValuesStream(Box<dyn ValuesStream + Send>),
19    Meta(MetaResponse),
20    Stats(Vec<StatLine>),
21    StatsStream(Box<dyn StatsStream + Send>),
22    Version(Bytes),
23    Noop,
24    Error(Error),
25}
26
27/// Convert handler output into a Response.
28pub trait IntoResponse {
29    fn into_response(self) -> Response;
30}
31
32impl IntoResponse for Response {
33    fn into_response(self) -> Response {
34        self
35    }
36}
37
38impl IntoResponse for Error {
39    fn into_response(self) -> Response {
40        Response::Error(self)
41    }
42}
43
44impl IntoResponse for Infallible {
45    fn into_response(self) -> Response {
46        match self {}
47    }
48}
49
50impl<T> IntoResponse for Result<T, Error>
51where
52    T: IntoResponse,
53{
54    fn into_response(self) -> Response {
55        match self {
56            Ok(value) => value.into_response(),
57            Err(err) => Response::Error(err),
58        }
59    }
60}
61
62impl IntoResponse for u64 {
63    fn into_response(self) -> Response {
64        Response::Numeric(self)
65    }
66}
67
68impl IntoResponse for ValueEntry {
69    fn into_response(self) -> Response {
70        Response::Value(self)
71    }
72}
73
74impl IntoResponse for Vec<ValueEntry> {
75    fn into_response(self) -> Response {
76        Response::Values(self)
77    }
78}
79
80impl IntoResponse for MetaResponse {
81    fn into_response(self) -> Response {
82        Response::Meta(self)
83    }
84}
85
86impl IntoResponse for Vec<StatLine> {
87    fn into_response(self) -> Response {
88        Response::Stats(self)
89    }
90}
91
92/// Stored response marker.
93#[derive(Debug, Clone, Copy)]
94pub struct Stored;
95
96impl IntoResponse for Stored {
97    fn into_response(self) -> Response {
98        Response::Stored
99    }
100}
101
102/// Not-stored response marker.
103#[derive(Debug, Clone, Copy)]
104pub struct NotStored;
105
106impl IntoResponse for NotStored {
107    fn into_response(self) -> Response {
108        Response::NotStored
109    }
110}
111
112/// Exists response marker.
113#[derive(Debug, Clone, Copy)]
114pub struct Exists;
115
116impl IntoResponse for Exists {
117    fn into_response(self) -> Response {
118        Response::Exists
119    }
120}
121
122/// Not-found response marker.
123#[derive(Debug, Clone, Copy)]
124pub struct NotFound;
125
126impl IntoResponse for NotFound {
127    fn into_response(self) -> Response {
128        Response::NotFound
129    }
130}
131
132/// Deleted response marker.
133#[derive(Debug, Clone, Copy)]
134pub struct Deleted;
135
136impl IntoResponse for Deleted {
137    fn into_response(self) -> Response {
138        Response::Deleted
139    }
140}
141
142/// Touched response marker.
143#[derive(Debug, Clone, Copy)]
144pub struct Touched;
145
146impl IntoResponse for Touched {
147    fn into_response(self) -> Response {
148        Response::Touched
149    }
150}
151
152/// Version response wrapper.
153#[derive(Debug, Clone)]
154pub struct Version(pub Bytes);
155
156impl IntoResponse for Version {
157    fn into_response(self) -> Response {
158        Response::Version(self.0)
159    }
160}