This crate simplifies the work to add extra attributes for prost-build/tonic-build generated code.
Add this to your Cargo.toml
:
```toml [dependencies] sqlx = "..." derive_builder = "..." serde = "..."
[build-dependencies] proto-builder-trait = "0.1" ```
In your build.rs:
```rust use protobuildertrait::prost::BuilderAttributes; use prost_build::Config;
fn main() { Config::default() .outdir(path.path()) .withserde(&["todo.Todo", "todo.TodoStatus"], true, true) .withderivebuilder(&["todo.Todo"]) .withsqlxtype(&["todo.TodoStatus"]) .withderivebuilderinto("todo.Todo", &["id", "title", "status", "description"]) .withderivebuilderoption("todo.Todo", &["createdat", "updatedat"]) .withfieldattributes( &["todo.Todo.createdat", "todo.Todo.updatedat"], &["#[derive(Copy)]"], ) .compile_protos(&["fixtures/protos/todo.proto"], &["fixtures/protos"]) .unwrap(); } ```
This will generate the following code:
```rust
pub struct Todo { #[prost(string, tag="1")] #[builder(setter(into), default)] pub id: ::prost::alloc::string::String, #[prost(string, tag="2")] #[builder(setter(into), default)] pub title: ::prost::alloc::string::String, #[prost(string, tag="3")] #[builder(setter(into), default)] pub description: ::prost::alloc::string::String, #[prost(enumeration="TodoStatus", tag="4")] #[builder(setter(into), default)] pub status: i32, #[prost(message, optional, tag="5")] #[builder(setter(into, stripoption), default)] #[derive(Copy)] pub createdat: ::core::option::Option<::prosttypes::Timestamp>, #[prost(message, optional, tag="6")] #[builder(setter(into, stripoption), default)] #[derive(Copy)] pub updatedat: ::core::option::Option<::prosttypes::Timestamp>, }
pub struct GetTodosRequest { #[prost(string, repeated, tag="1")] pub id: ::prost::alloc::vec::Vec<::prost::alloc::string::String>, }
pub struct CreateTodoRequest { #[prost(string, tag="1")] pub title: ::prost::alloc::string::String, #[prost(string, tag="2")] pub description: ::prost::alloc::string::String, }
pub struct DeleteTodoRequest { #[prost(string, tag="1")] pub id: ::prost::alloc::string::String, }
pub struct DeleteTodoResponse { }
pub enum TodoStatus { Doing = 0, Done = 1, } impl TodoStatus { /// String value of the enum field names used in the ProtoBuf definition. /// /// The values are not transformed in any way and thus are considered stable /// (if the ProtoBuf definition does not change) and safe for programmatic use. pub fn asstrname(&self) -> &'static str { match self { TodoStatus::Doing => "TODOSTATUSDOING", TodoStatus::Done => "TODOSTATUSDONE", } } } ```