|
| 1 | +let find_as_attribute_payload (attributes : Parsetree.attribute list) = |
| 2 | + attributes |
| 3 | + |> List.find_map (fun (attr : Parsetree.attribute) -> |
| 4 | + match attr with |
| 5 | + | {txt = "as"}, payload -> Some payload |
| 6 | + | _ -> None) |
| 7 | + |
| 8 | +(* TODO: Improve error messages? Say why we can't coerce. *) |
| 9 | + |
| 10 | +let check_constructors (constructors : Types.constructor_declaration list) check |
| 11 | + = |
| 12 | + List.for_all |
| 13 | + (fun (c : Types.constructor_declaration) -> |
| 14 | + check c.cd_args (find_as_attribute_payload c.cd_attributes)) |
| 15 | + constructors |
| 16 | + |
| 17 | +let can_coerce_to_string (constructors : Types.constructor_declaration list) = |
| 18 | + check_constructors constructors (fun args payload -> |
| 19 | + match (args, payload) with |
| 20 | + | Cstr_tuple [], None -> true |
| 21 | + | Cstr_tuple [], Some payload |
| 22 | + when Ast_payload.is_single_string payload |> Option.is_some -> |
| 23 | + true |
| 24 | + | _ -> false) |
| 25 | + |
| 26 | +let can_coerce_to_int (constructors : Types.constructor_declaration list) = |
| 27 | + check_constructors constructors (fun args payload -> |
| 28 | + match (args, payload) with |
| 29 | + | Cstr_tuple [], Some payload |
| 30 | + when Ast_payload.is_single_int payload |> Option.is_some -> |
| 31 | + true |
| 32 | + | _ -> false) |
| 33 | + |
| 34 | +let can_coerce_to_float (constructors : Types.constructor_declaration list) = |
| 35 | + check_constructors constructors (fun args payload -> |
| 36 | + match (args, payload) with |
| 37 | + | Cstr_tuple [], Some payload |
| 38 | + when Ast_payload.is_single_float payload |> Option.is_some -> |
| 39 | + true |
| 40 | + | _ -> false) |
| 41 | + |
| 42 | +let can_coerce_path (path : Path.t) = |
| 43 | + Path.same path Predef.path_string |
| 44 | + || Path.same path Predef.path_int |
| 45 | + || Path.same path Predef.path_float |
| 46 | + |
| 47 | +let can_coerce_variant ~(path : Path.t) |
| 48 | + (constructors : Types.constructor_declaration list) = |
| 49 | + if Path.same path Predef.path_string && can_coerce_to_string constructors then |
| 50 | + true |
| 51 | + else if Path.same path Predef.path_int && can_coerce_to_int constructors then |
| 52 | + true |
| 53 | + else if Path.same path Predef.path_float && can_coerce_to_float constructors |
| 54 | + then true |
| 55 | + else false |
| 56 | + |
| 57 | +let is_variant_typedecl |
| 58 | + ((_, _, typedecl) : Path.t * Path.t * Types.type_declaration) = |
| 59 | + match typedecl with |
| 60 | + | {type_kind = Type_variant constructors} -> Some constructors |
| 61 | + | _ -> None |
0 commit comments