-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathvariant_coercion.ml
56 lines (50 loc) · 1.98 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
let can_coerce_path (path : Path.t) =
Path.same path Predef.path_string
|| Path.same path Predef.path_int
|| Path.same path Predef.path_float
let can_coerce_variant ~(path : Path.t)
(constructors : Types.constructor_declaration list) =
if can_coerce_path path then
if Path.same path Predef.path_string && can_coerce_to_string constructors
then true
else if Path.same path Predef.path_int && can_coerce_to_int constructors
then true
else if Path.same path Predef.path_float && can_coerce_to_float constructors
then true
else false
else false