add result.ml; polymorphic variant errors + refacto
This commit is contained in:
parent
9fd3b5a3cc
commit
42b0ec1445
36 changed files with 949 additions and 954 deletions
|
|
@ -40,7 +40,7 @@ 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
|
||||
|
||||
let make ~sign ~currency ~value ~fraction =
|
||||
let make sign currency value fraction =
|
||||
let* () = check_currency currency in
|
||||
let* () = check_value value in
|
||||
let* () = check_fraction fraction in
|
||||
|
|
@ -93,10 +93,7 @@ module Parser = struct
|
|||
return n)
|
||||
|
||||
let amount =
|
||||
lift4
|
||||
(fun sign currency value fraction ->
|
||||
make ~sign ~currency ~value ~fraction)
|
||||
sign letters
|
||||
lift4 make sign letters
|
||||
(char ':' *> value)
|
||||
(option 0_l (char '.' *> fraction))
|
||||
<* end_of_input
|
||||
|
|
@ -105,8 +102,6 @@ module Parser = struct
|
|||
Angstrom.parse_string ~consume:Consume.All amount s |> Result.join
|
||||
end
|
||||
|
||||
let of_string = Parser.parse
|
||||
|
||||
let pp =
|
||||
let open Fmt in
|
||||
let pp_sign =
|
||||
|
|
@ -133,31 +128,35 @@ let pp =
|
|||
pf ppf "%a%s:%Lu%a" pp_sign sign currency value pp_fraction fraction
|
||||
|
||||
let to_string = Fmt.str "%a" pp
|
||||
let of_string = Parser.parse
|
||||
|
||||
(* - *)
|
||||
|
||||
let jsont = Jsont.of_of_string ~kind:"Amount" of_string ~enc:to_string
|
||||
|
||||
let pad_currency_string s =
|
||||
let len = String.length s in
|
||||
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 *)
|
||||
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
|
||||
(* note: binary decoding never used, like for all signatures types *)
|
||||
let decode_exn value fraction currency =
|
||||
match make None currency value fraction with
|
||||
| Error e -> Fmt.failwith "amount decode %s." e
|
||||
| Ok t -> t
|
||||
|
||||
let bin =
|
||||
let open Bin in
|
||||
record make_exn
|
||||
record decode_exn
|
||||
|+ field beint64 (fun t -> t.value)
|
||||
|+ field beint32 (fun t -> t.fraction)
|
||||
|+ field (bytes currency_length_limit) (fun t ->
|
||||
pad_currency_string t.currency)
|
||||
let len = String.length t.currency in
|
||||
if not (len < currency_length_limit) then
|
||||
Fmt.failwith "broken amount currency";
|
||||
let b = Bytes.make currency_length_limit '\x00' in
|
||||
Bytes.blit_string t.currency 0 b 0 len;
|
||||
Bytes.unsafe_to_string b)
|
||||
|> sealr
|
||||
|
||||
let caqti ~currency =
|
||||
let open Caqti_type in
|
||||
custom
|
||||
~encode:(fun amount -> Ok (amount.value, amount.fraction))
|
||||
~decode:(fun (value, fraction) -> make None currency value fraction)
|
||||
(t2 int64 int32)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue