imp:csv: ignore whitespace, show error position when reading .latest files

This commit is contained in:
Simon Michael 2024-12-05 16:35:18 -10:00
parent d3ce054789
commit 1048791810

View File

@ -125,7 +125,7 @@ module Hledger.Read (
--- ** imports --- ** imports
import qualified Control.Exception as C import qualified Control.Exception as C
import Control.Monad (unless, when) import Control.Monad (unless, when, forM)
import "mtl" Control.Monad.Except (ExceptT(..), runExceptT, liftEither) import "mtl" Control.Monad.Except (ExceptT(..), runExceptT, liftEither)
import Control.Monad.IO.Class (MonadIO, liftIO) import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Default (def) import Data.Default (def)
@ -159,6 +159,7 @@ import Hledger.Read.RulesReader (tests_RulesReader)
import Hledger.Utils import Hledger.Utils
import Prelude hiding (getContents, writeFile) import Prelude hiding (getContents, writeFile)
import Hledger.Data.JournalChecks (journalStrictChecks) import Hledger.Data.JournalChecks (journalStrictChecks)
import Text.Printf (printf)
--- ** doctest setup --- ** doctest setup
-- $setup -- $setup
@ -401,12 +402,15 @@ saveLatestDatesForFiles = mapM_ (\(LatestDatesForFile f ds) -> saveLatestDates d
previousLatestDates :: FilePath -> IO LatestDates previousLatestDates :: FilePath -> IO LatestDates
previousLatestDates f = do previousLatestDates f = do
let latestfile = latestDatesFileFor f let latestfile = latestDatesFileFor f
parsedate s = maybe (fail $ "could not parse date \"" ++ s ++ "\"") return $
parsedateM s
exists <- doesFileExist latestfile exists <- doesFileExist latestfile
if exists t <- if exists then readFileStrictly latestfile else return T.empty
then traverse (parsedate . T.unpack . T.strip) . T.lines =<< readFileStrictly latestfile let nls = zip [1::Int ..] $ T.lines t
else return [] fmap catMaybes $ forM nls $ \(n,l) -> do
let s = T.unpack $ T.strip l
case (s, parsedateM s) of
("", _) -> return Nothing
(_, Nothing) -> error' (printf "%s:%d: invalid date: \"%s\"" latestfile n s)
(_, Just d) -> return $ Just d
-- | Where to save latest transaction dates for the given file path. -- | Where to save latest transaction dates for the given file path.
-- (.latest.FILE) -- (.latest.FILE)