Module Ppx_deriving_tools.ConvSource

A common scheme to define data conversions (like to_json/of_json).

A simplified parsetree representation.

We define a few types to represent the data we want to derive conversions for. Such types are less verbose but less precise than the original parsetree, though it is enough for conversion purposes.

The types still keep the original parsetree nodes as context (this is also needed to play well with Ppxlib.Attributes API).

Sourcetype 'ctx tuple = {
  1. tpl_loc : Ppxlib.location;
  2. tpl_types : Ppxlib.core_type list;
  3. tpl_ctx : 'ctx;
}
Sourcetype 'ctx record = {
  1. rcd_loc : Ppxlib.location;
  2. rcd_fields : Ppxlib.label_declaration list;
  3. rcd_ctx : 'ctx;
}
Sourceand variant_case_ctx =
  1. | Vcs_ctx_variant of Ppxlib.constructor_declaration
  2. | Vcs_ctx_polyvariant of Ppxlib.row_field
Sourcetype variant = {
  1. vrt_loc : Ppxlib.location;
  2. vrt_cases : variant_case list;
  3. vrt_ctx : variant_ctx;
}
Sourceand variant_ctx =
  1. | Vrt_ctx_variant of Ppxlib.type_declaration
  2. | Vrt_ctx_polyvariant of Ppxlib.core_type

Define a serializer.

Sourceval deriving_of : name:Ppxlib.label -> of_t:(loc:Ppxlib.location -> Ppxlib.core_type) -> is_allow_any_constr:(variant_case_ctx -> bool) -> derive_of_tuple: (derive_of_core_type -> Ppxlib.core_type tuple -> Ppxlib.expression -> Ppxlib.expression) -> derive_of_record: (derive_of_core_type -> Ppxlib.type_declaration record -> Ppxlib.expression -> Ppxlib.expression) -> derive_of_variant: (derive_of_core_type -> variant -> allow_any_constr:(Ppxlib.expression -> Ppxlib.expression) option -> Ppxlib.expression -> Ppxlib.expression -> Ppxlib.expression) -> derive_of_variant_case: (derive_of_core_type -> (Ppxlib.expression option -> Ppxlib.expression) -> variant_case -> allow_any_constr:(Ppxlib.expression -> Ppxlib.expression) option -> Ppxlib.expression -> Ppxlib.expression) -> unit -> deriving

Define a deserializer.

Sourceval deriving_of_match : name:Ppxlib.label -> of_t:(loc:Ppxlib.location -> Ppxlib.core_type) -> cmp_sort_vcs:(variant_case_ctx -> variant_case_ctx -> int) -> derive_of_tuple: (derive_of_core_type -> Ppxlib.core_type tuple -> Ppxlib.expression -> Ppxlib.expression) -> derive_of_record: (derive_of_core_type -> Ppxlib.type_declaration record -> Ppxlib.expression -> Ppxlib.expression) -> derive_of_variant_case: (derive_of_core_type -> (Ppxlib.expression option -> Ppxlib.expression) -> variant_case -> Ppxlib.case) -> unit -> deriving

Define a deserializer using pattern matching.

This is a less general but more compact variant of deriving_of, for cases where the serialized data can be inspected with pattern matching.