Skip to content

Commit f9ff352

Browse files
authored
refactor(internal/sidekick/rust): remove config package dependency (#3809)
The Generate and GenerateStorage functions now accept the specification format and codec options directly instead of a *config.Config struct. This decouples the rust package from the sidekick config package, so that we can delete that package later on.
1 parent 9c6f9c6 commit f9ff352

File tree

4 files changed

+22
-23
lines changed

4 files changed

+22
-23
lines changed

internal/librarian/rust/generate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func Generate(ctx context.Context, library *config.Library, sources *Sources) er
6868
return err
6969
}
7070
}
71-
if err := sidekickrust.Generate(ctx, model, library.Output, sidekickConfig); err != nil {
71+
if err := sidekickrust.Generate(ctx, model, library.Output, sidekickConfig.General.SpecificationFormat, sidekickConfig.Codec); err != nil {
7272
return err
7373
}
7474
if !exists {
@@ -110,7 +110,7 @@ func generateVeneer(ctx context.Context, library *config.Library, sources *Sourc
110110
}
111111
switch sidekickConfig.General.Language {
112112
case "rust":
113-
err = sidekickrust.Generate(ctx, model, module.Output, sidekickConfig)
113+
err = sidekickrust.Generate(ctx, model, module.Output, sidekickConfig.General.SpecificationFormat, sidekickConfig.Codec)
114114
case "rust_storage":
115115
return generateRustStorage(ctx, library, module.Output, sources)
116116
case "rust+prost":
@@ -212,7 +212,7 @@ func generateRustStorage(ctx context.Context, library *config.Library, moduleOut
212212
return fmt.Errorf("failed to create control model: %w", err)
213213
}
214214

215-
return sidekickrust.GenerateStorage(ctx, moduleOutput, storageModel, storageConfig, controlModel, controlConfig)
215+
return sidekickrust.GenerateStorage(ctx, moduleOutput, storageModel, storageConfig.General.SpecificationFormat, storageConfig.Codec, controlModel, controlConfig.General.SpecificationFormat, controlConfig.Codec)
216216
}
217217

218218
func findModuleByOutput(library *config.Library, output string) *config.RustModule {

internal/sidekick/rust/generate.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,32 @@ import (
2020
"path/filepath"
2121

2222
"github.com/googleapis/librarian/internal/sidekick/api"
23-
"github.com/googleapis/librarian/internal/sidekick/config"
2423
"github.com/googleapis/librarian/internal/sidekick/language"
2524
)
2625

2726
//go:embed all:templates
2827
var templates embed.FS
2928

3029
// Generate generates Rust code from the model.
31-
func Generate(ctx context.Context, model *api.API, outdir string, cfg *config.Config) error {
32-
codec, err := newCodec(cfg.General.SpecificationFormat, cfg.Codec)
30+
func Generate(ctx context.Context, model *api.API, outdir, specFormat string, codec map[string]string) error {
31+
c, err := newCodec(specFormat, codec)
3332
if err != nil {
3433
return err
3534
}
36-
annotations := annotateModel(model, codec)
35+
annotations := annotateModel(model, c)
3736
provider := templatesProvider()
38-
generatedFiles := codec.generatedFiles(annotations.HasServices())
37+
generatedFiles := c.generatedFiles(annotations.HasServices())
3938
return language.GenerateFromModel(outdir, model, provider, generatedFiles)
4039
}
4140

4241
// GenerateStorage generates Rust code for the storage service.
43-
func GenerateStorage(ctx context.Context, outdir string, storageModel *api.API, storageConfig *config.Config, controlModel *api.API, controlConfig *config.Config) error {
44-
storageCodec, err := newCodec(storageConfig.General.SpecificationFormat, storageConfig.Codec)
42+
func GenerateStorage(ctx context.Context, outdir string, storageModel *api.API, storageSpecFormat string, storageCodecOpts map[string]string, controlModel *api.API, controlSpecFormat string, controlCodecOpts map[string]string) error {
43+
storageCodec, err := newCodec(storageSpecFormat, storageCodecOpts)
4544
if err != nil {
4645
return err
4746
}
4847
annotateModel(storageModel, storageCodec)
49-
controlCodec, err := newCodec(controlConfig.General.SpecificationFormat, controlConfig.Codec)
48+
controlCodec, err := newCodec(controlSpecFormat, controlCodecOpts)
5049
if err != nil {
5150
return err
5251
}

internal/sidekick/rust/generate_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ func TestCodecError(t *testing.T) {
9999
if err != nil {
100100
t.Fatal(err)
101101
}
102-
if err := Generate(t.Context(), model, outDir, errorConfig); err == nil {
102+
if err := Generate(t.Context(), model, outDir, errorConfig.General.SpecificationFormat, errorConfig.Codec); err == nil {
103103
t.Errorf("expected an error with invalid Codec options")
104104
}
105105

106-
if err := GenerateStorage(t.Context(), outDir, model, errorConfig, model, goodConfig); err == nil {
106+
if err := GenerateStorage(t.Context(), outDir, model, errorConfig.General.SpecificationFormat, errorConfig.Codec, model, goodConfig.General.SpecificationFormat, goodConfig.Codec); err == nil {
107107
t.Errorf("expected an error with invalid Codec options for storage")
108108
}
109-
if err := GenerateStorage(t.Context(), outDir, model, goodConfig, model, errorConfig); err == nil {
109+
if err := GenerateStorage(t.Context(), outDir, model, goodConfig.General.SpecificationFormat, goodConfig.Codec, model, errorConfig.General.SpecificationFormat, errorConfig.Codec); err == nil {
110110
t.Errorf("expected an error with invalid Codec options for control")
111111
}
112112
}
@@ -126,7 +126,7 @@ func TestRustFromOpenAPI(t *testing.T) {
126126
if err != nil {
127127
t.Fatal(err)
128128
}
129-
if err := Generate(t.Context(), model, outDir, cfg); err != nil {
129+
if err := Generate(t.Context(), model, outDir, cfg.General.SpecificationFormat, cfg.Codec); err != nil {
130130
t.Fatal(err)
131131
}
132132
for _, expected := range expectedInCrate {
@@ -159,7 +159,7 @@ func TestRustFromDiscovery(t *testing.T) {
159159
if err != nil {
160160
t.Fatal(err)
161161
}
162-
if err := Generate(t.Context(), model, outDir, cfg); err != nil {
162+
if err := Generate(t.Context(), model, outDir, cfg.General.SpecificationFormat, cfg.Codec); err != nil {
163163
t.Fatal(err)
164164
}
165165
for _, expected := range expectedInCrate {
@@ -194,7 +194,7 @@ func TestRustFromProtobuf(t *testing.T) {
194194
if err != nil {
195195
t.Fatal(err)
196196
}
197-
if err := Generate(t.Context(), model, outDir, cfg); err != nil {
197+
if err := Generate(t.Context(), model, outDir, cfg.General.SpecificationFormat, cfg.Codec); err != nil {
198198
t.Fatal(err)
199199
}
200200
for _, expected := range expectedInCrate {
@@ -233,7 +233,7 @@ func TestRustClient(t *testing.T) {
233233
if err != nil {
234234
t.Fatal(err)
235235
}
236-
if err := Generate(t.Context(), model, outDir, cfg); err != nil {
236+
if err := Generate(t.Context(), model, outDir, cfg.General.SpecificationFormat, cfg.Codec); err != nil {
237237
t.Fatal(err)
238238
}
239239
for _, expected := range expectedInClient {
@@ -278,7 +278,7 @@ func TestRustNosvc(t *testing.T) {
278278
if err != nil {
279279
t.Fatal(err)
280280
}
281-
if err := Generate(t.Context(), model, outDir, cfg); err != nil {
281+
if err := Generate(t.Context(), model, outDir, cfg.General.SpecificationFormat, cfg.Codec); err != nil {
282282
t.Fatal(err)
283283
}
284284
for _, expected := range expectedInNosvc {
@@ -316,7 +316,7 @@ func TestRustModuleRpc(t *testing.T) {
316316
if err != nil {
317317
t.Fatal(err)
318318
}
319-
if err := Generate(t.Context(), model, path.Join(outDir, "rpc"), cfg); err != nil {
319+
if err := Generate(t.Context(), model, path.Join(outDir, "rpc"), cfg.General.SpecificationFormat, cfg.Codec); err != nil {
320320
t.Fatal(err)
321321
}
322322

@@ -356,7 +356,7 @@ func TestRustBootstrapWkt(t *testing.T) {
356356
if err != nil {
357357
t.Fatal(err)
358358
}
359-
if err := Generate(t.Context(), model, path.Join(outDir, "wkt"), cfg); err != nil {
359+
if err := Generate(t.Context(), model, path.Join(outDir, "wkt"), cfg.General.SpecificationFormat, cfg.Codec); err != nil {
360360
t.Fatal(err)
361361
}
362362

internal/sidekick/sidekick/refresh.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func refreshDir(ctx context.Context, rootConfig *config.Config, cmdLine *Command
7878

7979
switch config.General.Language {
8080
case "rust":
81-
return rust.Generate(ctx, model, output, config)
81+
return rust.Generate(ctx, model, output, config.General.SpecificationFormat, config.Codec)
8282
case "rust_storage":
8383
// The StorageControl client depends on multiple specification sources.
8484
// We load them both here manually, and pass them along to
@@ -91,7 +91,7 @@ func refreshDir(ctx context.Context, rootConfig *config.Config, cmdLine *Command
9191
if err != nil {
9292
return err
9393
}
94-
return rust.GenerateStorage(ctx, output, storageModel, storageConfig, controlModel, controlConfig)
94+
return rust.GenerateStorage(ctx, output, storageModel, storageConfig.General.SpecificationFormat, storageConfig.Codec, controlModel, controlConfig.General.SpecificationFormat, controlConfig.Codec)
9595
case "rust+prost":
9696
return rust_prost.Generate(ctx, model, output, config)
9797
case "dart":

0 commit comments

Comments
 (0)