Skip to content

Commit

Permalink
Replaced multiple usages of String with Label.
Browse files Browse the repository at this point in the history
  • Loading branch information
rickvanprim committed Dec 27, 2023
1 parent 1048ecf commit 24abb37
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 36 deletions.
1 change: 1 addition & 0 deletions crate_universe/docs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ call above or [crates_repository::generator_urls](#crates_repository-generator_u
- [crate_universe_dependencies](#crate_universe_dependencies)
- [crate.annotation](#crateannotation)
- [crate.select](#crateselect)
- [crate.spec](#cratespec)
- [crate.workspace_member](#crateworkspace_member)
- [render_config](#render_config)
Expand Down
15 changes: 8 additions & 7 deletions crate_universe/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use serde::de::{Deserializer, SeqAccess, Visitor};
use serde::{Deserialize, Serialize, Serializer};

use crate::select::{Select, Selectable};
use crate::utils::starlark::Label;
use crate::utils::target_triple::TargetTriple;

/// Representations of different kinds of crate vendoring into workspaces.
Expand Down Expand Up @@ -226,11 +227,11 @@ pub struct CrateAnnotations {

/// Additional data to pass to
/// [deps](https://bazelbuild.github.io/rules_rust/defs.html#rust_library-deps) attribute.
pub deps: Option<Select<BTreeSet<String>>>,
pub deps: Option<Select<BTreeSet<Label>>>,

/// Additional data to pass to
/// [proc_macro_deps](https://bazelbuild.github.io/rules_rust/defs.html#rust_library-proc_macro_deps) attribute.
pub proc_macro_deps: Option<Select<BTreeSet<String>>>,
pub proc_macro_deps: Option<Select<BTreeSet<Label>>>,

/// Additional data to pass to the target's
/// [crate_features](https://bazelbuild.github.io/rules_rust/defs.html#rust_library-crate_features) attribute.
Expand Down Expand Up @@ -269,19 +270,19 @@ pub struct CrateAnnotations {

/// Additional dependencies to pass to a build script's
/// [deps](https://bazelbuild.github.io/rules_rust/cargo.html#cargo_build_script-deps) attribute.
pub build_script_deps: Option<Select<BTreeSet<String>>>,
pub build_script_deps: Option<Select<BTreeSet<Label>>>,

/// Additional data to pass to a build script's
/// [proc_macro_deps](https://bazelbuild.github.io/rules_rust/cargo.html#cargo_build_script-proc_macro_deps) attribute.
pub build_script_proc_macro_deps: Option<Select<BTreeSet<String>>>,
pub build_script_proc_macro_deps: Option<Select<BTreeSet<Label>>>,

/// Additional data to pass to a build script's
/// [build_script_data](https://bazelbuild.github.io/rules_rust/cargo.html#cargo_build_script-data) attribute.
pub build_script_data: Option<Select<BTreeSet<String>>>,

/// Additional data to pass to a build script's
/// [tools](https://bazelbuild.github.io/rules_rust/cargo.html#cargo_build_script-tools) attribute.
pub build_script_tools: Option<Select<BTreeSet<String>>>,
pub build_script_tools: Option<Select<BTreeSet<Label>>>,

/// An optional glob pattern to set on the
/// [build_script_data](https://bazelbuild.github.io/rules_rust/cargo.html#cargo_build_script-build_script_env) attribute.
Expand All @@ -297,7 +298,7 @@ pub struct CrateAnnotations {

/// Additional labels to pass to a build script's
/// [toolchains](https://bazel.build/reference/be/common-definitions#common-attributes) attribute.
pub build_script_toolchains: Option<BTreeSet<String>>,
pub build_script_toolchains: Option<BTreeSet<Label>>,

/// Directory to run the crate's build script in. If not set, will run in the manifest directory, otherwise a directory relative to the exec root.
pub build_script_rundir: Option<Select<String>>,
Expand Down Expand Up @@ -431,7 +432,7 @@ pub struct AnnotationsProvidedByPackage {
pub gen_build_script: Option<bool>,
pub data: Option<Select<BTreeSet<String>>>,
pub data_glob: Option<BTreeSet<String>>,
pub deps: Option<Select<BTreeSet<String>>>,
pub deps: Option<Select<BTreeSet<Label>>>,
pub compile_data: Option<Select<BTreeSet<String>>>,
pub compile_data_glob: Option<BTreeSet<String>>,
pub rustc_env: Option<Select<BTreeMap<String, String>>>,
Expand Down
16 changes: 8 additions & 8 deletions crate_universe/src/context/crate_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::config::{AliasRule, CrateId, GenBinaries};
use crate::metadata::{CrateAnnotation, Dependency, PairedExtras, SourceAnnotation};
use crate::select::Select;
use crate::utils::sanitize_module_name;
use crate::utils::starlark::Glob;
use crate::utils::starlark::{Glob, Label};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct CrateDependency {
Expand Down Expand Up @@ -83,7 +83,7 @@ pub struct CommonAttributes {
pub deps: Select<BTreeSet<CrateDependency>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub extra_deps: Select<BTreeSet<String>>,
pub extra_deps: Select<BTreeSet<Label>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub deps_dev: Select<BTreeSet<CrateDependency>>,
Expand All @@ -97,7 +97,7 @@ pub struct CommonAttributes {
pub proc_macro_deps: Select<BTreeSet<CrateDependency>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub extra_proc_macro_deps: Select<BTreeSet<String>>,
pub extra_proc_macro_deps: Select<BTreeSet<Label>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub proc_macro_deps_dev: Select<BTreeSet<CrateDependency>>,
Expand Down Expand Up @@ -161,7 +161,7 @@ pub struct BuildScriptAttributes {
pub deps: Select<BTreeSet<CrateDependency>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub extra_deps: Select<BTreeSet<String>>,
pub extra_deps: Select<BTreeSet<Label>>,

// TODO: refactor a crate with a build.rs file from two into three bazel
// rules in order to deduplicate link_dep information. Currently as the
Expand All @@ -184,7 +184,7 @@ pub struct BuildScriptAttributes {
pub link_deps: Select<BTreeSet<CrateDependency>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub extra_link_deps: Select<BTreeSet<String>>,
pub extra_link_deps: Select<BTreeSet<Label>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub build_script_env: Select<BTreeMap<String, String>>,
Expand All @@ -193,7 +193,7 @@ pub struct BuildScriptAttributes {
pub rundir: Select<String>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub extra_proc_macro_deps: Select<BTreeSet<String>>,
pub extra_proc_macro_deps: Select<BTreeSet<Label>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub proc_macro_deps: Select<BTreeSet<CrateDependency>>,
Expand All @@ -208,13 +208,13 @@ pub struct BuildScriptAttributes {
pub rustc_env_files: Select<BTreeSet<String>>,

#[serde(skip_serializing_if = "Select::is_empty")]
pub tools: Select<BTreeSet<String>>,
pub tools: Select<BTreeSet<Label>>,

#[serde(skip_serializing_if = "Option::is_none")]
pub links: Option<String>,

#[serde(skip_serializing_if = "BTreeSet::is_empty")]
pub toolchains: BTreeSet<String>,
pub toolchains: BTreeSet<Label>,
}

impl Default for BuildScriptAttributes {
Expand Down
21 changes: 13 additions & 8 deletions crate_universe/src/rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ impl Renderer {
starlark.push(Starlark::Alias(Alias {
rule: AliasRule::default().rule(),
name: target.crate_name.clone(),
actual: format!("{}_build_script", krate.name),
actual: Label::from_str(&format!("{}_build_script", krate.name)).unwrap(),
tags: BTreeSet::from(["manual".to_owned()]),
}));
}
Expand Down Expand Up @@ -566,7 +566,10 @@ impl Renderer {
krate.common_attrs.extra_deps.clone(),
);
if let Some(library_target_name) = &krate.library_target_name {
deps.insert(format!(":{library_target_name}"), None);
deps.insert(
Label::from_str(&format!(":{library_target_name}")).unwrap(),
None,
);
}
SelectSet::new(deps, platforms)
},
Expand Down Expand Up @@ -674,7 +677,8 @@ impl Renderer {
&dependency.id.version,
&dependency.target,
);
aliases.insert((label, alias.clone()), configuration.clone());
// Should `Select` support `BTreeMap<Label, T>` or `BTreeMap<U, T>`?
aliases.insert((label.repr(), alias.clone()), configuration.clone());
}
}
}
Expand All @@ -684,8 +688,8 @@ impl Renderer {
fn make_deps(
&self,
deps: Select<BTreeSet<CrateDependency>>,
extra_deps: Select<BTreeSet<String>>,
) -> Select<BTreeSet<String>> {
extra_deps: Select<BTreeSet<Label>>,
) -> Select<BTreeSet<Label>> {
Select::merge(
deps.map(|dep| self.crate_label(&dep.id.name, &dep.id.version, &dep.target)),
extra_deps,
Expand All @@ -712,14 +716,15 @@ impl Renderer {
}
}

fn crate_label(&self, name: &str, version: &str, target: &str) -> String {
sanitize_repository_name(&render_crate_bazel_label(
fn crate_label(&self, name: &str, version: &str, target: &str) -> Label {
Label::from_str(&sanitize_repository_name(&render_crate_bazel_label(
&self.config.crate_label_template,
&self.config.repository_name,
name,
version,
target,
))
)))
.unwrap()
}
}

Expand Down
26 changes: 13 additions & 13 deletions crate_universe/src/utils/starlark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub struct Filegroup {
pub struct Alias {
pub rule: String,
pub name: String,
pub actual: String,
pub actual: Label,
pub tags: Set<String>,
}

Expand All @@ -90,16 +90,16 @@ pub struct CargoBuildScript {
#[serde(skip_serializing_if = "Data::is_empty")]
pub data: Data,
#[serde(skip_serializing_if = "SelectSet::is_empty")]
pub deps: SelectSet<String>,
pub deps: SelectSet<Label>,
#[serde(skip_serializing_if = "SelectSet::is_empty")]
pub link_deps: SelectSet<String>,
pub link_deps: SelectSet<Label>,
pub edition: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub linker_script: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub links: Option<String>,
#[serde(skip_serializing_if = "SelectSet::is_empty")]
pub proc_macro_deps: SelectSet<String>,
pub proc_macro_deps: SelectSet<Label>,
#[serde(skip_serializing_if = "SelectScalar::is_empty")]
pub rundir: SelectScalar<String>,
#[serde(skip_serializing_if = "SelectDict::is_empty")]
Expand All @@ -112,9 +112,9 @@ pub struct CargoBuildScript {
#[serde(skip_serializing_if = "Set::is_empty")]
pub tags: Set<String>,
#[serde(skip_serializing_if = "SelectSet::is_empty")]
pub tools: SelectSet<String>,
pub tools: SelectSet<Label>,
#[serde(skip_serializing_if = "Set::is_empty")]
pub toolchains: Set<String>,
pub toolchains: Set<Label>,
pub version: String,
pub visibility: Set<String>,
}
Expand All @@ -123,9 +123,9 @@ pub struct CargoBuildScript {
pub struct RustProcMacro {
pub name: String,
#[serde(skip_serializing_if = "SelectSet::is_empty")]
pub deps: SelectSet<String>,
pub deps: SelectSet<Label>,
#[serde(skip_serializing_if = "SelectSet::is_empty")]
pub proc_macro_deps: SelectSet<String>,
pub proc_macro_deps: SelectSet<Label>,
#[serde(skip_serializing_if = "SelectDict::is_empty")]
pub aliases: SelectDict<String>,
#[serde(flatten)]
Expand All @@ -136,9 +136,9 @@ pub struct RustProcMacro {
pub struct RustLibrary {
pub name: String,
#[serde(skip_serializing_if = "SelectSet::is_empty")]
pub deps: SelectSet<String>,
pub deps: SelectSet<Label>,
#[serde(skip_serializing_if = "SelectSet::is_empty")]
pub proc_macro_deps: SelectSet<String>,
pub proc_macro_deps: SelectSet<Label>,
#[serde(skip_serializing_if = "SelectDict::is_empty")]
pub aliases: SelectDict<String>,
#[serde(flatten)]
Expand All @@ -151,9 +151,9 @@ pub struct RustLibrary {
pub struct RustBinary {
pub name: String,
#[serde(skip_serializing_if = "SelectSet::is_empty")]
pub deps: SelectSet<String>,
pub deps: SelectSet<Label>,
#[serde(skip_serializing_if = "SelectSet::is_empty")]
pub proc_macro_deps: SelectSet<String>,
pub proc_macro_deps: SelectSet<Label>,
#[serde(skip_serializing_if = "SelectDict::is_empty")]
pub aliases: SelectDict<String>,
#[serde(flatten)]
Expand Down Expand Up @@ -219,7 +219,7 @@ impl Serialize for Alias {
#[derive(Serialize)]
struct AliasInner<'a> {
pub name: &'a String,
pub actual: &'a String,
pub actual: &'a Label,
pub tags: &'a Set<String>,
}

Expand Down
1 change: 1 addition & 0 deletions docs/crate_universe.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ call above or [crates_repository::generator_urls](#crates_repository-generator_u

- [crate_universe_dependencies](#crate_universe_dependencies)
- [crate.annotation](#crateannotation)
- [crate.select](#crateselect)
- [crate.spec](#cratespec)
- [crate.workspace_member](#crateworkspace_member)
- [render_config](#render_config)
Expand Down

0 comments on commit 24abb37

Please sign in to comment.