#tonic-build #prost-build #prost #build #tonic #derive-builder #serde-derive

proto-builder-trait

Builder tools for easily adding attributes for prost-build/tonic-build generated code. serde/sqlx/derive_builder are supported.

10 releases (5 breaking)

0.6.2 Oct 19, 2024
0.6.1 May 4, 2024
0.6.0 Dec 18, 2023
0.5.1 Jan 11, 2023
0.5.0 Oct 29, 2022

#371 in Development tools

Download history 34/week @ 2024-08-19 82/week @ 2024-08-26 113/week @ 2024-09-02 60/week @ 2024-09-09 75/week @ 2024-09-16 109/week @ 2024-09-23 98/week @ 2024-09-30 50/week @ 2024-10-07 283/week @ 2024-10-14 105/week @ 2024-10-21 92/week @ 2024-10-28 64/week @ 2024-11-04 52/week @ 2024-11-11 60/week @ 2024-11-18 61/week @ 2024-11-25 94/week @ 2024-12-02

273 downloads per month

MIT license

53KB
995 lines

Proto Builder Trait

This crate simplifies the work to add extra attributes for prost-build/tonic-build generated code.

Usage

Add this to your Cargo.toml:

[dependencies]
sqlx = "..."
derive_builder = "..."
serde = "..."

[build-dependencies]
proto-builder-trait = "0.1"

In your build.rs:

use proto_builder_trait::prost::BuilderAttributes;
use prost_build::Config;

fn main() {
    Config::default()
        .out_dir(path.path())
        .with_serde(&["todo.Todo", "todo.TodoStatus"], true, true, Some(&[r#"#[serde(rename_all = "camelCase")]"#]))
        .with_derive_builder(&["todo.Todo"], Some(&[r#"#[builder(build_fn(name = "private_build"))]"#]))
        .with_sqlx_type(&["todo.TodoStatus"], None)
        .with_strum(
                &["todo.TodoStatus"],
                Some(&[r#"#[strum(ascii_case_insensitive, serialize_all = "snake_case")]"#]),
            )
        .with_field_attributes(
                &["todo.Todo.created_at", "todo.Todo.updated_at"],
                &["#[derive(Copy)]"],
            )
        .compile_protos(&["fixtures/protos/todo.proto"], &["fixtures/protos"])
        .unwrap();
}

This will generate the following code:

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde_with::serde_as]
#[serde_with::skip_serializing_none]
#[derive(derive_builder::Builder)]
#[builder(setter(into, strip_option), default)]
#[builder(build_fn(name = "private_build"))]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Todo {
    #[prost(string, tag="1")]
    pub id: ::prost::alloc::string::String,
    #[prost(string, tag="2")]
    pub title: ::prost::alloc::string::String,
    #[prost(string, tag="3")]
    pub description: ::prost::alloc::string::String,
    #[prost(enumeration="TodoStatus", tag="4")]
    #[serde_as(as = "DisplayFromStr")]
    pub status: i32,
    #[prost(message, optional, tag="5")]
    #[serde_as(as = "DisplayFromStr")]
    #[derive(Copy)]
    pub created_at: ::core::option::Option<::prost_types::Timestamp>,
    #[prost(message, optional, tag="6")]
    #[derive(Copy)]
    pub updated_at: ::core::option::Option<::prost_types::Timestamp>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct GetTodosRequest {
    #[prost(string, repeated, tag="1")]
    pub id: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CreateTodoRequest {
    #[prost(string, tag="1")]
    pub title: ::prost::alloc::string::String,
    #[prost(string, tag="2")]
    pub description: ::prost::alloc::string::String,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct DeleteTodoRequest {
    #[prost(string, tag="1")]
    pub id: ::prost::alloc::string::String,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct DeleteTodoResponse {
}
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
#[derive(sqlx::Type)]
#[derive(strum::EnumString, strum::Display,strum::EnumIter)]
#[strum(ascii_case_insensitive, serialize_all = "snake_case")]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
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 as_str_name(&self) -> &'static str {
        match self {
            TodoStatus::Doing => "TODO_STATUS_DOING",
            TodoStatus::Done => "TODO_STATUS_DONE",
        }
    }
}

Dependencies

~0–9MB
~97K SLoC