/warp-protobuf

Protobuf support for the popular Rust web framework warp

Primary LanguageRustMIT LicenseMIT

warp-protobuf

Crates.io Crates.io

Protobuf support for the popular Rust web framework warp

Filter

The function warp_protobuf::body::protobuf() returns a filter that parses the body of a request as a protobuf!

// Generated by PROST!
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct UserRequest {
     #[prost(string, tag = "1")]
     pub name: std::string::String,
 }

let route = warp::body::content_length_limit(1024 * 16)
             .and(warp_protobuf::body::protobuf())
             .map(|user: HelloUser| {
                 format!("Hello {}!\nUserID: {}", user.name, user.id)
             });

Reply

The function warp_protobuf::reply::protobuf() returns an object that impl's warp::Reply so your proto messages are seamlessly encoded and sent over the wire.

use warp::Filter;

// Generated by PROST!
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct UserResponse {
    #[prost(uint64, tag = "1")]
    pub id: u64,
}

// GET /user returns a `200 OK` with a serialized UserResponse
// object.
let route = warp::path("user")
    .map(|| {
        let user = UserResponse { id: 42 };
        warp_protobuf::reply::protobuf(&user)
    });