pub struct Response { /* private fields */ }
Expand description
Represents a RESP response being built.
This is the buffer in which the response is stored along with a control structure which monitors the nesting of the result and ensures that only valid response will be generated.
Implementations§
source§impl Response
impl Response
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new response.
Creates a new response instance which internally allocates a buffer of 8 kB. Note that this expects a single response element. If several elements are to be output, an Response::array has to be used.
sourcepub fn complete(self) -> Result<BytesMut, OutputError>
pub fn complete(self) -> Result<BytesMut, OutputError>
Completes the request and returns the serialized response as bytes buffer.
Note that this returns an error if the internal nesting is invalid (e.g. an array is missing elements).
As this consumes self, this is the final operation to be performed on a response.
sourcepub fn complete_string(self) -> Result<String, OutputError>
pub fn complete_string(self) -> Result<String, OutputError>
Provides a helper method which directly transforms the response into its string representation.
This is only intended to be used in test environments to verify if a generated response as the expected size and shape.
Note that this does not support responses which contain non UTF-8 data (which are generally supported by RESP).
sourcepub fn array(&mut self, items: i32) -> OutputResult
pub fn array(&mut self, items: i32) -> OutputResult
Starts an array with the given number of items.
Note that this call has to be followed by the exact number of outer output calls in order to generate a valid response.
§Example
Properly building an array:
let mut response = Response::new();
response.array(2)?;
response.simple("Hello")?;
response.simple("World")?;
assert_eq!(response.complete_string()?, "*2\r\n+Hello\r\n+World\r\n");
This will panic as the array is missing elements
let mut response = Response::new();
response.array(3)?;
response.simple("Hello")?;
response.complete_string().unwrap();
sourcepub fn ok(&mut self) -> OutputResult
pub fn ok(&mut self) -> OutputResult
Emits “OK” as simple string.
§Example
let mut response = Response::new();
response.ok()?;
assert_eq!(response.complete_string()?, "+OK\r\n");
sourcepub fn zero(&mut self) -> OutputResult
pub fn zero(&mut self) -> OutputResult
Emits “0” as number.
§Example
let mut response = Response::new();
response.zero()?;
assert_eq!(response.complete_string()?, ":0\r\n");
sourcepub fn one(&mut self) -> OutputResult
pub fn one(&mut self) -> OutputResult
Emits “1” as number.
§Example
let mut response = Response::new();
response.one()?;
assert_eq!(response.complete_string()?, ":1\r\n");
sourcepub fn number(&mut self, number: i64) -> OutputResult
pub fn number(&mut self, number: i64) -> OutputResult
Emits the given number.
§Example
let mut response = Response::new();
response.number(42)?;
assert_eq!(response.complete_string()?, ":42\r\n");
sourcepub fn boolean(&mut self, boolean: bool) -> OutputResult
pub fn boolean(&mut self, boolean: bool) -> OutputResult
Emits “1” if the given value is true or “0” otherwise.
§Example
let mut response = Response::new();
response.boolean(true)?;
assert_eq!(response.complete_string()?, ":1\r\n");
sourcepub fn simple(&mut self, string: impl AsRef<str>) -> OutputResult
pub fn simple(&mut self, string: impl AsRef<str>) -> OutputResult
Emits the given string as simple string.
A simple string is encoded as “+STRING_VALUE”. This requires that the given string is valid UTF-8 and that it does not contain any line breaks (CR or LF). This isn’t enforced by this method. When in doubt, use Response::bulk.
§Example
let mut response = Response::new();
response.simple("Hello World")?;
assert_eq!(response.complete_string()?, "+Hello World\r\n");
sourcepub fn empty_string(&mut self) -> OutputResult
pub fn empty_string(&mut self) -> OutputResult
Emits an empty string.
§Example
let mut response = Response::new();
response.empty_string()?;
assert_eq!(response.complete_string()?, "+\r\n");
sourcepub fn bulk(&mut self, string: impl AsRef<str>) -> OutputResult
pub fn bulk(&mut self, string: impl AsRef<str>) -> OutputResult
Emits the given string as bulk data.
RESP doesn’t provide any requirements for bulk string. These can therefore contain line breaks (CR and or LF) as well as non UTF-8 data (which isn’t supported here).
§Example
let mut response = Response::new();
response.bulk("Hello\nWorld")?;
assert_eq!(response.complete_string()?, "$11\r\nHello\nWorld\r\n");
sourcepub fn error(&mut self, string: impl AsRef<str>) -> OutputResult
pub fn error(&mut self, string: impl AsRef<str>) -> OutputResult
Emits an error message.
Errors are encoded as “-ERROR MESSAGE”. Therefore, just like simple strings, these must not contain line breaks (CR or LF) or non UTF-8 data.
This method will automatically transform CR and LF to “ “ so that we do not double fail (crash when reporting an error).
§Example
let mut response = Response::new();
response.error("Good bye,\ncruel World")?;
assert_eq!(response.complete_string()?, "-Good bye, cruel World\r\n");
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Response
impl RefUnwindSafe for Response
impl Send for Response
impl Sync for Response
impl Unpin for Response
impl UnwindSafe for Response
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more