Module Spindle_http.Structured

Structured Fields, as RFC 9651 defines them for the fields built on it: an item, a list or a dictionary, each value with parameters.

Structured.dictionary "a=1, b;q=5, c=(x \"y\")"
= Ok
    [
      ("a", Item (Integer 1, []));
      ("b", Item (Boolean true, [ ("q", Integer 5) ]));
      ("c", Inner ([ (Token "x", []); (String "y", []) ], []));
    ]

A field defines which of the three it is, so the caller says which it reads. A field given on several lines is read from its lines joined with ", " (RFC 9651 §4.2).

type bare =
  1. | Integer of int
    (*

    fifteen digits at most, signed

    *)
  2. | Decimal of int
    (*

    thousandths: RFC 9651 bounds a decimal to three fractional digits, so 1.5 is 1500 and every decimal is exact

    *)
  3. | String of string
    (*

    printable ASCII

    *)
  4. | Token of string
  5. | Bytes of string
    (*

    the bytes, their base64 undone

    *)
  6. | Boolean of bool
  7. | Date of Ptime.t
    (*

    whole seconds as a field holds it: one written with a fraction of a second is written in the second it is in, and one read outside the years 0 to 9999, which RFC 9651 allows and no instant holds, is refused

    *)
  8. | Display of string
    (*

    UTF-8, which a String cannot hold

    *)
type parameters = (string * bare) list

In order, each key once: a key given twice keeps its first place and its last value, as §4.2.3.2 has it.

type item = bare * parameters
type member =
  1. | Item of item
  2. | Inner of item list * parameters
    (*

    an inner list, and its parameters

    *)
type dictionary = (string * member) list

In order, each key once, as parameters.

val item : string -> (item, string) Stdlib.result
val list : string -> (member list, string) Stdlib.result
val dictionary : string -> (dictionary, string) Stdlib.result
val item_to_string : item -> (string, string) Stdlib.result

An item as §4.1 writes it, or Error where it holds what no field can: an integer past fifteen digits, a decimal past twelve, a string with a character outside printable ASCII, a key or token that is not one, or a key given twice among one value's parameters or a dictionary's members. item reads it back as itself.

val list_to_string : member list -> (string, string) Stdlib.result
val dictionary_to_string : dictionary -> (string, string) Stdlib.result