|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package rust |
| 16 | + |
| 17 | +import ( |
| 18 | + "strings" |
| 19 | + |
| 20 | + "github.com/googleapis/librarian/internal/sideflip/config" |
| 21 | + sidekickconfig "github.com/googleapis/librarian/internal/sidekick/config" |
| 22 | +) |
| 23 | + |
| 24 | +func toSidekickConfig(library *config.Library, googleapisDir, serviceConfig string) *sidekickconfig.Config { |
| 25 | + sidekickCfg := &sidekickconfig.Config{ |
| 26 | + General: sidekickconfig.GeneralConfig{ |
| 27 | + Language: "rust", |
| 28 | + SpecificationFormat: "protobuf", |
| 29 | + ServiceConfig: serviceConfig, |
| 30 | + SpecificationSource: library.Channel, |
| 31 | + }, |
| 32 | + Source: map[string]string{ |
| 33 | + "googleapis-root": googleapisDir, |
| 34 | + }, |
| 35 | + Codec: buildCodec(library), |
| 36 | + } |
| 37 | + |
| 38 | + if library.Rust != nil { |
| 39 | + if len(library.Rust.DocumentationOverrides) > 0 { |
| 40 | + sidekickCfg.CommentOverrides = make([]sidekickconfig.DocumentationOverride, len(library.Rust.DocumentationOverrides)) |
| 41 | + for i, override := range library.Rust.DocumentationOverrides { |
| 42 | + sidekickCfg.CommentOverrides[i] = sidekickconfig.DocumentationOverride{ |
| 43 | + ID: override.ID, |
| 44 | + Match: override.Match, |
| 45 | + Replace: override.Replace, |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if len(library.Rust.PaginationOverrides) > 0 { |
| 51 | + sidekickCfg.PaginationOverrides = make([]sidekickconfig.PaginationOverride, len(library.Rust.PaginationOverrides)) |
| 52 | + for i, override := range library.Rust.PaginationOverrides { |
| 53 | + sidekickCfg.PaginationOverrides[i] = sidekickconfig.PaginationOverride{ |
| 54 | + ID: override.ID, |
| 55 | + ItemField: override.ItemField, |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + return sidekickCfg |
| 61 | +} |
| 62 | + |
| 63 | +func buildCodec(library *config.Library) map[string]string { |
| 64 | + codec := make(map[string]string) |
| 65 | + if library.Version != "" { |
| 66 | + codec["version"] = library.Version |
| 67 | + } |
| 68 | + if library.ReleaseLevel != "" { |
| 69 | + codec["release-level"] = library.ReleaseLevel |
| 70 | + } |
| 71 | + if library.Name != "" { |
| 72 | + codec["package-name-override"] = library.Name |
| 73 | + } |
| 74 | + if library.CopyrightYear != "" { |
| 75 | + codec["copyright-year"] = library.CopyrightYear |
| 76 | + } |
| 77 | + if library.Rust == nil { |
| 78 | + return codec |
| 79 | + } |
| 80 | + |
| 81 | + rust := library.Rust |
| 82 | + if rust.ModulePath != "" { |
| 83 | + codec["module-path"] = rust.ModulePath |
| 84 | + } |
| 85 | + if library.Publish != nil && library.Publish.Disabled { |
| 86 | + codec["not-for-publication"] = "true" |
| 87 | + } |
| 88 | + if len(rust.DisabledRustdocWarnings) > 0 { |
| 89 | + codec["disabled-rustdoc-warnings"] = strings.Join(rust.DisabledRustdocWarnings, ",") |
| 90 | + } |
| 91 | + if len(rust.DisabledClippyWarnings) > 0 { |
| 92 | + codec["disabled-clippy-warnings"] = strings.Join(rust.DisabledClippyWarnings, ",") |
| 93 | + } |
| 94 | + if rust.TemplateOverride != "" { |
| 95 | + codec["template-override"] = rust.TemplateOverride |
| 96 | + } |
| 97 | + if rust.IncludeGrpcOnlyMethods { |
| 98 | + codec["include-grpc-only-methods"] = "true" |
| 99 | + } |
| 100 | + if rust.PerServiceFeatures { |
| 101 | + codec["per-service-features"] = "true" |
| 102 | + } |
| 103 | + if len(rust.DefaultFeatures) > 0 { |
| 104 | + codec["default-features"] = strings.Join(rust.DefaultFeatures, ",") |
| 105 | + } |
| 106 | + if rust.DetailedTracingAttributes { |
| 107 | + codec["detailed-tracing-attributes"] = "true" |
| 108 | + } |
| 109 | + if rust.HasVeneer { |
| 110 | + codec["has-veneer"] = "true" |
| 111 | + } |
| 112 | + if len(rust.ExtraModules) > 0 { |
| 113 | + codec["extra-modules"] = strings.Join(rust.ExtraModules, ",") |
| 114 | + } |
| 115 | + if rust.RoutingRequired { |
| 116 | + codec["routing-required"] = "true" |
| 117 | + } |
| 118 | + if rust.GenerateSetterSamples { |
| 119 | + codec["generate-setter-samples"] = "true" |
| 120 | + } |
| 121 | + |
| 122 | + for _, dep := range rust.PackageDependencies { |
| 123 | + codec["package:"+dep.Name] = formatPackageDependency(dep) |
| 124 | + } |
| 125 | + return codec |
| 126 | +} |
| 127 | + |
| 128 | +func formatPackageDependency(dep config.RustPackageDependency) string { |
| 129 | + var parts []string |
| 130 | + if dep.Package != "" { |
| 131 | + parts = append(parts, "package="+dep.Package) |
| 132 | + } |
| 133 | + if dep.Source != "" { |
| 134 | + parts = append(parts, "source="+dep.Source) |
| 135 | + } |
| 136 | + if dep.ForceUsed { |
| 137 | + parts = append(parts, "force-used=true") |
| 138 | + } |
| 139 | + if dep.UsedIf != "" { |
| 140 | + parts = append(parts, "used-if="+dep.UsedIf) |
| 141 | + } |
| 142 | + if dep.Feature != "" { |
| 143 | + parts = append(parts, "feature="+dep.Feature) |
| 144 | + } |
| 145 | + return strings.Join(parts, ",") |
| 146 | +} |
0 commit comments