Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions example/build-with-buf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ version = "0.1.0"
edition = "2021"

[features]
default = ["proto_full"]
default = ["proto_full", "serde"]
serde = ["dep:serde"]
# @@protoc_deletion_point(features)
# This section is automatically generated by protoc-gen-prost-crate.
# Changes in this area may be lost on regeneration.
Expand All @@ -17,7 +18,7 @@ bytes = "1.10.1"
prost = "0.14.1"
pbjson = "0.8.0"
pbjson-types = "0.8.0"
serde = "1.0.228"
serde = { version = "1.0.228", optional = true }
tonic = { version = "0.14.2", features = ["gzip"] }
tonic-prost = "0.14.2"

Expand Down
2 changes: 2 additions & 0 deletions example/build-with-buf/buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ plugins:
- local: protoc-gen-prost-serde
#path: ../../target/debug/protoc-gen-prost-serde
out: src/gen
opt:
- feature
- local: protoc-gen-tonic
#path: ../../target/debug/protoc-gen-tonic
out: src/gen
Expand Down
3 changes: 3 additions & 0 deletions protoc-gen-prost-serde/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ In addition, the following options can also be specified:
by `protoc-gen-prost`. This behavior may be desired if this plugin is run
in a separate `protoc` invocation and you encounter a `Tried to insert into
file that doesn't exist` error.
* `feature(=<string>)`: Adds feature gate to all generated includes. If `no_include`
is set, this option has no effect. If the `<string>` value is not specified,
default feature name `"serde"` will be used.

A note on parameter values:

Expand Down
13 changes: 12 additions & 1 deletion protoc-gen-prost-serde/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct PbJsonGenerator {
builder: pbjson_build::Builder,
prefixes: Vec<String>,
insert_include: bool,
feature_name: Option<String>,
}

impl Generator for PbJsonGenerator {
Expand All @@ -27,6 +28,11 @@ impl Generator for PbJsonGenerator {

if self.insert_include {
res.push(request.append_to_file(|buf| {
if let Some(feature_name) = &self.feature_name {
buf.push_str("#[cfg(feature = \"");
buf.push_str(feature_name);
buf.push_str("\")]\n");
}
buf.push_str("include!(\"");
buf.push_str(&output_filename);
buf.push_str("\");\n");
Expand Down Expand Up @@ -55,11 +61,16 @@ impl Generator for PbJsonGenerator {
}

impl PbJsonGenerator {
pub fn new(builder: pbjson_build::Builder, insert_include: bool) -> Self {
pub fn new(
builder: pbjson_build::Builder,
insert_include: bool,
feature_name: Option<String>,
) -> Self {
Self {
builder,
prefixes: vec![".".to_owned()],
insert_include,
feature_name,
}
}
}
14 changes: 13 additions & 1 deletion protoc-gen-prost-serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ pub fn execute(raw_request: &[u8]) -> protoc_gen_prost::Result {
params.flat_output_dir,
)?;

let files = PbJsonGenerator::new(builder, !params.no_include).generate(&module_request_set)?;
let files = PbJsonGenerator::new(builder, !params.no_include, params.feature)
.generate(&module_request_set)?;

Ok(files)
}
Expand All @@ -49,6 +50,7 @@ struct Parameters {
btree_map: Vec<String>,
flat_output_dir: bool,
exclude: Vec<String>,
feature: Option<String>,
}

impl Parameters {
Expand Down Expand Up @@ -93,6 +95,9 @@ impl Parameters {
}
}

/// This name will be used as a gate feature name when "feature" parameter is passed without value
const DEFAULT_FEATURE_NAME: &str = "serde";

impl str::FromStr for Parameters {
type Err = InvalidParameter;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand Down Expand Up @@ -184,6 +189,13 @@ impl str::FromStr for Parameters {
param: "exclude",
value: prefix,
} => ret_val.exclude.push(prefix.to_string()),
Param::Parameter { param: "feature" } => {
ret_val.feature = Some(DEFAULT_FEATURE_NAME.to_string())
}
Param::Value {
param: "feature",
value,
} => ret_val.feature = Some(value.to_string()),
_ => return Err(InvalidParameter::from(param)),
}
}
Expand Down
Loading