Data encoding: fail on string_enum that should be a constant

This commit is contained in:
Milo Davis 2018-01-10 20:41:52 -05:00 committed by Grégoire Henry
parent c77cbcb30b
commit f00c7c0667

View File

@ -535,11 +535,14 @@ module Encoding = struct
let array e = dynamic_size (Variable.array e)
let list e = dynamic_size (Variable.list e)
let string_enum cases =
let arr = Array.of_list (List.map snd cases) in
let tbl = Hashtbl.create (Array.length arr) in
List.iteri (fun ind (str, a) -> Hashtbl.add tbl a (str, ind)) cases ;
make @@ String_enum (tbl, arr)
let string_enum = function
| [] -> invalid_arg "data_encoding.string_enum: cannot have zero cases"
| [ _case ] -> invalid_arg "data_encoding.string_enum: cannot have a single case, use constant instead"
| _ :: _ as cases ->
let arr = Array.of_list (List.map snd cases) in
let tbl = Hashtbl.create (Array.length arr) in
List.iteri (fun ind (str, a) -> Hashtbl.add tbl a (str, ind)) cases ;
make @@ String_enum (tbl, arr)
let conv proj inj ?schema encoding =
make @@ Conv { proj ; inj ; encoding ; schema }