From e6873458facadea0dfb228bb33291d6baf68c427 Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Tue, 26 Aug 2025 00:35:27 +0200 Subject: Basic import seems to be working --- lib/ledger.ml | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 9 deletions(-) (limited to 'lib/ledger.ml') diff --git a/lib/ledger.ml b/lib/ledger.ml index 3b52bcc..ba21e49 100644 --- a/lib/ledger.ml +++ b/lib/ledger.ml @@ -14,10 +14,11 @@ type tx_type = | Direct_debit_tx | Periodic_tx -type iban_tag = Account_tag | Counterparty_iban_tag [@@deriving compare] +type iban_tag = Account_tag | Counterparty_iban_tag +[@@deriving compare, sexp_of] type unit_tag = Filed_tag | Google_pay_tag | Auto_round_savings_tag -[@@deriving compare] +[@@deriving compare, sexp_of] type string_tag = | Desc_tag @@ -31,7 +32,7 @@ type string_tag = | Terminal_tag | Card_seq_no_tag | Savings_account_tag -[@@deriving compare] +[@@deriving compare, sexp_of] module Label = struct type 'a t = @@ -58,7 +59,39 @@ module Label = struct | Unit_label _, _ -> Gt end -module Labels = Dmap.Make (Label) +module Labels = struct + include Dmap.Make (Label) + + let sexp_of_t m = + Sexp.List + (bindings m + |> List.map ~f:(function + | Binding (Iban_label tag, iban) -> + Sexp.List + [ + Sexp.Atom "Iban_label"; + [%sexp_of: iban_tag] tag; + [%sexp_of: Iban.t] iban; + ] + | Binding (String_label tag, s) -> + Sexp.List + [ + Sexp.Atom "String_label"; + [%sexp_of: string_tag] tag; + Sexp.Atom s; + ] + | Binding (Timestamp_label, ts) -> + Sexp.List + [ Sexp.Atom "Timestamp_label"; [%sexp_of: Time_ns_unix.t] ts ] + | Binding (Unit_label tag, ()) -> + Sexp.List [ Sexp.Atom "Unit_label"; [%sexp_of: unit_tag] tag ])) +end + +module Z = struct + include Z + + let sexp_of_t x = Sexp.Atom (Z.to_string x) +end module Money : sig type t @@ -69,8 +102,9 @@ module Money : sig val to_z : t -> Z.t val ( + ) : t -> t -> t val ( - ) : t -> t -> t + val sexp_of_t : t -> Sexp.t end = struct - type t = Z.t + type t = Z.t [@@deriving sexp_of] let equal = Z.equal let compare = Z.compare @@ -80,8 +114,11 @@ end = struct let ( - ) x y = Z.(x - y) end -type scalar = Amount of Money.t | Rate of Z.t [@@deriving equal, compare] -type commodity_id = string (* TODO: consider making this UUID *) +type scalar = Amount of Money.t | Rate of Z.t +[@@deriving equal, compare, sexp_of] + +type commodity_id = string +(* TODO: consider making this UUID *) [@@deriving sexp] module Account_id = struct type t = string list [@@deriving sexp, compare] @@ -93,12 +130,14 @@ type account = { commodity_id : commodity_id; balance : Money.t; } +[@@deriving sexp_of] type bal_assert = { account : Account_id.t; amount : Money.t; labels : Labels.t; } +[@@deriving sexp_of] module Account_id_map = Map.Make (Account_id) @@ -119,6 +158,7 @@ module Tx : sig val debit : t -> scalar Account_id_map.t val credit : t -> scalar Account_id_map.t val labels : t -> Labels.t + val sexp_of_t : t -> Sexp.t end = struct (* We hide this because we only want to allow constructing balanced transactions *) type t = { @@ -128,7 +168,7 @@ end = struct credit : scalar Account_id_map.t; labels : Labels.t; } - [@@deriving fields] + [@@deriving fields, sexp_of] type error = Unbalanced @@ -141,4 +181,6 @@ end = struct end type item = Tx_item of Tx.t | Bal_assert_item of bal_assert -type ledger = Ledger of item list +[@@deriving sexp_of] + +type t = item list [@@deriving sexp_of] -- cgit v1.2.3