fix alt_unit_names

This commit is contained in:
swrup 2026-02-27 22:05:47 +01:00 committed by Swrup
parent 6886d14027
commit b7892fdca9
4 changed files with 45 additions and 56 deletions

View file

@ -212,34 +212,3 @@ let etag s =
match Headers_lib.Etag.parse (Fmt.str "\"%s\"" s) with
| Ok etag -> etag
| Error _ -> fail "could not parse etag `%s`: %s" s e)
(* TODO
move to another module
can we type the json as a Int_map directly? *)
module Alt_unit_names = struct
open Syntax
module String_map = Map.Make (String)
let string_map_jsont = Jsont.Object.as_string_map Jsont.string
let decode s =
let* string_map = Jsont_bytesrw.decode_string string_map_jsont s in
let l = String_map.to_list string_map in
let* l =
list_map
(fun (k, v) ->
match int_of_string_opt k with
| None -> Error "alt_unit_names has a non-integer key"
| Some k -> Ok (k, v))
l
in
match List.find_opt (fun (i, _) -> i = 0) l with
| None -> Error "alt_unit_names with no entry for base value \"0\""
| Some _ -> Ok l
let encode l =
let l = List.map (fun (k, v) -> (string_of_int k, v)) l in
let string_map = String_map.of_list l in
let+ s = Jsont_bytesrw.encode_string string_map_jsont string_map in
s
end