Type Safe Enum Generation Per Language
Trigger: You support 300+ string-keyed types and want compile-time validation in every language without hand-maintaining four enum lists.
Steps
- Parse content_types_kb.min.json once; produce a deterministically sorted list of labels.
- Generate per language: Rust pub enum (with non_exhaustive), Python StrEnum, TS string literal union, Go const iota with String().
- Apply consistent name normalization (3gp → _3gp in Rust due to no leading-digit identifiers).
- Generate from_string / to_string + a SIZE/COUNT constant.
- At codegen time, validate every label is present in the target model — fail the build on drift.
- Add a 'DO NOT EDIT' header to every generated file and check generated files into git.
Counter / Caveats
- Label normalization rules differ per language; keep them in one place.
- Rust enums with 300+ variants slow compile time noticeably; non_exhaustive helps downstream evolution.
- Index stability matters if downstream uses int indices for array lookups; renumbering breaks model output mappings.
- Tooling (rust-analyzer, pyright) needs the generated files to exist before it can resolve symbols — generate before checkout-time tasks.
Source
Extracted from magika (https://github.com/google/magika.git @ main).
Files of interest:
rust/gen/src/main.rs:40-100python/src/magika/types/content_type_label.py:19-50js/src/content-type-label.ts
Source: kjuhwa/skills-hub — distributed by TomeVault.