January 2019
Intermediate to advanced
520 pages
14h 32m
English
We need two extra crates: queryst for parsing parameters from the query string, and the serde_cbor crate to support the CBOR serialization format. Add these to your Cargo.toml:
queryst = "2.0" serde_cbor = "0.8"
Also, import them in main.rs:
extern crate queryst;extern crate serde_cbor;
Instead of using serde_json::to_string directly in the handler, we'll move it to a separate function that serializes data depending on the expected format:
fn serialize(format: &str, resp: &RngResponse) -> Result<Vec<u8>, Error> { match format { "json" => { Ok(serde_json::to_vec(resp)?) }, "cbor" => { Ok(serde_cbor::to_vec(resp)?) }, _ => { Err(format_err!("unsupported format {}", format)) }, }}
In this code, we used a match expression to ...
Read now
Unlock full access