package bonsai

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

Module type Variant.SSource

You can derive this module with @@deriving typed_variants on t.

Sourceval parser_for_variant : 'a Typed_variant.t -> 'a t

Given a typed variant, it should return the parser that you want to use for that variant's constructor. For example:


  module My_url = struct
    type t =
      | Foo
      | Bar of int
    [@@deriving typed_variants, sexp, equal]

    let parser_for_variant : type a. a Typed_variant.t -> a Parser.t = function
      | Foo -> Parser.unit
      | Bar -> Parser.from_path Value_parser.int
  end

  let my_parser = Variant.make (module My_url)

Which variant constructor is used for a given url? The following hierarchy exists:

1. If the given parser has a "constant" part of a path, like with_prefix or with_remaining_path, then the very next one in the parse chain is used. 2. If the given parser does not have any "constant" parts of a path, then the constructors name is used. 3. If two constructors, both match, the one with most specificity is used (i.e longest match first and if tie, then from_remaining_path wins over with_prefix). Furthermore, if there is a "tie", then this is a static error.