1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// SPDX-License-Identifier: MIT
//! Drive `tonic-prost-build` with `type_attribute(...)` injection.
use BTreeMap;
use Message as _;
use FileDescriptorSet;
use crateError;
/// Run `tonic-prost-build` codegen on `fds_bytes`, applying every
/// `type_attribute(<fqn>, <attribute>)` from `type_attributes` before
/// invocation.
///
/// `builder_setup` lets callers configure the builder (server/client
/// flags, additional `type_attribute(...)`, `out_dir`, etc.) before
/// codegen runs. The closure receives a fresh
/// `tonic_prost_build::Builder` and returns it modified.
///
/// `type_attributes` is keyed by **un-dotted** fully-qualified name
/// (`my.v1.Foo`); this helper prepends the leading dot
/// (`.my.v1.Foo`) per `tonic-prost-build`'s convention.
///
/// # Errors
///
/// Returns [`Error::Tonic`] when `tonic-prost-build` codegen fails.
///
/// # Example
///
/// ```ignore
/// use proto_build_kit::{compile_protos, extract_method_string_extension,
/// tonic_prost_build_with_attrs, Stager};
///
/// let staged = Stager::new().with(my_protos::files()).stage()?;
/// let out = compile_protos(&["my/v1/svc.proto"], &[staged.path()])?;
/// let etag_fields = extract_method_string_extension(&out.pool, "envelope.v1.etag_field");
///
/// let mut attrs: std::collections::BTreeMap<String, Vec<String>> = Default::default();
/// for (fqn, etag) in etag_fields {
/// attrs.entry(fqn).or_default().push(format!(
/// "#[derive(::service_kit::Envelope)] #[envelope(etag = \"{etag}\")]"
/// ));
/// }
///
/// tonic_prost_build_with_attrs(&out.fds_bytes, &attrs, |b| {
/// b.build_server(true).build_client(true)
/// })?;
/// ```