Request and response

The request type called QrRequest contains data about the QR image:

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct QrRequest {
pub image: Vec<u8>,
}

It implements the Message trait from actix, which is to be set as an associated type of QueueHandler:

impl Message for QrRequest {
type Result = ();
}

The response type is represented by the QrResponse enumeration:

#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum QrResponse {
Succeed(String),
Failed(String),
}

It contains two variants: Succeed for successful results and Failed for errors. It is similar to the Result type of the standard library, but we decided to add our own type to have a chance to override the serialization behavior when we need it. But we can construct this response from a Result instance by implementing the From trait. It's useful because we can use a function to construct a value that returns the Result type. Look at the implementation here:

impl From<Result<String, Error>> for QrResponse {
fn from(res: Result<String, Error>) -> Self {
match res {
Ok(data) => QrResponse::Succeed(data),
Err(err) => QrResponse::Failed(err.to_string()),
}
}
}

QrResponse also has to implement the Message trait:

impl Message for QrResponse {
type Result = ();
}

The library crate is ready to be used to create a worker and a server. Let's start by implementing a worker.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.136.119