Tonic annotate only enums at compile time

444 Views Asked by At

Using tonic_build I can add .type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]”) to annotate my types with e.g. serde.

However is there a way to only annotate enums, that are in the same package as structs.

Example:

syntax = "proto3";

package foo.bar;

message A {
  Enu b = 0;
}

enum Enu {
  FOO = 0;
  BAR = 1;
}

I want in the end, for the generated struct to have serde, which I can already but for the enum to have one more annotation (strum in this case):


#[derive(serde::Serialize, serde::Deserialize)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct A {
    #[prost(enumeration = “Enu", tag = “1")]
    pub b: i32,
}

#[derive(serde::Serialize, serde::Deserialize)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
#[derive(strum::EnumString)] // <— This is what I want added only here
pub enum Enu {
    FOO = 0,
    BAR = 1,
}

Note this was hand-crafted, so I might have missed a character somewhere

0

There are 0 best solutions below