--- a/rust2rpm/cfg.py 2022-07-24 12:13:41.000000000 -0700 +++ b/rust2rpm/cfg.py 2023-08-01 15:49:09.101619723 -0700 @@ -63,58 +63,56 @@ def evaluate_predicate(name: str, value: str) -> bool: # based on: https://doc.rust-lang.org/reference/conditional-compilation.html - match name: - case "target_arch": + if name == "target_arch": # Needs to be ignored, as we cannot generate patches that are # different depending on the host architecture - except if the # target architecture is "wasm32", which we don't support. return value != "wasm32" - case "target_feature": + elif name == "target_feature": # The "target_feature" predicate can be ignored as well, since the # valid values for this predicate are architecture-dependent. return True - case "target_os": + elif name == "target_os": return value == "linux" - case "target_family": + elif name == "target_family": return value == "unix" - case "target_env": + elif name == "target_env": # The "target_env" predicate is used to disambiguate target # platforms based on its C library / C ABI (i.e. we can ignore # "msvc" and "musl"), and if there's no need to disambiguate, the # value can be the empty string. return value in ["", "gnu"] - case "target_endian": + elif name == "target_endian": # Needs to be ignored, as we cannot generate patches that are # different depending on the host architecture. return True - case "target_pointer_width": + elif name == "target_pointer_width": # Needs to be ignored, as we cannot generate patches that are # different depending on the host architecture. return True - case "target_vendor": + elif name == "target_vendor": # On linux systems, "target_vendor" is always "unknown". return value == "unknown" - case _: + else: log.warn(f'Ignoring invalid predicate \'"{name}" = "{value}"\' in cfg-expression.') return False @functools.cache def evaluate_atom(name: str) -> bool: - match name: - case "unix": + if name == "unix": return True - case "windows": + elif name == "windows": return False - case _: + else: log.warn( f"Ignoring unknown identifier {name!r} in cfg-expression. " + 'Only "unix" and "windows" are standard identifiers, ' @@ -126,21 +124,21 @@ def evaluate(expr, nested=False) -> bool: if hasattr(expr, "asList"): expr = expr.asList() # compat with pyparsing 2.7.x - match expr: - case ["cfg", subexpr] if not nested: - return evaluate(subexpr, True) - case ["not", subexpr] if nested: - return not evaluate(subexpr, True) - case ["all", *args] if nested: - return all(evaluate(arg, True) for arg in args) - case ["any", *args] if nested: - return any(evaluate(arg, True) for arg in args) - case [variable, value] if nested: - v = ast.literal_eval(value) - return evaluate_predicate(variable, v) - case [variable] if nested: - return evaluate_atom(variable) - case _: + if not nested and len(expr) == 2 and expr[0] == "cfg": + return evaluate(expr[1], True) + elif nested and len(expr) == 2 and expr[0] == "not": + return not evaluate(expr[1], True) + elif nested and len(expr) >= 1 and expr[0] == "all": + return all(evaluate(arg, True) for arg in expr[1:]) + elif nested and len(expr) >= 1 and expr[0] == "any": + return any(evaluate(arg, True) for arg in expr[1:]) + elif nested and len(expr) == 2: + v = ast.literal_eval(expr[1]) + x = evaluate_variable(variable) + return x == v + elif nested and len(expr) == 1: + return evaluate_atom(expr[0]) + else: raise ValueError