-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathvariant_coercion.ml
39 lines (35 loc) · 1.41 KB
/
variant_coercion.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
let find_as_attribute_payload (attributes : Parsetree.attribute list) =
attributes
|> List.find_map (fun (attr : Parsetree.attribute) ->
match attr with
| {txt = "as"}, payload -> Some payload
| _ -> None)
(* TODO: Improve error messages? Say why we can't coerce. *)
let can_coerce_to_string (constructors : Types.constructor_declaration list) =
List.for_all
(fun (c : Types.constructor_declaration) ->
match (c.cd_args, find_as_attribute_payload c.cd_attributes) with
| Cstr_tuple [], None -> true
| Cstr_tuple [], Some payload
when Ast_payload.is_single_string payload |> Option.is_some ->
true
| _ -> false)
constructors
let can_coerce_to_int (constructors : Types.constructor_declaration list) =
List.for_all
(fun (c : Types.constructor_declaration) ->
match (c.cd_args, find_as_attribute_payload c.cd_attributes) with
| Cstr_tuple [], Some payload
when Ast_payload.is_single_int payload |> Option.is_some ->
true
| _ -> false)
constructors
let can_coerce_to_float (constructors : Types.constructor_declaration list) =
List.for_all
(fun (c : Types.constructor_declaration) ->
match (c.cd_args, find_as_attribute_payload c.cd_attributes) with
| Cstr_tuple [], Some payload
when Ast_payload.is_single_float payload |> Option.is_some ->
true
| _ -> false)
constructors