/prosit

PROST! a Protocol Buffers implementation for the Rust Language

Primary LanguageRustApache License 2.0Apache-2.0

continuous integration

PROSIT

prosit is a Protocol Buffers implementation for the Rust Language, based on prost. prosit extends prost with different code generation options, adding more idiomatic Rust types according to the parse, don't validate philosophy.

To add prosit to your project as an alternative code generator, use the patch feature of cargo, and use prost inside your codebase.

[dependencies]
prost = "0.7"
uuid = "0.8"

[build-dependencies]
tonic-build = { version = "0.4" }

[patch.crates-io]
prost = { git = 'https://github.com/kaiserkarel/prosit' }
prost-derive = { git = 'https://github.com/kaiserkarel/prosit' }
prost-types = { git = 'https://github.com/kaiserkarel/prosit' }
prost-build = { git = 'https://github.com/kaiserkarel/prosit' }

Once prosit exits experimental stage, it will be published on Cargo, but hopefully as an extension to prost

Codegen

Include rust.proto in your protobufs (by copying the file, or vendoring prosit). You can then use the custom codegen options.

syntax = "proto3";

package proto.api.v1;

import "api/rust.proto";

message Data {
  // generates the Uuid type instead of the string type
  string id = 1 [(rust.codegen).type = UUID];
  
  // does not wrap Foo in an option, but instead errors during parsing if foo is missing.
  Foo foo = 2 [(rust.codegen).required = true];
  
  // generates the url type.
  string url = 3 [(rust.codegen).type = URL];
}

message Foo {
  oneof biz {
    option (rust.required) = true;

    string bar = 1;
    string bax = 2;
  }
}

This wil generate approximately the following code:

pub struct Data {
   id: ::uuid::Uuid,
   foo: Foo,
   url: ::url::Url,
}

pub struct Foo {
  biz: Biz,
}

pub enum Biz {
  Bar(String),
  Baz(String),
}