Skip to content

Commit

Permalink
Support artifact dependencies in crate.spec (#2229)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay authored Oct 31, 2023
1 parent d86faee commit 841a733
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
33 changes: 23 additions & 10 deletions crate_universe/private/crate.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def _workspace_member(version, sha256 = None):
def _spec(
package = None,
version = None,
artifact = None,
lib = None,
default_features = True,
features = [],
git = None,
Expand All @@ -33,6 +35,8 @@ def _spec(
Args:
package (str, optional): The explicit name of the package (used when attempting to alias a crate).
version (str, optional): The exact version of the crate. Cannot be used with `git`.
artifact (str, optional): Set to "bin" to pull in a binary crate as an artifact dependency. Requires a nightly Cargo.
lib (bool, optional): If using `artifact = "bin"`, additionally setting `lib = True` declares a dependency on both the package's library and binary, as opposed to just the binary.
default_features (bool, optional): Maps to the `default-features` flag.
features (list, optional): A list of features to use for the crate
git (str, optional): The Git url to use for the crate. Cannot be used with `version`.
Expand All @@ -43,16 +47,25 @@ def _spec(
Returns:
string: A json encoded string of all inputs
"""
return json.encode(struct(
package = package,
default_features = default_features,
features = features,
version = version,
git = git,
branch = branch,
tag = tag,
rev = rev,
))
return json.encode({
k: v
for k, v in {
"artifact": artifact,
"branch": branch,
"default_features": default_features,
"features": features,
"git": git,
"lib": lib,
"package": package,
"rev": rev,
"tag": tag,
"version": version,
}.items()
# The `cargo_toml` crate parses unstable fields to a flattened
# BTreeMap<String, toml::Value> and toml::Value does not support null,
# so we must omit null values.
if v != None
})

def _assert_absolute(label):
"""Ensure a given label is an absolute label
Expand Down
4 changes: 3 additions & 1 deletion docs/crate_universe.md
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ list: A list of labels to generated rust targets (str)
## crate.spec

<pre>
crate.spec(<a href="#crate.spec-package">package</a>, <a href="#crate.spec-version">version</a>, <a href="#crate.spec-default_features">default_features</a>, <a href="#crate.spec-features">features</a>, <a href="#crate.spec-git">git</a>, <a href="#crate.spec-branch">branch</a>, <a href="#crate.spec-tag">tag</a>, <a href="#crate.spec-rev">rev</a>)
crate.spec(<a href="#crate.spec-package">package</a>, <a href="#crate.spec-version">version</a>, <a href="#crate.spec-artifact">artifact</a>, <a href="#crate.spec-lib">lib</a>, <a href="#crate.spec-default_features">default_features</a>, <a href="#crate.spec-features">features</a>, <a href="#crate.spec-git">git</a>, <a href="#crate.spec-branch">branch</a>, <a href="#crate.spec-tag">tag</a>, <a href="#crate.spec-rev">rev</a>)
</pre>

A constructor for a crate dependency.
Expand All @@ -586,6 +586,8 @@ See [specifying dependencies][sd] in the Cargo book for more details.
| :------------- | :------------- | :------------- |
| <a id="crate.spec-package"></a>package | The explicit name of the package (used when attempting to alias a crate). | `None` |
| <a id="crate.spec-version"></a>version | The exact version of the crate. Cannot be used with <code>git</code>. | `None` |
| <a id="crate.spec-artifact"></a>artifact | Set to "bin" to pull in a binary crate as an artifact dependency. Requires a nightly Cargo. | `None` |
| <a id="crate.spec-lib"></a>lib | If using <code>artifact = "bin"</code>, additionally setting <code>lib = True</code> declares a dependency on both the package's library and binary, as opposed to just the binary. | `None` |
| <a id="crate.spec-default_features"></a>default_features | Maps to the <code>default-features</code> flag. | `True` |
| <a id="crate.spec-features"></a>features | A list of features to use for the crate | `[]` |
| <a id="crate.spec-git"></a>git | The Git url to use for the crate. Cannot be used with <code>version</code>. | `None` |
Expand Down

0 comments on commit 841a733

Please sign in to comment.