• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bn-d / ppx_subliner / 119

pending completion
119

Pull #30

github

web-flow
Merge ce6d4096d into 8157102c0
Pull Request #30: Add deriver for enum

39 of 39 new or added lines in 8 files covered. (100.0%)

526 of 580 relevant lines covered (90.69%)

24.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.74
/src/enum.ml
1
open Ppxlib
2

3
let suffix = "cmdliner_conv"
4
let gen_name_str = Utils.gen_name_str suffix
14✔
5
let gen_name { txt = name; loc } = { txt = gen_name_str name; loc }
2✔
6

7
let expression_of_const_decl (cd : constructor_declaration) : expression =
8
  let loc = cd.pcd_loc in
2✔
9
  let () =
10
    match cd.pcd_args with Pcstr_tuple [] -> () | _ -> Error.enum_payload ~loc
×
11
  in
12
  let enum_expr =
13
    Ast_helper.Exp.construct ~loc (Utils.longident_loc_of_name cd.pcd_name) None
2✔
14
  and names = Attribute_parser.Enum.parse cd.pcd_attributes in
2✔
15
  match names with
16
  | None ->
1✔
17
      let name_expr =
18
        let name =
19
          cd.pcd_name.txt
20
          |> String.lowercase_ascii
21
          |> String.map (function '_' -> '-' | c -> c)
1✔
22
        in
23
        Ast_builder.Default.estring ~loc name
1✔
24
      in
25
      [%expr [ ([%e name_expr], [%e enum_expr]) ]]
26
  | Some (loc, structure) ->
1✔
27
      let expr = Attribute_parser.to_expr "names" (loc, structure) in
28
      [%expr List.map (fun name -> (name, [%e enum_expr])) [%e expr]]
29

30
let core_type_of_type_name ~loc name =
31
  let ct =
2✔
32
    let lid = Utils.longident_loc_of_name name in
33
    Ast_helper.Typ.constr ~loc lid []
2✔
34
  in
35
  [%type: unit -> [%t ct] Cmdliner.Arg.conv]
36

37
let structure_of_const_decls ~loc name (cds : constructor_declaration list) =
38
  let stri =
1✔
39
    let pat = Ast_helper.Pat.var ~loc @@ gen_name name
1✔
40
    and ct = core_type_of_type_name ~loc name
1✔
41
    and expr =
42
      let enum_exprs = List.map expression_of_const_decl cds in
43
      Ast_builder.Default.elist ~loc enum_exprs
1✔
44
    in
45
    [%stri
46
      let ([%p pat] : [%t ct]) =
47
       fun () ->
48
        let enums = List.concat [%e expr] in
49
        Cmdliner.Arg.enum enums]
50
  in
51
  [ stri ]
52

53
let signature_of_const_decls ~loc name =
54
  let sigi =
1✔
55
    let fun_name = gen_name name and ct = core_type_of_type_name ~loc name in
1✔
56
    Ast_helper.Val.mk ~loc fun_name ct |> Ast_helper.Sig.value
1✔
57
  in
58
  [ sigi ]
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc