Skip to content

Cleanup enum variants in tuple form with multiple fields #1156

@tslil-topos

Description

@tslil-topos

Low priority

I put together a quick python script (source at end) and pointed this at the repo, here's the output. Some of these will require human judgement and we could imagine increasing the threshold from 2 to 3 fields etc.

TupleVariant(file='catlog/src/dbl/model.rs',
             enum='InvalidDblModel',
             variant='Eqn',
             fields=['Option<usize>', 'NonEmpty<InvalidModelEqn>'])


TupleVariant(file='catlog/src/dbl/theory.rs',
             enum='InvalidDblTheory',
             variant='MorTypeEq',
             fields=['usize', 'NonEmpty<InvalidPathEq>'])


TupleVariant(file='catlog/src/dbl/theory.rs',
             enum='InvalidDblTheory',
             variant='ObOpEq',
             fields=['usize', 'NonEmpty<InvalidPathEq>'])


TupleVariant(file='catlog/src/one/fp_category.rs',
             enum='InvalidFpCategory',
             variant='Eqn',
             fields=['usize', 'NonEmpty<InvalidPathEq>'])


TupleVariant(file='catlog/src/tt/stx.rs',
             enum='TyS_',
             variant='Morphism',
             fields=['MorType', 'TmS', 'TmS'])


TupleVariant(file='catlog/src/tt/stx.rs',
             enum='TyS_',
             variant='Sing',
             fields=['TyS', 'TmS'])


TupleVariant(file='catlog/src/tt/stx.rs',
             enum='TyS_',
             variant='Id',
             fields=['TyS', 'TmS', 'TmS'])


TupleVariant(file='catlog/src/tt/stx.rs',
             enum='TyS_',
             variant='Specialize',
             fields=['TyS', 'Vec<(Vec<(FieldName, LabelSegment)>, TyS)>'])


TupleVariant(file='catlog/src/tt/stx.rs',
             enum='TmS_',
             variant='TopApp',
             fields=['TopVarName', 'Vec<TmS>'])


TupleVariant(file='catlog/src/tt/stx.rs',
             enum='TmS_',
             variant='Var',
             fields=['BwdIdx', 'VarName', 'LabelSegment'])


TupleVariant(file='catlog/src/tt/stx.rs',
             enum='TmS_',
             variant='Proj',
             fields=['TmS', 'FieldName', 'LabelSegment'])


TupleVariant(file='catlog/src/tt/stx.rs',
             enum='TmS_',
             variant='Compose',
             fields=['TmS', 'TmS'])


TupleVariant(file='catlog/src/tt/stx.rs',
             enum='TmS_',
             variant='ObApp',
             fields=['VarName', 'TmS'])


TupleVariant(file='catlog/src/tt/val.rs',
             enum='TyV_',
             variant='Morphism',
             fields=['MorType', 'TmV', 'TmV'])


TupleVariant(file='catlog/src/tt/val.rs',
             enum='TyV_',
             variant='Sing',
             fields=['TyV', 'TmV'])


TupleVariant(file='catlog/src/tt/val.rs',
             enum='TyV_',
             variant='Id',
             fields=['TyV', 'TmV', 'TmV'])


TupleVariant(file='catlog/src/tt/val.rs',
             enum='TmN_',
             variant='Var',
             fields=['FwdIdx', 'VarName', 'LabelSegment'])


TupleVariant(file='catlog/src/tt/val.rs',
             enum='TmN_',
             variant='Proj',
             fields=['TmN', 'FieldName', 'LabelSegment'])


TupleVariant(file='catlog/src/tt/val.rs',
             enum='TmV_',
             variant='Neu',
             fields=['TmN', 'TyV'])


TupleVariant(file='catlog/src/tt/val.rs',
             enum='TmV_',
             variant='App',
             fields=['VarName', 'TmV'])


TupleVariant(file='catlog/src/tt/val.rs',
             enum='TmV_',
             variant='Compose',
             fields=['TmV', 'TmV'])


TupleVariant(file='catlog/src/tt/text_elab.rs',
             enum='TopElabResult',
             variant='Declaration',
             fields=['TopVarName', 'TopDecl'])


TupleVariant(file='catlog/src/dbl/modal/model.rs',
             enum='ModalOb',
             variant='App',
             fields=['Box<Self>', 'QualifiedName'])


TupleVariant(file='catlog/src/dbl/modal/model.rs',
             enum='ModalOb',
             variant='List',
             fields=['List', 'Vec<Self>'])


TupleVariant(file='catlog/src/dbl/modal/model.rs',
             enum='ModalMor',
             variant='App',
             fields=['Box<Path<ModalOb, Self>>', 'QualifiedName'])


TupleVariant(file='catlog/src/dbl/modal/model.rs',
             enum='ModalMor',
             variant='HomApp',
             fields=['Box<Path<ModalOb, Self>>', 'QualifiedName'])


TupleVariant(file='catlog/src/dbl/modal/model.rs',
             enum='ModalMor',
             variant='List',
             fields=['MorListData', 'Vec<Self>'])


TupleVariant(file='catlog/src/dbl/modal/theory.rs',
             enum='ModalOp',
             variant='Concat',
             fields=['List', 'usize', 'ModalObType'])


Script

#!/usr/bin/env -S uv run --script
#
# /// script
# requires-python = ">=3.13"
# dependencies = ["tree-sitter", "tree-sitter-rust"]
# ///

import sys
from pathlib import Path
from dataclasses import dataclass
from pprint import pp

import tree_sitter_rust
from tree_sitter import Language, Parser

RUST_LANGUAGE = Language(tree_sitter_rust.language())
parser = Parser(RUST_LANGUAGE)


@dataclass
class TupleVariant:
    file: str
    enum: str
    variant: str
    fields: list[str]


def find_enum_variants_with_fields(
    repo_root: Path, file_path: Path
) -> list[TupleVariant]:
    with file_path.open("rb") as fh:
        tree = parser.parse(fh.read())

    file = file_path.relative_to(repo_root).as_posix()

    enums = []
    for node in tree.root_node.children:
        if node.type == "enum_item":
            enum_name = None
            for child in node.children:
                if child.type == "type_identifier":
                    enum_name = child.text.decode("utf-8")
                elif enum_name and child.type == "enum_variant_list":
                    for variant in child.children:
                        if variant.type == "enum_variant":
                            variant_name = None
                            fields = []
                            for v_child in variant.children:
                                if v_child.type == "identifier":
                                    variant_name = v_child.text.decode("utf-8")
                                elif v_child.type == "ordered_field_declaration_list":
                                    for f in v_child.children:
                                        if "type" in f.type:
                                            fields.append(f.text.decode("utf-8"))
                            if len(fields) > 1 and variant_name:
                                enums.append(
                                    TupleVariant(file, enum_name, variant_name, fields)
                                )

    return enums


def find_all(repo_root: Path) -> list[TupleVariant]:
    rust_files = repo_root.glob("**/*.rs")
    return [
        tv
        for rust_file in rust_files
        for tv in find_enum_variants_with_fields(repo_root, rust_file)
    ]


def go():
    if len(sys.argv) != 2:
        print(f"Usage: {sys.argv[0]} /path/to/repo/")
        sys.exit(1)
    print("```")
    for tv in find_all(Path(sys.argv[1])):
        pp(tv)
        print("\n")
    print("```")


if __name__ == "__main__":
    go()

Metadata

Metadata

Assignees

No one assigned

    Labels

    coreRust core for categorical logic and general computationminorThings like typos or comments

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions