Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file ppx_polymarket_enum.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177(** PPX deriver for enum types with string conversion.
Generates the full enum interface inline without any runtime dependencies.
Usage:
{[
type t = Foo | Bar | Baz [@@deriving enum]
]}
Generates UPPERCASE strings by default (Foo -> "FOO"). Case-insensitive
parsing is enabled by default.
For custom string mappings, use [@value]:
{[
type t = Min_1 [@value "1m"] | Hour_1 [@value "1h"] [@@deriving enum]
]}
Generated functions:
- to_string : t -> string
- of_string : string -> t
- of_string_opt : string -> t option
- t_of_yojson : Yojson.Safe.t -> t
- yojson_of_t : t -> Yojson.Safe.t
- pp : Format.formatter -> t -> unit
- equal : t -> t -> bool *)openPpxlib(** Extract custom value from [@value "..."] attribute on a constructor *)letget_custom_valueattrs=List.find_map(funattr->matchattr.attr_name.txtwith|"value"->(matchattr.attr_payloadwith|PStr[{pstr_desc=Pstr_eval({pexp_desc=Pexp_constant(Pconst_string(s,_,_));_;},_);_;};]->Somes|_->None)|_->None)attrs(** Convert constructor name to UPPERCASE string (default convention) *)letconstructor_to_uppercasename=String.uppercase_asciiname(** Generate the to_string function as a pattern match *)letgenerate_to_string~locconstructors=letcases=List.map(fun(name,attrs,_args)->letstr_value=matchget_custom_valueattrswith|Somecustom->custom|None->constructor_to_uppercasenameinletlhs=Ast_builder.Default.ppat_construct~loc(Ast_builder.Default.Located.lident~locname)Noneinletrhs=Ast_builder.Default.estring~locstr_valueinAst_builder.Default.case~lhs~guard:None~rhs)constructorsin[%exprfunt->[%eAst_builder.Default.pexp_match~loc[%exprt]cases]](** Generate the of_string_opt function with case-insensitive matching *)letgenerate_of_string_opt~locconstructors=(* For each constructor, generate pattern cases for all case variations *)letcases=List.concat_map(fun(name,attrs,_args)->letstr_value=matchget_custom_valueattrswith|Somecustom->custom|None->constructor_to_uppercasenamein(* Generate case variations: original, lowercase, uppercase *)letvariants=[str_value;String.lowercase_asciistr_value;String.uppercase_asciistr_value;]|>List.sort_uniqString.compareinletctor_expr=[%exprSome[%eAst_builder.Default.pexp_construct~loc(Ast_builder.Default.Located.lident~locname)None]]inList.map(funvariant->letlhs=Ast_builder.Default.pstring~locvariantinAst_builder.Default.case~lhs~guard:None~rhs:ctor_expr)variants)constructorsin(* Add the catch-all None case *)letdefault_case=Ast_builder.Default.case~lhs:(Ast_builder.Default.ppat_any~loc)~guard:None~rhs:[%exprNone]in[%exprfuns->[%eAst_builder.Default.pexp_match~loc[%exprs](cases@[default_case])]](** Main structure generator for the deriver *)letgenerate_impl~ctxt(_rec_flag,type_declarations)=letloc=Expansion_context.Deriver.derived_item_locctxtinList.concat_map(fun(td:type_declaration)->matchtd.ptype_kindwith|Ptype_variantconstructors->(* Extract constructor info: (name, attributes, args) *)letctor_info=List.map(fun(cd:constructor_declaration)->(cd.pcd_name.txt,cd.pcd_attributes,cd.pcd_args))constructorsin(* Check that all constructors have no arguments *)List.iter(fun(name,_,args)->matchargswith|Pcstr_tuple[]->()|_->Location.raise_errorf~loc:td.ptype_loc"[@@deriving enum] only supports constructors without \
arguments, but %s has arguments"name)ctor_info;(* Generate to_string and of_string_opt *)letto_string_expr=generate_to_string~locctor_infoinletof_string_opt_expr=generate_of_string_opt~locctor_infoin(* Generate all functions inline - no external dependencies *)[%strletto_string=[%eto_string_expr]letof_string_opt=[%eof_string_opt_expr]letof_strings=matchof_string_optswith|Somev->v|None->failwith("Unknown enum value: "^s)lett_of_yojson=function|`Strings->of_strings|_->failwith"Expected string for enum"letyojson_of_tt=`String(to_stringt)letppfmtt=Format.fprintffmt"%s"(to_stringt)letequalab=String.equal(to_stringa)(to_stringb)]|_->Location.raise_errorf~loc:td.ptype_loc"[@@deriving enum] can only be applied to variant types")type_declarations(** Register the deriver *)letimpl_generator=Deriving.Generator.V2.make_noarggenerate_implletmy_deriver=Deriving.add"enum"~str_type_decl:impl_generator