Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanvanburen committed May 8, 2024
1 parent aa0afb6 commit 0aba6f5
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 429 deletions.
76 changes: 14 additions & 62 deletions private/bufpkg/bufplugin/bufplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,37 +286,13 @@ func ProtoCargoConfigToCargoRegistryConfig(protoCargoConfig *registryv1alpha1.Ca
cargoConfig := &bufpluginconfig.CargoRegistryConfig{
RustVersion: protoCargoConfig.RustVersion,
}
if stdConfig := protoCargoConfig.Std; stdConfig != nil {
for _, dependency := range stdConfig.GetDeps() {
cargoConfig.Std.Deps = append(cargoConfig.Std.Deps, bufpluginconfig.CargoRegistryDependency{
Name: dependency.Name,
Req: dependency.Req,
Features: dependency.Features,
Optional: dependency.Optional,
DefaultFeatures: dependency.DefaultFeatures,
Target: dependency.Target,
Kind: dependency.Kind,
Registry: dependency.Registry,
Package: dependency.Package,
})
}
cargoConfig.Std.Options = append(cargoConfig.Std.Options, stdConfig.GetOptions()...)
}
if nostdConfig := protoCargoConfig.NoStd; nostdConfig != nil {
for _, dependency := range nostdConfig.GetDeps() {
cargoConfig.NoStd.Deps = append(cargoConfig.NoStd.Deps, bufpluginconfig.CargoRegistryDependency{
Name: dependency.Name,
Req: dependency.Req,
Features: dependency.Features,
Optional: dependency.Optional,
DefaultFeatures: dependency.DefaultFeatures,
Target: dependency.Target,
Kind: dependency.Kind,
Registry: dependency.Registry,
Package: dependency.Package,
})
}
cargoConfig.NoStd.Options = append(cargoConfig.NoStd.Options, nostdConfig.GetOptions()...)
for _, dependency := range protoCargoConfig.RuntimeLibraries {
cargoConfig.Deps = append(cargoConfig.Deps, bufpluginconfig.CargoRegistryDependency{
Name: dependency.Name,
VersionRequirement: dependency.VersionRequirement,
DefaultFeatures: dependency.DefaultFeatures,
Features: dependency.Features,
})
}
return cargoConfig, nil
}
Expand All @@ -325,37 +301,13 @@ func CargoRegistryConfigToProtoCargoConfig(cargoConfig *bufpluginconfig.CargoReg
protoCargoConfig := &registryv1alpha1.CargoConfig{
RustVersion: cargoConfig.RustVersion,
}
if stdConfig := cargoConfig.Std; stdConfig != nil {
for _, dependency := range stdConfig.Deps {
protoCargoConfig.Std.Deps = append(protoCargoConfig.Std.Deps, &registryv1alpha1.CargoConfig_Dependency{
Name: dependency.Name,
Req: dependency.Req,
Features: dependency.Features,
Optional: dependency.Optional,
DefaultFeatures: dependency.DefaultFeatures,
Target: dependency.Target,
Kind: dependency.Kind,
Registry: dependency.Registry,
Package: dependency.Package,
})
}
protoCargoConfig.Std.Options = append(protoCargoConfig.Std.Options, stdConfig.Options...)
}
if noStdConfig := cargoConfig.NoStd; noStdConfig != nil {
for _, dependency := range noStdConfig.Deps {
protoCargoConfig.NoStd.Deps = append(protoCargoConfig.NoStd.Deps, &registryv1alpha1.CargoConfig_Dependency{
Name: dependency.Name,
Req: dependency.Req,
Features: dependency.Features,
Optional: dependency.Optional,
DefaultFeatures: dependency.DefaultFeatures,
Target: dependency.Target,
Kind: dependency.Kind,
Registry: dependency.Registry,
Package: dependency.Package,
})
}
protoCargoConfig.NoStd.Options = append(protoCargoConfig.NoStd.Options, noStdConfig.Options...)
for _, dependency := range cargoConfig.Deps {
protoCargoConfig.RuntimeLibraries = append(protoCargoConfig.RuntimeLibraries, &registryv1alpha1.CargoConfig_RuntimeLibrary{
Name: dependency.Name,
VersionRequirement: dependency.VersionRequirement,
DefaultFeatures: dependency.DefaultFeatures,
Features: dependency.Features,
})
}
return protoCargoConfig, nil
}
Expand Down
50 changes: 15 additions & 35 deletions private/bufpkg/bufplugin/bufpluginconfig/bufpluginconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,29 +242,20 @@ type PythonRegistryConfig struct {
PackageType string
}

// TODO: document this.
type CargoRegistryDependency struct {
Name string
Req string
Features []string
Optional bool
DefaultFeatures bool
Target string
Kind string
Registry string
Package string
}

type CargoRegistryFeature struct {
Deps []CargoRegistryDependency
// Options contains the Maven plugin options for the runtime. Overrides RegistryConfig.Options.
Options []string
Name string
VersionRequirement string
DefaultFeatures bool
Features []string
}

// CargoRegistryConfig is the registry configuration for a Cargo plugin.
type CargoRegistryConfig struct {
Std *CargoRegistryFeature
NoStd *CargoRegistryFeature
// TODO: document
RustVersion string
// TODO: document
Deps []CargoRegistryDependency
}

// ConfigOption is an optional option used when loading a Config.
Expand Down Expand Up @@ -517,28 +508,17 @@ type ExternalPythonRegistryConfig struct {
}

type ExternalCargoDependency struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Req string `json:"req,omitempty" yaml:"req,omitempty"`
Features []string `json:"features,omitempty" yaml:"features,omitempty"`
Optional bool `json:"optional,omitempty" yaml:"optional,omitempty"`
DefaultFeatures bool `json:"default_features,omitempty" yaml:"default_features,omitempty"`
Target string `json:"target,omitempty" yaml:"target,omitempty"`
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
Registry string `json:"registry,omitempty" yaml:"registry,omitempty"`
Package string `json:"package,omitempty" yaml:"package,omitempty"`
}

type ExternalCargoFeature struct {
// Deps are dependencies for the generated SDK.
Deps []ExternalCargoDependency `json:"deps,omitempty" yaml:"deps,omitempty"`
Opts []string `json:"opts,omitempty" yaml:"opts,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
VersionRequirement string `json:"req,omitempty" yaml:"req,omitempty"`
DefaultFeatures bool `json:"default_features,omitempty" yaml:"default_features,omitempty"`
Features []string `json:"features,omitempty" yaml:"features,omitempty"`
}

type ExternalCargoRegistryConfig struct {
// RustVersion specifies the `rust_version` of the generated crate.
RustVersion string `json:"rust_version,omitempty" yaml:"rust_version,omitempty"`
Std ExternalCargoFeature `json:"std,omitempty" yaml:"std,omitempty"`
NoStd ExternalCargoFeature `json:"no_std,omitempty" yaml:"no_std,omitempty"`
RustVersion string `json:"rust_version,omitempty" yaml:"rust_version,omitempty"`
// Deps are dependencies for the generated SDK.
Deps []ExternalCargoDependency `json:"deps,omitempty" yaml:"deps,omitempty"`
}

type externalConfigVersion struct {
Expand Down
26 changes: 7 additions & 19 deletions private/bufpkg/bufplugin/bufpluginconfig/bufpluginconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,28 +346,16 @@ func TestParsePluginConfigCargoYAML(t *testing.T) {
OutputLanguages: []string{"rust"},
Registry: &RegistryConfig{
Cargo: &CargoRegistryConfig{
RustVersion: "1.62",
Std: &CargoRegistryFeature{
Deps: []CargoRegistryDependency{
{
Name: "prost",
Req: "0.6",
DefaultFeatures: true,
},
},
Options: []string{"bytes=."},
},
NoStd: &CargoRegistryFeature{
Deps: []CargoRegistryDependency{
{
Name: "prost",
Req: "0.6",
DefaultFeatures: false,
},
RustVersion: "1.60",
Deps: []CargoRegistryDependency{
{
Name: "prost",
VersionRequirement: "0.12.3",
DefaultFeatures: true,
},
Options: []string{"btree_map=.", "bytes=."},
},
},
Options: map[string]string{"enable_type_names": "true"},
},
},
pluginConfig,
Expand Down
45 changes: 12 additions & 33 deletions private/bufpkg/bufplugin/bufpluginconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,8 @@ func newRegistryConfig(externalRegistryConfig ExternalRegistryConfig) (*Registry
return nil, err
}
return &RegistryConfig{
Cargo: cargoRegistryConfig,
// NOTE: Cargo does not use options at the top-level;
// instead they are defined per feature.
// TODO: Is this actually a good idea?
Cargo: cargoRegistryConfig,
Options: options,
}, nil
default:
return nil, errors.New("unknown registry configuration")
Expand Down Expand Up @@ -374,38 +372,19 @@ func newCargoRegistryConfig(externalCargoRegistryConfig *ExternalCargoRegistryCo
if externalCargoRegistryConfig == nil {
return nil, nil
}
parseFeature := func(externalFeature ExternalCargoFeature) (CargoRegistryFeature, error) {
feature := CargoRegistryFeature{}
for _, dependency := range externalFeature.Deps {
if dependency.Name == "" {
return CargoRegistryFeature{}, fmt.Errorf("cargo registry dependency cannot have empty name")
}
if dependency.Req == "" {
return CargoRegistryFeature{}, fmt.Errorf("cargo registry dependency cannot have empty req")
}
feature.Deps = append(feature.Deps, CargoRegistryDependency(dependency))
config := &CargoRegistryConfig{
RustVersion: externalCargoRegistryConfig.RustVersion,
}
for _, dependency := range externalCargoRegistryConfig.Deps {
if dependency.Name == "" {
return nil, fmt.Errorf("cargo registry dependency cannot have empty name")
}
for _, opt := range externalFeature.Opts {
if opt == "" {
return CargoRegistryFeature{}, fmt.Errorf("cargo registry option cannot be empty")
}
feature.Options = append(feature.Options, opt)
if dependency.VersionRequirement == "" {
return nil, fmt.Errorf("cargo registry dependency cannot have empty req")
}
return feature, nil
config.Deps = append(config.Deps, CargoRegistryDependency(dependency))
}
stdConfig, err := parseFeature(externalCargoRegistryConfig.Std)
if err != nil {
return nil, fmt.Errorf("parsing std feature: %s", err)
}
noStdConfig, err := parseFeature(externalCargoRegistryConfig.NoStd)
if err != nil {
return nil, fmt.Errorf("parsing no_std feature: %s", err)
}
return &CargoRegistryConfig{
RustVersion: externalCargoRegistryConfig.RustVersion,
Std: &stdConfig,
NoStd: &noStdConfig,
}, nil
return config, nil
}

func pluginIdentityForStringWithOverrideRemote(identityStr string, overrideRemote string) (bufpluginref.PluginIdentity, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,17 @@ output_languages:
spdx_license_id: Apache-2.0
license_url: https://github.com/neoeinstein/protoc-gen-prost/blob/protoc-gen-prost-v0.3.1/LICENSE
registry:
# TODO: Update all these links to the specific version, rather than main/latest.
cargo:
# TODO: Change this link to tag, not main
# https://github.com/neoeinstein/protoc-gen-prost/blob/main/protoc-gen-prost/Cargo.toml#L11
rust_version: "1.62"
std:
deps:
- name: prost
# TODO: what's the actual version?
req: "0.6"
default_features: true
# https://github.com/neoeinstein/protoc-gen-prost/tree/main/protoc-gen-prost#options
# https://docs.rs/protoc-gen-prost/latest/protoc_gen_prost/#options
# TODO: Should we set enable_type_names? Needed for encoding messages as `Any`?
# TODO: file_descriptor_set?
opts:
# TODO: Is bytes better? We should either enable this everywhere, or nowhere.
- bytes=.
# https://docs.rs/prost/latest/prost/#using-prost-in-a-no_std-crate
no_std:
deps:
- name: prost
# TODO: what's the actual version?
req: "0.6"
default_features: false
# https://github.com/neoeinstein/protoc-gen-prost/tree/main/protoc-gen-prost#options
opts:
# Enable BTreeMap fields for Protobuf maps.
- btree_map=.
# TODO: Is bytes better? We should either enable this everywhere, or nowhere.
- bytes=.
# https://github.com/neoeinstein/protoc-gen-prost/blob/protoc-gen-prost-v0.3.1/protoc-gen-prost/Cargo.toml#L21
# https://github.com/tokio-rs/prost/blob/v0.12.3/prost-build/Cargo.toml#L15
rust_version: "1.60"
deps:
# https://github.com/tokio-rs/prost/blob/907e9f6fbf72262f52333459bbfb27224da1ad72/prost-build/Cargo.toml#L30
- name: prost
req: "0.12.3"
default_features: true
# https://github.com/neoeinstein/protoc-gen-prost/tree/protoc-gen-prost-v0.3.1/protoc-gen-prost#options
# https://docs.rs/protoc-gen-prost/latest/protoc_gen_prost/#options
opts:
# `Any` support.
- enable_type_names=true

0 comments on commit 0aba6f5

Please sign in to comment.