Struct jupiter::response::Response

source ·
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

source

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.

source

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.

source

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).

source

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();
source

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");
source

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");
source

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");
source

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");
source

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");
source

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");
source

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");
source

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");
source

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§

source§

impl Default for Response

source§

fn default() -> Response

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more