package catala

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module Clerk_lib.Clerk_toml_encodingSource

Overview

This module provides constructors and combinators that allow to describe the structure of an expected TOML file. It also provide a type-safe encoder/decoder.

Types

Sourcetype 'a descr
Sourcetype 'a field
Sourcetype 'a case
Sourcetype 'a table_descr
Sourcetype 'a t = 'a table_descr

Encoding functions

Sourceval decode : Otoml.t -> 'a t -> 'a
Sourceval encode : 'a -> 'a t -> Otoml.t

Constructors & Combinators

Basic constructors

Sourceval string : string descr
Sourceval bool : bool descr
Sourceval pair : 'a descr -> 'b descr -> ('a * 'b) descr
Sourceval list : 'a descr -> 'a list descr
Sourceval binding_list : 'a descr -> (string * 'a) list descr

Object's Field constructors

Sourceval req_field : name:string -> 'a descr -> 'a field
Sourceval opt_field : name:string -> 'a descr -> 'a option field
Sourceval dft_field : name:string -> default:'a -> 'a descr -> 'a field

Object constructors

Objects are translated as inner tables in TOML: e.g., obj2 (req_field ~name:"a" string) (req_field ~name:"b" (list string)) described under a table "target" will match the following TOML:

[target]
a = "some string"
b = [ "hello" ; "world" ]

Warning: registering multiple fields in an object under the same name will raise an error at runtime when the descriptor gets evaluated.

Sourceval obj1 : 'a field -> 'a descr
Sourceval obj2 : 'a field -> 'b field -> ('a * 'b) descr
Sourceval obj3 : 'a field -> 'b field -> 'c field -> ('a * 'b * 'c) descr
Sourceval obj4 : 'a field -> 'b field -> 'c field -> 'd field -> ('a * 'b * 'c * 'd) descr
Sourceval obj5 : 'a field -> 'b field -> 'c field -> 'd field -> 'e field -> ('a * 'b * 'c * 'd * 'e) descr
Sourceval obj6 : 'a field -> 'b field -> 'c field -> 'd field -> 'e field -> 'f field -> ('a * 'b * 'c * 'd * 'e * 'f) descr
Sourceval merge_objs : 'a descr -> 'b descr -> ('a * 'b) descr

Union

Sourceval case : info:string -> proj:('a -> 'b option) -> inj:('b -> 'a option) -> 'b descr -> 'a case

Union's case: the info is used to describe the case to the user.

Sourceval union : 'a case list -> 'a descr

union cases will create a descriptor that matches several patterns, e.g., to describe a sum-type. The first case found for which its proj function returns Some _ will be used.

Sourceval string_cases : (string * 'a) list -> 'a case list

Table constructors

Tables are translated as toplevel tables in TOML: e.g., table2 (table_opt ~name:"project" (...)) (multi_table ~name:"module" (...)) will match the following TOML:

[project]
...

[[module]]
...

[[module]]
...

N.b., the project table could be omitted

Warning: describing different toplevel tables using the same name will raise an error at runtime when the descriptor gets evaluated.

Sourceval table_req : name:string -> 'a descr -> 'a t
Sourceval table_opt : name:string -> 'a descr -> 'a option t
Sourceval multi_table : name:string -> 'a descr -> 'a list t
Sourceval table2 : 'a t -> 'b t -> ('a * 'b) t
Sourceval table3 : 'a t -> 'b t -> 'c t -> ('a * 'b * 'c) t
Sourceval table4 : 'a t -> 'b t -> 'c t -> 'd t -> ('a * 'b * 'c * 'd) t
Sourceval table5 : 'a t -> 'b t -> 'c t -> 'd t -> 'e t -> ('a * 'b * 'c * 'd * 'e) t
Sourceval table6 : 'a t -> 'b t -> 'c t -> 'd t -> 'e t -> 'f t -> ('a * 'b * 'c * 'd * 'e * 'f) t
Sourceval merge_tables : 'a t -> 'b t -> ('a * 'b) t

Conversion operators

Sourceval conv : ('a -> 'b) -> ('b -> 'a) -> 'b descr -> 'a descr
Sourceval convt : ('a -> 'b) -> ('b -> 'a) -> 'b t -> 'a t

Utilities

Sourceval proj_empty_list : 'a list -> 'a list option
Sourceval inj_empty_list : 'a list option -> 'a list