mte/src/amount.ml

156 lines
4.2 KiB
OCaml
Raw Normal View History

(* TODO implement operations on amounts *)
2026-03-01 00:35:59 +01:00
open Syntax
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-03-01 00:35:59 +01:00
let currency_length_limit = 12
let value_limit = Int64.add 1_L (Int64.of_float (Float.pow 2. 52.))
let fraction_nb_digits = 8
2026-02-11 00:06:28 +01:00
2026-03-01 00:35:59 +01:00
let fraction_limit =
Int32.of_float (Float.pow 10. (Int.to_float fraction_nb_digits))
let is_letter = function 'a' .. 'z' | 'A' .. 'Z' -> true | _ -> false
let is_digit = function '0' .. '9' -> true | _ -> false
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 < currency_length_limit then Ok ()
else
Fmt.error "currency is more than %d characters long" currency_length_limit
let check_value v =
if Int64.unsigned_compare v value_limit < 0 then Ok ()
else Error "value is greater than 2^52"
let check_fraction v =
if Int32.unsigned_compare v fraction_limit < 0 then Ok ()
else Fmt.error "fraction has more than %d digits" fraction_nb_digits
2025-10-13 04:15:58 +02:00
2025-10-13 00:02:35 +02:00
let make ~sign ~currency ~value ~fraction =
2026-03-01 00:35:59 +01:00
let* () = check_currency currency in
let* () = check_value value in
let* () = check_fraction fraction in
let currency = String.uppercase_ascii currency in
Ok { sign; currency; value; fraction }
module Parser = struct
2026-02-11 00:06:28 +01:00
open Angstrom
2025-11-20 18:38:01 +01:00
2026-03-01 00:35:59 +01:00
let letters = take_while1 is_letter
let digits = take_while1 is_digit
2026-02-11 00:06:28 +01:00
let sign =
2026-03-01 00:35:59 +01:00
choice
[
char '+' *> return (Some Sign_plus);
char '-' *> return (Some Sign_minus);
return None;
]
let value =
digits >>= fun s ->
match Int64.of_string_opt s with
| None -> fail "not a valid int64"
2025-10-13 00:02:35 +02:00
| Some n -> return n
2026-02-11 00:06:28 +01:00
2026-03-01 00:35:59 +01:00
let fraction =
digits >>= fun s ->
let len = String.length s in
match len <= fraction_nb_digits with
| false ->
fail (Fmt.str "fraction has more than %d digits" fraction_nb_digits)
| true -> (
match Int32.of_string_opt s with
| None -> fail "not a valid int32"
| Some n ->
let magnitude =
let exp = Int.to_float (fraction_nb_digits - len) in
Int32.of_float @@ Float.pow 10. exp
in
let n = Int32.mul n magnitude in
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-03-01 00:35:59 +01:00
sign letters
(char ':' *> value)
(option 0_l (char '.' *> fraction))
2026-02-11 00:06:28 +01:00
<* end_of_input
2026-03-01 00:35:59 +01:00
let parse s =
Angstrom.parse_string ~consume:Consume.All amount s |> Result.join
2026-02-11 00:06:28 +01:00
end
2026-03-01 00:35:59 +01:00
let of_string = Parser.parse
2026-02-11 00:06:28 +01:00
let pp =
let open Fmt in
2026-03-01 00:35:59 +01:00
let pp_sign =
let pp ppf = function
| Sign_plus -> char ppf '+'
| Sign_minus -> char ppf '-'
in
Fmt.option pp
in
let rec rm_trailing_zeros count x =
if x <> 0_l && Int32.rem x 10_l = 0_l then
rm_trailing_zeros (succ count) (Int32.div x 10_l)
else (count, x)
in
let pp_fraction ppf fraction =
match fraction = 0_l with
| true -> Fmt.nop ppf ()
| false ->
let count, x = rm_trailing_zeros 0 fraction in
let nb_leading_zeros = fraction_nb_digits - count in
pf ppf ".%0*lu" nb_leading_zeros x
2025-10-13 00:02:35 +02:00
in
2026-02-11 00:06:28 +01:00
fun ppf { sign; currency; value; fraction } ->
2026-03-01 00:35:59 +01:00
pf ppf "%a%s:%Lu%a" pp_sign sign currency value pp_fraction fraction
2026-02-11 00:06:28 +01:00
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
let pad_currency_string s =
2025-11-28 19:17:25 +01:00
let len = String.length s in
2026-03-01 00:35:59 +01:00
match len < currency_length_limit with
| false -> Fmt.failwith "amount with invalid currency"
| true ->
let b = Bytes.make currency_length_limit '\x00' in
Bytes.blit_string s 0 b 0 len;
Bytes.unsafe_to_string b
(* binary decoding never unused *)
2026-02-11 00:06:28 +01:00
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:06:28 +01:00
record make_exn
2025-11-28 14:00:11 +01:00
|+ field beint64 (fun t -> t.value)
2026-03-01 00:35:59 +01:00
|+ field beint32 (fun t -> t.fraction)
|+ field (bytes currency_length_limit) (fun t ->
pad_currency_string t.currency)
2025-11-28 14:00:11 +01:00
|> sealr