~~~
This commit is contained in:
parent
099cd80670
commit
b507d540ad
43 changed files with 5647 additions and 1808 deletions
|
|
@ -228,43 +228,35 @@ let ed25519 s =
|
|||
|> Result.map_error (fun e -> Fmt.str "%a" Mirage_crypto_ec.pp_error e)
|
||||
|> unwrap_res
|
||||
|
||||
module Parse_alt_unit_names = struct
|
||||
let rm_brackets s =
|
||||
let s = String.trim s in
|
||||
match
|
||||
String.starts_with ~prefix:"{" s && String.ends_with ~suffix:"}" s
|
||||
with
|
||||
| false -> fail "expected json, got `%s`" s
|
||||
| true ->
|
||||
let s = String.sub s 1 (String.length s - 2) in
|
||||
s
|
||||
(* 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 rm_quotes s =
|
||||
let s = String.trim s in
|
||||
match
|
||||
String.starts_with ~prefix:"\"" s && String.ends_with ~suffix:"\"" s
|
||||
with
|
||||
| false -> fail "expected quoted string, got `%s`" s
|
||||
| true ->
|
||||
let s = String.sub s 1 (String.length s - 2) in
|
||||
s
|
||||
let string_map_jsont = Jsont.Object.as_string_map Jsont.string
|
||||
|
||||
let parse s =
|
||||
let s = rm_brackets s in
|
||||
String.split_on_char ',' s
|
||||
|> List.map (String.split_on_char ':')
|
||||
|> List.map (function
|
||||
| [ k; v ] -> (k, v)
|
||||
| _ -> fail "invalid json key-value map")
|
||||
|> List.map (fun (k, v) ->
|
||||
let k = rm_quotes k in
|
||||
let v = rm_quotes v in
|
||||
let k =
|
||||
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 ->
|
||||
fail "invalid json key-value map, expected integer key, got `%s`"
|
||||
k
|
||||
| Some k -> k
|
||||
in
|
||||
(k, v))
|
||||
| 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
|
||||
|
||||
let encode_exn l = encode l |> Result.get_ok
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue