Generate Rust bitflags definitions from a Python dictionary. Args: d (dict): The dictionary containing the bitflag variants. prefix (str): The prefix to strip from the variant names. derives (str): The derive attributes to include. struct_name (str): The name of
(d, prefix, derives, struct_name, int_t)
| 59 | |
| 60 | |
| 61 | def dump_bitflags(d, prefix, derives, struct_name, int_t): |
| 62 | """Generate Rust bitflags definitions from a Python dictionary. |
| 63 | |
| 64 | Args: |
| 65 | d (dict): The dictionary containing the bitflag variants. |
| 66 | prefix (str): The prefix to strip from the variant names. |
| 67 | derives (str): The derive attributes to include. |
| 68 | struct_name (str): The name of the struct to generate. |
| 69 | int_t (str): The integer type to use for the bitflags. |
| 70 | |
| 71 | Returns: |
| 72 | list: A list of strings representing the bitflags definition. |
| 73 | """ |
| 74 | items = [(value, name) for name, value in d.items() if name.startswith(prefix)] |
| 75 | content = ["bitflags! {\n"] |
| 76 | content.append(f"{derives}\n") if derives else None |
| 77 | content.append(f" pub struct {struct_name}: {int_t} {{\n") |
| 78 | for value, name in sorted(items): |
| 79 | name = str(name).removeprefix(prefix) |
| 80 | content.append(f" const {name} = {value};\n") |
| 81 | content.append(" }\n") |
| 82 | content.append("}\n\n") |
| 83 | return content |
| 84 | |
| 85 | |
| 86 | def main( |
no test coverage detected