use jsont for alt_unit_names
This commit is contained in:
parent
99d2ee6194
commit
7b8edd05e9
3 changed files with 29 additions and 42 deletions
|
|
@ -104,7 +104,8 @@ module Currency = struct
|
|||
fractional_normal_digits= get "fractional_normal_digits" |> int;
|
||||
fractional_trailing_zero_digits=
|
||||
get "fractional_trailing_zero_digits" |> int;
|
||||
alt_unit_names= get "alt_unit_names" |> Alt_unit_names.parse;
|
||||
alt_unit_names=
|
||||
get "alt_unit_names" |> Alt_unit_names.decode |> Parse_config.unwrap_res;
|
||||
}
|
||||
|
||||
let all_currencies = List.map parse_currency currency_sections
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ let mk_keys ~db_conn ~sm ~last_issue_date =
|
|||
let currency_specification =
|
||||
let v = Config.Currency.v in
|
||||
let alt_unit_names =
|
||||
Parse_config.Alt_unit_names.to_json_string v.alt_unit_names
|
||||
Parse_config.Alt_unit_names.encode_exn v.alt_unit_names
|
||||
in
|
||||
CurrencySpecification.
|
||||
{
|
||||
|
|
|
|||
|
|
@ -228,49 +228,35 @@ let ed25519 s =
|
|||
|> Result.map_error (fun e -> Fmt.str "%a" Mirage_crypto_ec.pp_error e)
|
||||
|> unwrap_res
|
||||
|
||||
(* TODO alt_unit_names jsont *)
|
||||
(* TODO
|
||||
move to another module
|
||||
can we type the json as a Int_map directly? *)
|
||||
module 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
|
||||
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
|
||||
| None -> Error "alt_unit_names has a non-integer key"
|
||||
| Some k -> Ok (k, v))
|
||||
l
|
||||
in
|
||||
(k, v))
|
||||
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 to_json_string l =
|
||||
let l = List.map (fun (i, s) -> Fmt.str {|"%d": "%s"|} i s) l in
|
||||
let s = String.concat "," l in
|
||||
"{" ^ s ^ "}"
|
||||
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