wip fix amount

This commit is contained in:
swrup 2026-03-01 00:35:59 +01:00
parent 3485d83a3e
commit 7be98eb8ad

View file

@ -14,67 +14,96 @@ type t = {
fraction: Int32.t; fraction: Int32.t;
} }
let maximum_currency_length = 11
let value_upper_bound = Int64.of_float @@ Float.pow 2. 52. let value_upper_bound = Int64.of_float @@ Float.pow 2. 52.
let fraction_max_number_of_digits = 8
(* TODO (* TODO choose to either include or exclude upper limit for all bounds
the constraint is on the number of digits, need a -1 here *)
so this wrong if leading 0s let fraction_upper_bound =
this depends on currency..? *) Int32.of_float @@ Float.pow 10. (Int.to_float fraction_max_number_of_digits)
let fraction_upper_bound = Int32.of_int 100_000_000
let check_currency s =
if not @@ String.for_all is_letter s then
Error "currency contains forbidden character"
else
let len = String.length s in
if len = 0 then Error "currency is empty string"
else if len > maximum_currency_length then
Fmt.error "currency is more than %d characters long"
maximum_currency_length
else Ok ()
let check_value v =
if Int64.unsigned_compare v value_upper_bound > 0 then
Error "value is greater than 2^52"
else Ok v
let check_fraction v =
if Int32.unsigned_compare v fraction_upper_bound > 0 then
Fmt.error "fraction has more than %d digits" fraction_max_number_of_digits
else Ok v
let make ~sign ~currency ~value ~fraction = let make ~sign ~currency ~value ~fraction =
if value < Int64.zero then Error "value is negative" Ok { sign; currency; value; fraction }
else if fraction < Int32.zero then Error "fraction is negative"
else if value > value_upper_bound then Error "value is greater than 2^52-1"
else if fraction >= fraction_upper_bound then
Error "fraction has more than 8 decimal digits"
else Ok { sign; currency; value; fraction }
module Parse = struct open Angstrom
open Angstrom
let sign = let is_letter = function 'a' .. 'z' | 'A' .. 'Z' -> true | _ -> false
char '+' *> return (Some Sign_plus) let is_digit = function '0' .. '9' -> true | _ -> false
<|> char '-' *> return (Some Sign_minus) let letters = take_while1 is_letter
<|> return None let digits = take_while1 is_digit
let currency = let sign =
take_while1 (function 'a' .. 'z' | 'A' .. 'Z' -> true | _ -> false) choice
>>= fun s -> [
match String.length s < 12 with char '+' *> return (Some Sign_plus);
| false -> fail "currency is more than 11 characters" char '-' *> return (Some Sign_minus);
| true -> return s return None;
]
let int64 = let currency_of_string s =
take_while1 (function '0' .. '9' -> true | _ -> false) if not @@ String.for_all is_letter s then
>>| Int64.of_string_opt Error "currency contains forbidden character"
>>= function else if String.length s > maximum_currency_length then
| None -> fail "value is not a valid int64" Fmt.error "currency is more than %d characters long" maximum_currency_length
| Some n when n >= value_upper_bound -> fail "value is greater than 2^52-1" else Ok (String.uppercase_ascii s)
| Some n -> return n
let int32 = let value_of_string s =
take_while1 (function '0' .. '9' -> true | _ -> false) if not @@ String.for_all is_digit s then
>>| Int32.of_string_opt Error "value contains forbidden character"
>>= function else
| None -> fail "fraction is not a valid int32" match Int64.of_string_opt s with
| Some n when n >= fraction_upper_bound -> | None -> Error "value is not a valid integer"
fail "fraction is greater than 10^8-1" | Some v ->
| Some n -> return n if v > value_upper_bound then Error "value is greater than 2^52"
else if v < Int64.zero then Error "value is negative"
else Ok v
let amount = let fraction_of_string s =
lift4 if not @@ String.for_all is_digit s then
(fun sign currency value fraction -> Error "fraction contains forbidden character"
make ~sign ~currency ~value ~fraction) else if String.length s > fraction_max_number_of_digits then
sign currency Fmt.error "fraction has more than %d digits" fraction_max_number_of_digits
(char ':' *> int64) else
(char '.' *> int32 <|> return 0_l) match Int32.of_string_opt s with
<* end_of_input | None -> Error "fraction is not a valid integer"
| Some v -> if v < Int32.zero then Error "fraction is negative" else Ok v
let f s = parse_string ~consume:Consume.All amount s |> Result.join let amount =
end lift4
(fun sign currency value fraction ->
let open Syntax in
let* currency = currency_of_string currency in
let* value = value_of_string value in
let* fraction = fraction_of_string fraction in
Ok { sign; currency; value; fraction })
sign letters
(char ':' *> digits)
(option "" (char '.' *> digits))
<* end_of_input
let of_string = Parse.f let of_string s = parse_string ~consume:Consume.All amount s |> Result.join
let pp = let pp =
let open Fmt in let open Fmt in
@ -92,9 +121,7 @@ let to_string = Fmt.str "%a" pp
(* - *) (* - *)
let jsont = Jsont.of_of_string ~kind:"Amount" of_string ~enc:to_string let jsont = Jsont.of_of_string ~kind:"Amount" of_string ~enc:to_string
let currency_length = 12
(* byte length of currency string *)
let currency_len = 12
let pad_currency_string s = let pad_currency_string s =
let len = String.length s in let len = String.length s in