fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

UebelAndreReplaced `crates_universe` rule with `crates_repository` (#1158)59fab4e7

0.8.1
2.1 KiB56 linesraw
1"""A helper module solving for complex select statements in rendered cargo-bazel modules"""
2
3def select_with_or(input_dict, no_match_error = ""):
4    """Drop-in replacement for `select()` that supports ORed keys.
5
6    This is notably different from [@bazel_skylib//lib:selects.bzl%selects.with_or][swo] in that
7    the input dict must have a list as it's values and they keys will continue to expand for each
8    entry instead of failing on duplicates.
9
10    Example:
11    ```starlark
12    deps = selects.with_or({
13        "//configs:one": [":dep1"],
14        ("//configs:two", "//configs:three"): [":dep2or3"],
15        "//configs:four": [":dep4"],
16        "//conditions:default": [":default"]
17    })
18    ```
19    Key labels may appear at most once anywhere in the input.
20
21    This macro eturns a native `select` that expands `("//configs:two", "//configs:three"): [":dep2or3"]` to
22    ```starlark
23    "//configs:two": [":dep2or3"],
24    "//configs:three": [":dep2or3"],
25    ```
26
27    [swo]: https://github.com/bazelbuild/bazel-skylib/blob/1.1.1/docs/selects_doc.md#selectswith_or
28
29    Args:
30        input_dict: The same dictionary `select()` takes, except keys may take
31            either the usual form `"//foo:config1"` or
32            `("//foo:config1", "//foo:config2", ...)` to signify
33            `//foo:config1` OR `//foo:config2` OR `...`.
34        no_match_error: Optional custom error to report if no condition matches.
35
36    Returns:
37        A native `select()`
38    """
39    output_dict = {}
40    for (key, value) in input_dict.items():
41        if type(key) == type(()):
42            for config_setting in key:
43                if config_setting in output_dict:
44                    output_dict[config_setting].extend(value)
45                else:
46                    output_dict[config_setting] = list(value)
47        elif key in output_dict:
48            output_dict[key].extend(value)
49        else:
50            output_dict[key] = list(value)
51
52    # return a dict with deduped lists
53    return select(
54        {key: depset(value).to_list() for key, value in output_dict.items()},
55        no_match_error = no_match_error,
56    )