mte/src/amount.ml

127 lines
3.4 KiB
OCaml
Raw Normal View History

2025-10-13 00:02:35 +02:00
(* TODO
2026-02-11 00:06:28 +01:00
have a currency agnostic amount_lib.ml
and specialize amount.ml to Config.currency??
have safe amount arithmetic *)
2025-11-20 22:14:45 +01:00
type sign =
| Sign_plus
| Sign_minus
2025-10-13 00:02:35 +02:00
type t = {
2025-11-20 22:14:45 +01:00
sign: sign option;
currency: string;
2025-10-13 00:02:35 +02:00
value: Int64.t;
fraction: Int32.t;
2025-10-13 00:02:35 +02:00
}
2026-02-11 00:31:03 +01:00
let value_upper_bound = Int64.of_float @@ Float.pow 2. 52.
2026-02-11 00:06:28 +01:00
(* TODO
the constraint is on the number of digits,
so this wrong if leading 0s
this depends on currency..? *)
let fraction_upper_bound = Int32.of_int 100_000_000
2025-10-13 04:15:58 +02:00
2025-10-13 00:02:35 +02:00
let make ~sign ~currency ~value ~fraction =
2026-02-11 00:06:28 +01:00
if value < Int64.zero then Error "value is negative"
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 }
2025-10-13 00:02:35 +02:00
2026-02-11 00:06:28 +01:00
module Parse = struct
open Angstrom
2025-11-20 18:38:01 +01:00
2026-02-11 00:06:28 +01:00
let sign =
char '+' *> return (Some Sign_plus)
<|> char '-' *> return (Some Sign_minus)
<|> return None
2025-10-13 00:02:35 +02:00
2026-02-11 00:06:28 +01:00
let currency =
take_while1 (function 'a' .. 'z' | 'A' .. 'Z' -> true | _ -> false)
>>= fun s ->
match String.length s < 12 with
| false -> fail "currency is more than 11 characters"
| true -> return s
let int64 =
2025-10-13 00:02:35 +02:00
take_while1 (function '0' .. '9' -> true | _ -> false)
>>| Int64.of_string_opt
>>= function
2026-02-11 00:06:28 +01:00
| None -> fail "value is not a valid int64"
| Some n when n >= value_upper_bound -> fail "value is greater than 2^52-1"
2025-10-13 00:02:35 +02:00
| Some n -> return n
2026-02-11 00:06:28 +01:00
let int32 =
take_while1 (function '0' .. '9' -> true | _ -> false)
>>| Int32.of_string_opt
>>= function
2026-02-11 00:06:28 +01:00
| None -> fail "fraction is not a valid int32"
| Some n when n >= fraction_upper_bound ->
fail "fraction is greater than 10^8-1"
| Some n -> return n
2026-02-11 00:06:28 +01:00
let amount =
2025-10-13 00:02:35 +02:00
lift4
(fun sign currency value fraction ->
make ~sign ~currency ~value ~fraction)
2026-02-11 00:06:28 +01:00
sign currency
(char ':' *> int64)
(char '.' *> int32 <|> return 0_l)
<* end_of_input
let f s = parse_string ~consume:Consume.All amount s |> Result.join
end
let of_string = Parse.f
let pp =
let open Fmt in
let pp_sign ppf = function
| Sign_plus -> char ppf '+'
| Sign_minus -> char ppf '-'
2025-10-13 00:02:35 +02:00
in
2026-02-11 00:06:28 +01:00
fun ppf { sign; currency; value; fraction } ->
(* TODO
this depends on the currency's number of fraction digits *)
pf ppf "%a%s:%Ld.%02ld" (Fmt.option pp_sign) sign currency value fraction
let to_string = Fmt.str "%a" pp
(* - *)
2025-11-28 14:00:11 +01:00
let jsont = Jsont.of_of_string ~kind:"Amount" of_string ~enc:to_string
2026-02-11 00:06:28 +01:00
(* byte length of currency string *)
2025-11-28 14:00:11 +01:00
let currency_len = 12
2026-02-11 00:31:03 +01:00
let pad_currency_string s =
2025-11-28 19:17:25 +01:00
let len = String.length s in
2026-02-11 00:31:03 +01:00
assert (len < currency_len);
2025-11-28 19:17:25 +01:00
let b = Bytes.make 12 '\x00' in
Bytes.blit_string s 0 b 0 len;
Bytes.to_string b
2026-02-11 00:31:03 +01:00
(* binary decoding unused? *)
let make_exn value fraction currency =
match make ~sign:None ~currency ~value ~fraction with
| Error _ -> Fmt.failwith "Amount of binary data failure"
| Ok v -> v
2025-11-28 14:00:11 +01:00
let bin =
let open Bin in
2026-02-11 00:31:03 +01:00
record make_exn
2025-11-28 14:00:11 +01:00
|+ field neint64 (fun t -> t.value)
|+ field neint32 (fun t -> t.fraction)
2026-02-11 00:31:03 +01:00
|+ field (bytes currency_len) (fun t -> pad_currency_string t.currency)
2025-11-28 14:00:11 +01:00
|> sealr
let bin_nbo =
let open Bin in
2026-02-11 00:31:03 +01:00
record make_exn
2025-11-28 14:00:11 +01:00
|+ field beint64 (fun t -> t.value)
|+ field beint32 (fun t -> t.fraction)
2026-02-11 00:31:03 +01:00
|+ field (bytes currency_len) (fun t -> pad_currency_string t.currency)
2025-11-28 14:00:11 +01:00
|> sealr