This commit is contained in:
parent
2433ff3932
commit
cf31fd7441
3 changed files with 18 additions and 12 deletions
|
|
@ -1,7 +1,8 @@
|
||||||
(* TODO
|
(* TODO
|
||||||
have safe amount arithmetic
|
have safe amount arithmetic
|
||||||
make type private
|
make type private
|
||||||
use config currency option *)
|
use config currency option:
|
||||||
|
-> functorize or dune virtual-module *)
|
||||||
(* Amounts of currency, serialized as `<Currency>:<IntegerPart>.<FractionalPart>`
|
(* Amounts of currency, serialized as `<Currency>:<IntegerPart>.<FractionalPart>`
|
||||||
Fixed-precision numbers with 8 decimal places.
|
Fixed-precision numbers with 8 decimal places.
|
||||||
- <Currency> must be at most 11 characters long
|
- <Currency> must be at most 11 characters long
|
||||||
|
|
@ -15,17 +16,17 @@ type t = {
|
||||||
sign: [ `Plus | `Minus ] option;
|
sign: [ `Plus | `Minus ] option;
|
||||||
currency: [ `Eur ];
|
currency: [ `Eur ];
|
||||||
value: Int64.t;
|
value: Int64.t;
|
||||||
fraction: Int64.t;
|
fraction: Int32.t;
|
||||||
}
|
}
|
||||||
|
|
||||||
let ( let* ) o f = match o with Ok v -> f v | Error _ as e -> e
|
let ( let* ) o f = match o with Ok v -> f v | Error _ as e -> e
|
||||||
let check_not msg = function false -> Ok () | true -> Error msg
|
let check_not msg = function false -> Ok () | true -> Error msg
|
||||||
let value_upper_bound = Z.(pow (of_int 2) 52) |> Z.to_int64
|
let value_upper_bound = Z.(pow (of_int 2) 52) |> Z.to_int64
|
||||||
let fraction_upper_bound = Int64.of_int 100_000_000
|
let fraction_upper_bound = Int32.of_int 100_000_000
|
||||||
|
|
||||||
let make ~sign ~currency ~value ~fraction =
|
let make ~sign ~currency ~value ~fraction =
|
||||||
let* () = check_not "value is negative" (value < Int64.zero) in
|
let* () = check_not "value is negative" (value < Int64.zero) in
|
||||||
let* () = check_not "fraction is negative" (fraction < Int64.zero) in
|
let* () = check_not "fraction is negative" (fraction < Int32.zero) in
|
||||||
let* () =
|
let* () =
|
||||||
check_not "value is greater than 2^52" (value > value_upper_bound)
|
check_not "value is greater than 2^52" (value > value_upper_bound)
|
||||||
in
|
in
|
||||||
|
|
@ -44,7 +45,7 @@ let to_string =
|
||||||
in
|
in
|
||||||
let pp_currency ppf = function `Eur -> string ppf "EUR" in
|
let pp_currency ppf = function `Eur -> string ppf "EUR" in
|
||||||
fun ppf { sign; currency; value; fraction } ->
|
fun ppf { sign; currency; value; fraction } ->
|
||||||
pf ppf "%a%a:%Ld.%02Ld" (Fmt.option pp_sign) sign pp_currency currency
|
pf ppf "%a%a:%Ld.%02ld" (Fmt.option pp_sign) sign pp_currency currency
|
||||||
value fraction
|
value fraction
|
||||||
in
|
in
|
||||||
Fmt.str "%a" pp
|
Fmt.str "%a" pp
|
||||||
|
|
@ -59,19 +60,26 @@ let of_string =
|
||||||
]
|
]
|
||||||
in
|
in
|
||||||
let parse_currency = string "EUR" *> return `Eur in
|
let parse_currency = string "EUR" *> return `Eur in
|
||||||
let parse_int =
|
let parse_int64 =
|
||||||
take_while1 (function '0' .. '9' -> true | _ -> false)
|
take_while1 (function '0' .. '9' -> true | _ -> false)
|
||||||
>>| Int64.of_string_opt
|
>>| Int64.of_string_opt
|
||||||
>>= function
|
>>= function
|
||||||
| None -> fail "invalid integer"
|
| None -> fail "invalid integer"
|
||||||
| Some n -> return n
|
| Some n -> return n
|
||||||
in
|
in
|
||||||
|
let parse_int32 =
|
||||||
|
take_while1 (function '0' .. '9' -> true | _ -> false)
|
||||||
|
>>| Int32.of_string_opt
|
||||||
|
>>= function
|
||||||
|
| None -> fail "invalid integer"
|
||||||
|
| Some n -> return n
|
||||||
|
in
|
||||||
let parse_t =
|
let parse_t =
|
||||||
lift4
|
lift4
|
||||||
(fun sign currency value fraction ->
|
(fun sign currency value fraction ->
|
||||||
make ~sign ~currency ~value ~fraction)
|
make ~sign ~currency ~value ~fraction)
|
||||||
parse_sign parse_currency
|
parse_sign parse_currency
|
||||||
(char ':' *> parse_int)
|
(char ':' *> parse_int64)
|
||||||
(char '.' *> parse_int)
|
(char '.' *> parse_int32)
|
||||||
in
|
in
|
||||||
fun s -> parse_string ~consume:Consume.All parse_t s |> Result.join
|
fun s -> parse_string ~consume:Consume.All parse_t s |> Result.join
|
||||||
|
|
|
||||||
|
|
@ -14,11 +14,9 @@ end
|
||||||
|
|
||||||
let pg_amount : Amount.t Caqti_type.t =
|
let pg_amount : Amount.t Caqti_type.t =
|
||||||
let open Amount in
|
let open Amount in
|
||||||
(* TODO bad int32 conversion *)
|
|
||||||
Caqti_type.custom
|
Caqti_type.custom
|
||||||
~encode:(fun amount -> Ok (amount.value, Int64.to_int32 amount.fraction))
|
~encode:(fun amount -> Ok (amount.value, amount.fraction))
|
||||||
~decode:(fun (value, fraction) ->
|
~decode:(fun (value, fraction) ->
|
||||||
let fraction = Int64.of_int32 fraction in
|
|
||||||
Ok { sign= None; currency= `Eur; value; fraction })
|
Ok { sign= None; currency= `Eur; value; fraction })
|
||||||
Caqti_type.(t2 int64 int32)
|
Caqti_type.(t2 int64 int32)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ let () =
|
||||||
sign= Some `Plus;
|
sign= Some `Plus;
|
||||||
currency= `Eur;
|
currency= `Eur;
|
||||||
value= Int64.of_int 25;
|
value= Int64.of_int 25;
|
||||||
fraction= Int64.of_int 678;
|
fraction= Int32.of_int 678;
|
||||||
}
|
}
|
||||||
in
|
in
|
||||||
let s = to_string v in
|
let s = to_string v in
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue