From 7b8edd05e9305710d1071e5fca11ea5af24ba109 Mon Sep 17 00:00:00 2001 From: swrup Date: Mon, 22 Dec 2025 19:20:32 +0100 Subject: [PATCH] use jsont for alt_unit_names --- src/config.ml | 3 ++- src/http_keys.ml | 2 +- src/parse_config.ml | 66 ++++++++++++++++++--------------------------- 3 files changed, 29 insertions(+), 42 deletions(-) diff --git a/src/config.ml b/src/config.ml index 2228421c..7c62ffb2 100644 --- a/src/config.ml +++ b/src/config.ml @@ -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 diff --git a/src/http_keys.ml b/src/http_keys.ml index 48ae48ce..481c1f57 100644 --- a/src/http_keys.ml +++ b/src/http_keys.ml @@ -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. { diff --git a/src/parse_config.ml b/src/parse_config.ml index 0f53e4d3..f40f1885 100644 --- a/src/parse_config.ml +++ b/src/parse_config.ml @@ -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 - 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 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