Skip to content

Commit c1b4570

Browse files
authored
feat(sidekick): support extra modules for Rust (#2515)
Add option and modify mustache templates to support extra modules.
1 parent aa51a8f commit c1b4570

File tree

6 files changed

+43
-12
lines changed

6 files changed

+43
-12
lines changed

internal/sidekick/internal/rust/annotate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ type modelAnnotations struct {
5353
DefaultSystemParameters []systemParameter
5454
// Enables per-service features
5555
PerServiceFeatures bool
56+
// A list of additional modules loaded by the `lib.rs` file.
57+
ExtraModules []string
5658
// If true, at lease one service has a method we cannot wrap (yet).
5759
Incomplete bool
5860
// If true, the generator will produce reference documentation samples for message fields setters.
@@ -568,6 +570,7 @@ func annotateModel(model *api.API, codec *codec) *modelAnnotations {
568570
NotForPublication: codec.doNotPublish,
569571
DisabledRustdocWarnings: codec.disabledRustdocWarnings,
570572
PerServiceFeatures: codec.perServiceFeatures && len(servicesSubset) > 0,
573+
ExtraModules: codec.extraModules,
571574
Incomplete: slices.ContainsFunc(model.Services, func(s *api.Service) bool {
572575
return slices.ContainsFunc(s.Methods, func(m *api.Method) bool { return !codec.generateMethod(m) })
573576
}),

internal/sidekick/internal/rust/annotate_test.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,33 @@ func TestPackageNames(t *testing.T) {
3434
// Override the default name for test APIs ("Test").
3535
model.Name = "workflows-v1"
3636
codec, err := newCodec("protobuf", map[string]string{
37-
"per-service-features": "true",
38-
"copyright-year": "2035",
37+
"version": "1.2.3",
38+
"release-level": "stable",
39+
"copyright-year": "2035",
40+
"per-service-features": "true",
41+
"extra-modules": "operation",
42+
"generate-setter-samples": "true",
43+
"detailed-tracing-attributes": "true",
3944
})
4045
if err != nil {
4146
t.Fatal(err)
4247
}
4348
got := annotateModel(model, codec)
4449
want := &modelAnnotations{
45-
PackageName: "google-cloud-workflows-v1",
46-
PackageNamespace: "google_cloud_workflows_v1",
47-
PackageVersion: "0.0.0",
48-
ReleaseLevel: "preview",
49-
RequiredPackages: []string{},
50-
ExternPackages: []string{},
51-
CopyrightYear: "2035",
52-
Services: []*api.Service{},
53-
NameToLower: "workflows-v1",
54-
PerServiceFeatures: false,
50+
PackageName: "google-cloud-workflows-v1",
51+
PackageVersion: "1.2.3",
52+
ReleaseLevel: "stable",
53+
PackageNamespace: "google_cloud_workflows_v1",
54+
RequiredPackages: []string{},
55+
ExternPackages: []string{},
56+
HasLROs: false,
57+
CopyrightYear: "2035",
58+
Services: []*api.Service{},
59+
NameToLower: "workflows-v1",
60+
PerServiceFeatures: false, // no services
61+
ExtraModules: []string{"operation"},
62+
GenerateSetterSamples: true,
63+
DetailedTracingAttributes: true,
5564
}
5665
if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(modelAnnotations{}, "BoilerPlate")); diff != "" {
5766
t.Errorf("mismatch in modelAnnotations list (-want, +got)\n:%s", diff)

internal/sidekick/internal/rust/codec.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ func newCodec(specificationFormat string, options map[string]string) (*codec, er
144144
return nil, fmt.Errorf("cannot convert `has-veneer` value %q to boolean: %w", definition, err)
145145
}
146146
codec.hasVeneer = value
147+
case key == "extra-modules":
148+
codec.extraModules = strings.Split(definition, ",")
147149
case key == "internal-types":
148150
codec.internalTypes = strings.Split(definition, ",")
149151
case key == "routing-required":
@@ -269,6 +271,8 @@ type codec struct {
269271
detailedTracingAttributes bool
270272
// If true, there is a handwritten client surface.
271273
hasVeneer bool
274+
// Additional modules, maybe with hand-crafted code.
275+
extraModules []string
272276
// A list of types which should only be `pub(crate)`.
273277
//
274278
// In rare cases, it is easiest to manage type visibility via the codec

internal/sidekick/internal/rust/codec_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,15 @@ func TestParseOptions(t *testing.T) {
224224
c.hasVeneer = true
225225
},
226226
},
227+
{
228+
Format: "protobuf",
229+
Options: map[string]string{
230+
"extra-modules": "a,b,c",
231+
},
232+
Update: func(c *codec) {
233+
c.extraModules = []string{"a", "b", "c"}
234+
},
235+
},
227236
{
228237
Format: "protobuf",
229238
Options: map[string]string{

internal/sidekick/internal/rust/templates/crate/src/lib.rs.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,6 @@ pub(crate) mod info {
134134
}
135135

136136
{{/Codec.HasServices}}
137+
{{#Codec.ExtraModules}}
138+
mod {{{.}}};
139+
{{/Codec.ExtraModules}}

internal/sidekick/internal/rust/templates/nosvc/src/lib.rs.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ limitations under the License.
3939
#[allow(deprecated)]
4040
{{/HasDeprecatedEntities}}
4141
pub mod model;
42+
{{#Codec.ExtraModules}}
43+
mod {{{.}}};
44+
{{/Codec.ExtraModules}}

0 commit comments

Comments
 (0)