From 3f5221c2da2a19cf5de05284821e9b854d31b7fb Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Mon, 25 Aug 2025 21:48:54 +0200 Subject: Clean up CSV parsing code a bit --- lib/ledger.ml | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) (limited to 'lib/ledger.ml') diff --git a/lib/ledger.ml b/lib/ledger.ml index fd1b2a9..1d9a63c 100644 --- a/lib/ledger.ml +++ b/lib/ledger.ml @@ -98,13 +98,43 @@ module Account_id_key = struct type comparator_witness end -type tx = { - cleared : Date.t option; - commodity_id : commodity_id; - debit : scalar Map.M(Account_id_key).t; - credit : scalar Map.M(Account_id_key).t; - labels : Labels.t; -} +module Tx : sig + type t + type error = Unbalanced + + val make : + cleared:Date.t option -> + commodity_id:commodity_id -> + debit:scalar Map.M(Account_id_key).t -> + credit:scalar Map.M(Account_id_key).t -> + labels:Labels.t -> + (t, error) result + + val cleared : t -> Date.t option + val commodity_id : t -> commodity_id + val debit : t -> scalar Map.M(Account_id_key).t + val credit : t -> scalar Map.M(Account_id_key).t + val labels : t -> Labels.t +end = struct + (* We hide this because we only want to allow constructing balanced transactions *) + type t = { + cleared : Date.t option; + commodity_id : commodity_id; + debit : scalar Map.M(Account_id_key).t; + credit : scalar Map.M(Account_id_key).t; + labels : Labels.t; + } + [@@deriving fields] + + type error = Unbalanced + + (* TODO: check if debits and credits are balanced *) + let is_balanced _debits _credits = true + + let make ~cleared ~commodity_id ~debit ~credit ~labels = + if not (is_balanced debit credit) then Error Unbalanced + else Ok { cleared; commodity_id; debit; credit; labels } +end -type item = Tx_item of tx | Bal_assert_item of bal_assert +type item = Tx_item of Tx.t | Bal_assert_item of bal_assert type ledger = Ledger of item list -- cgit v1.2.3