Skip to content

Commit 0a7fda6

Browse files
committed
use metaquot for ppx_deriving_err_case
1 parent fbd1ac3 commit 0a7fda6

2 files changed

Lines changed: 10 additions & 16 deletions

File tree

src/ppx/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@
3838
(public_name ez_api.ppx_err_case)
3939
(optional)
4040
(modules ppx_deriving_err_case)
41+
(preprocess (pps ppxlib.metaquot))
4142
(kind ppx_deriver)
4243
(libraries ppx_deriving_encoding.lib))

src/ppx/ppx_deriving_err_case.ml

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,15 @@ let mk ~loc ?enc ?(kind_label="kind") ~title name code =
2525
else encoding in
2626
let select = pexp_function ~loc [
2727
case ~guard:None
28-
~lhs:(ppat_variant ~loc name (Option.map (fun _ -> pvar ~loc "x") enc))
29-
~rhs:(Option.fold ~none:(Utils.esome (eunit ~loc))
30-
~some:(fun _ -> Utils.esome (pexp_tuple ~loc [eunit ~loc; evar ~loc "x"])) enc) ;
31-
case ~guard:None
32-
~lhs:(ppat_any ~loc)
33-
~rhs:(Utils.enone ~loc) ] in
28+
~lhs:(ppat_variant ~loc name (Option.map (fun _ -> [%pat? x]) enc))
29+
~rhs:(Option.fold ~none:[%expr Some ()] ~some:(fun _ -> [%expr Some ((), x)]) enc);
30+
case ~guard:None ~lhs:[%pat? _] ~rhs:[%expr None] ] in
3431
let deselect = Utils.pexp_fun
35-
(Option.fold ~none:(punit ~loc) ~some:(fun _ -> ppat_tuple ~loc [punit ~loc; pvar ~loc "x"]) enc)
36-
(pexp_variant ~loc name (Option.map (fun _ -> evar ~loc "x") enc)) in
37-
pexp_apply ~loc (evar ~loc "EzAPI.Err.make") [
38-
Labelled "code", eint ~loc code;
39-
Labelled "name", estring ~loc name;
40-
Labelled "encoding", encoding;
41-
Labelled "select", select;
42-
Labelled "deselect", deselect ]
32+
(Option.fold ~none:[%pat? ()] ~some:(fun _ -> [%pat? ((), x)]) enc)
33+
(pexp_variant ~loc name (Option.map (fun _ -> [%expr x]) enc)) in
34+
[%expr
35+
EzAPI.Err.make ~code:[%e eint ~loc code] ~name:[%e estring ~loc name]
36+
~encoding:[%e encoding] ~select:[%e select] ~deselect:[%e deselect] ]
4337

4438
let get_int_attr = function
4539
| PStr [{pstr_desc = Pstr_eval ({pexp_desc = Pexp_constant (Pconst_integer (s, None)); _}, _); _}] ->
@@ -78,8 +72,7 @@ let str_gen ~loc ~path:_ (rec_flag, l) debug title kind_label =
7872
let cases = expressions ?kind_label ~title t in
7973
List.map (fun (name, expr) ->
8074
let pat = ppat_constraint ~loc (pvar ~loc (String.lowercase_ascii name ^ "_case"))
81-
(ptyp_constr ~loc (Utils.llid ~loc "EzAPI.Err.case") [
82-
ptyp_constr ~loc (Utils.llid ~loc t.ptype_name.txt) [] ]) in
75+
[%type: [%t ptyp_constr ~loc (Utils.llid ~loc t.ptype_name.txt) []] EzAPI.Err.case] in
8376
value_binding ~loc ~pat ~expr) cases) l in
8477
let l = List.flatten l in
8578
let rec_flag = if List.length l < 2 then Nonrecursive else rec_flag in

0 commit comments

Comments
 (0)