diff --git a/src/amount.ml b/src/amount.ml index 79639201..1fb66513 100644 --- a/src/amount.ml +++ b/src/amount.ml @@ -19,22 +19,19 @@ type t = { } 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 value_upper_bound = Z.(pow (of_int 2) 52) |> Z.to_int64 +let fraction_upper_bound = Int64.of_int 100_000_000 let make ~sign ~currency ~value ~fraction = - let value_upper_bound = Z.(pow (of_int 2) 52) |> Z.to_int64 in - let fraction_upper_bound = Int64.of_int 100_000_000 in - let* () = if value < Int64.zero then Error "value is negative" else Ok () in + let* () = check_not "value is negative" (value < Int64.zero) in + let* () = check_not "fraction is negative" (fraction < Int64.zero) in let* () = - if fraction < Int64.zero then Error "fraction is negative" else Ok () + check_not "value is greater than 2^52" (value > value_upper_bound) in let* () = - if value > value_upper_bound then Error "value is greater than 2^52" - else Ok () - in - let* () = - if fraction >= fraction_upper_bound then - Error "fraction have more than 8 decimal digits" - else Ok () + check_not "fraction has more than 8 decimal digits" + (fraction >= fraction_upper_bound) in Ok { sign; currency; value; fraction }