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