2025-10-13 00:02:35 +02:00
|
|
|
(* TODO
|
|
|
|
|
have safe amount arithmetic
|
2025-10-18 20:28:38 +02:00
|
|
|
make type private
|
2025-11-20 22:14:45 +01:00
|
|
|
redefine an Amount module with currency enforced to be Config.currency? *)
|
2025-10-13 00:02:35 +02:00
|
|
|
(* Amounts of currency, serialized as `<Currency>:<IntegerPart>.<FractionalPart>`
|
|
|
|
|
Fixed-precision numbers with 8 decimal places.
|
|
|
|
|
- <Currency> must be at most 11 characters long
|
|
|
|
|
only consist of ASCII letters (a-zA-Z).
|
|
|
|
|
- integer part of <DecimalAmount> may be at most 2^52.
|
|
|
|
|
- fractional part of <DecimalAmount> may contain at most 8 decimal digits.
|
|
|
|
|
|
|
|
|
|
Prefixed with '+' or '-' in certain contexts.
|
|
|
|
|
When no sign is present, the amount is assumed to be positive. *)
|
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;
|
2025-11-16 15:28:14 +01:00
|
|
|
fraction: Int32.t;
|
2025-10-13 00:02:35 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-13 04:15:58 +02:00
|
|
|
let ( let* ) o f = match o with Ok v -> f v | Error _ as e -> e
|
2025-10-18 20:28:38 +02:00
|
|
|
let check_not msg = function false -> Ok () | true -> Error msg
|
|
|
|
|
let value_upper_bound = Z.(pow (of_int 2) 52) |> Z.to_int64
|
2025-11-16 15:28:14 +01:00
|
|
|
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 =
|
2025-10-18 20:28:38 +02:00
|
|
|
let* () = check_not "value is negative" (value < Int64.zero) in
|
2025-11-16 15:28:14 +01:00
|
|
|
let* () = check_not "fraction is negative" (fraction < Int32.zero) in
|
2025-10-13 00:02:35 +02:00
|
|
|
let* () =
|
2025-10-18 20:28:38 +02:00
|
|
|
check_not "value is greater than 2^52" (value > value_upper_bound)
|
2025-10-13 00:02:35 +02:00
|
|
|
in
|
|
|
|
|
let* () =
|
2025-10-18 20:28:38 +02:00
|
|
|
check_not "fraction has more than 8 decimal digits"
|
|
|
|
|
(fraction >= fraction_upper_bound)
|
2025-10-13 00:02:35 +02:00
|
|
|
in
|
|
|
|
|
Ok { sign; currency; value; fraction }
|
|
|
|
|
|
2025-11-20 18:38:01 +01:00
|
|
|
let pp =
|
|
|
|
|
let open Fmt in
|
2025-11-20 22:14:45 +01:00
|
|
|
let pp_sign ppf = function
|
|
|
|
|
| Sign_plus -> char ppf '+'
|
|
|
|
|
| Sign_minus -> char ppf '-'
|
|
|
|
|
in
|
2025-11-20 18:38:01 +01:00
|
|
|
fun ppf { sign; currency; value; fraction } ->
|
2025-11-20 22:14:45 +01:00
|
|
|
pf ppf "%a%s:%Ld.%02ld" (Fmt.option pp_sign) sign currency value fraction
|
2025-11-20 18:38:01 +01:00
|
|
|
|
|
|
|
|
let to_string = Fmt.str "%a" pp
|
2025-10-13 00:02:35 +02:00
|
|
|
|
|
|
|
|
let of_string =
|
|
|
|
|
let open Angstrom in
|
|
|
|
|
let parse_sign =
|
|
|
|
|
choice
|
|
|
|
|
[
|
2025-11-20 22:14:45 +01:00
|
|
|
char '+' *> return (Some Sign_plus);
|
2025-11-29 19:36:35 +01:00
|
|
|
char '-' *> return (Some Sign_minus);
|
|
|
|
|
return None;
|
2025-10-13 00:02:35 +02:00
|
|
|
]
|
|
|
|
|
in
|
2025-11-20 22:14:45 +01:00
|
|
|
let parse_currency =
|
|
|
|
|
(* TODO currency string constraint/format *)
|
|
|
|
|
take_while1 (function
|
|
|
|
|
| 'a' .. 'z' | 'A' .. 'Z' -> true
|
|
|
|
|
| _ -> false)
|
|
|
|
|
in
|
2025-11-16 15:28:14 +01:00
|
|
|
let parse_int64 =
|
2025-10-13 00:02:35 +02:00
|
|
|
take_while1 (function '0' .. '9' -> true | _ -> false)
|
|
|
|
|
>>| Int64.of_string_opt
|
|
|
|
|
>>= function
|
|
|
|
|
| None -> fail "invalid integer"
|
|
|
|
|
| Some n -> return n
|
|
|
|
|
in
|
2025-11-16 15:28:14 +01:00
|
|
|
let parse_int32 =
|
|
|
|
|
take_while1 (function '0' .. '9' -> true | _ -> false)
|
|
|
|
|
>>| Int32.of_string_opt
|
|
|
|
|
>>= function
|
|
|
|
|
| None -> fail "invalid integer"
|
|
|
|
|
| Some n -> return n
|
|
|
|
|
in
|
2025-10-13 00:02:35 +02:00
|
|
|
let parse_t =
|
|
|
|
|
lift4
|
|
|
|
|
(fun sign currency value fraction ->
|
|
|
|
|
make ~sign ~currency ~value ~fraction)
|
|
|
|
|
parse_sign parse_currency
|
2025-11-16 15:28:14 +01:00
|
|
|
(char ':' *> parse_int64)
|
|
|
|
|
(char '.' *> parse_int32)
|
2025-10-13 00:02:35 +02:00
|
|
|
in
|
|
|
|
|
fun s -> parse_string ~consume:Consume.All parse_t s |> Result.join
|
2025-11-28 14:00:11 +01:00
|
|
|
|
|
|
|
|
let jsont = Jsont.of_of_string ~kind:"Amount" of_string ~enc:to_string
|
|
|
|
|
let currency_len = 12
|
|
|
|
|
|
2025-11-28 19:17:25 +01:00
|
|
|
let pad_currency s =
|
|
|
|
|
let len = String.length s in
|
|
|
|
|
assert (len <= 11);
|
|
|
|
|
let b = Bytes.make 12 '\x00' in
|
|
|
|
|
Bytes.blit_string s 0 b 0 len;
|
|
|
|
|
Bytes.to_string b
|
|
|
|
|
|
2025-11-28 14:00:11 +01:00
|
|
|
let bin =
|
|
|
|
|
let open Bin in
|
|
|
|
|
record (fun _value _fraction _currency ->
|
|
|
|
|
(* no need to decode amount? *)
|
|
|
|
|
assert false)
|
|
|
|
|
|+ field neint64 (fun t -> t.value)
|
|
|
|
|
|+ field neint32 (fun t -> t.fraction)
|
2025-11-28 19:17:25 +01:00
|
|
|
|+ field (bytes currency_len) (fun t -> pad_currency t.currency)
|
2025-11-28 14:00:11 +01:00
|
|
|
|> sealr
|
|
|
|
|
|
|
|
|
|
let bin_nbo =
|
|
|
|
|
let open Bin in
|
|
|
|
|
record (fun _value _fraction _currency -> assert false)
|
|
|
|
|
|+ field beint64 (fun t -> t.value)
|
|
|
|
|
|+ field beint32 (fun t -> t.fraction)
|
2025-11-28 19:17:25 +01:00
|
|
|
|+ field (bytes currency_len) (fun t -> pad_currency t.currency)
|
2025-11-28 14:00:11 +01:00
|
|
|
|> sealr
|
2026-02-03 15:56:05 +01:00
|
|
|
|
|
|
|
|
let dummy_value = "DUMMY:0.0" |> of_string |> Result.get_ok
|