fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
59fab4e7
0.9.0
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 6This is notably different from [@bazel_skylib//lib:selects.bzl%selects.with_or][swo] in that 7the input dict must have a list as it's values and they keys will continue to expand for each 8entry instead of failing on duplicates. 9 10Example: 11```starlark 12deps = selects.with_or({ 13"//configs:one": [":dep1"], 14("//configs:two", "//configs:three"): [":dep2or3"], 15"//configs:four": [":dep4"], 16"//conditions:default": [":default"] 17}) 18``` 19Key labels may appear at most once anywhere in the input. 20 21This 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 29Args: 30input_dict: The same dictionary `select()` takes, except keys may take 31either the usual form `"//foo:config1"` or 32`("//foo:config1", "//foo:config2", ...)` to signify 33`//foo:config1` OR `//foo:config2` OR `...`. 34no_match_error: Optional custom error to report if no condition matches. 35 36Returns: 37A native `select()` 38""" 39output_dict = {} 40for ( key , value ) in input_dict . items (): 41if type ( key ) == type (( )): 42for config_setting in key : 43if config_setting in output_dict : 44output_dict [ config_setting ]. extend ( value ) 45else : 46output_dict [ config_setting ] = list ( value ) 47elif key in output_dict : 48output_dict [ key ]. extend ( value ) 49else : 50output_dict [ key ] = list ( value ) 51 52# return a dict with deduped lists 53return select ( 54{ key : depset ( value ). to_list () for key , value in output_dict . items ()}, 55no_match_error = no_match_error , 56)