From 357b27fb6370a55bb60ca9531035c6e69e22a938 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 4 Feb 2010 20:55:12 +0000 Subject: [PATCH] fix an unsafe ! in ledgerAccount; clarify that withLedgerDo does not cache the ledger --- Ledger/Ledger.hs | 10 ++++++---- Utils.hs | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Ledger/Ledger.hs b/Ledger/Ledger.hs index 1a7ef925a..942cbe5f3 100644 --- a/Ledger/Ledger.hs +++ b/Ledger/Ledger.hs @@ -54,10 +54,10 @@ aliases for easier interaction. Here's an example: module Ledger.Ledger where import qualified Data.Map as Map -import Data.Map ((!), fromList) +import Data.Map (findWithDefault, fromList) import Ledger.Utils import Ledger.Types -import Ledger.Account () +import Ledger.Account (nullacct) import Ledger.AccountName import Ledger.Journal import Ledger.Posting @@ -100,9 +100,11 @@ type CachedLedger = Ledger ledgerAccountNames :: Ledger -> [AccountName] ledgerAccountNames = drop 1 . flatten . accountnametree --- | Get the named account from a ledger. +-- | Get the named account from a (cached) ledger. +-- If the ledger has not been cached (with crunchJournal or +-- cacheLedger'), this returns the null account. ledgerAccount :: Ledger -> AccountName -> Account -ledgerAccount = (!) . accountmap +ledgerAccount l a = findWithDefault nullacct a $ accountmap l -- | List a ledger's accounts, in tree order ledgerAccounts :: Ledger -> [Account] diff --git a/Utils.hs b/Utils.hs index f0cc60626..505092989 100644 --- a/Utils.hs +++ b/Utils.hs @@ -21,6 +21,8 @@ import System.Time (ClockTime,getClockTime) -- | Parse the user's specified ledger file and run a hledger command on -- it, or report a parse error. This function makes the whole thing go. +-- Warning, this provides only an uncached Ledger (no accountnametree or +-- accountmap), so cmd must cacheLedger'/crunchJournal if needed. withLedgerDo :: [Opt] -> [String] -> String -> ([Opt] -> [String] -> Ledger -> IO ()) -> IO () withLedgerDo opts args cmdname cmd = do -- We kludgily read the file before parsing to grab the full text, unless