lib: refactor, extract parseBalanceAssertionType

This commit is contained in:
Simon Michael 2024-02-20 20:54:13 -10:00
parent 9f53e36904
commit 60a1adc5ba

View File

@ -20,6 +20,7 @@ Most of the code for reading rules files and csv files is in this module.
{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-} {-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-} {-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# LANGUAGE LambdaCase #-}
--- ** exports --- ** exports
module Hledger.Read.RulesReader ( module Hledger.Read.RulesReader (
@ -33,6 +34,7 @@ module Hledger.Read.RulesReader (
-- CsvRules, -- CsvRules,
dataFileFor, dataFileFor,
rulesFileFor, rulesFileFor,
parseBalanceAssertionType,
-- * Tests -- * Tests
tests_RulesReader, tests_RulesReader,
) )
@ -2361,16 +2363,26 @@ mkBalanceAssertion rules record (amt, pos) = assrt{baamount=amt, baposition=pos}
where where
assrt = assrt =
case getDirective "balance-type" rules of case getDirective "balance-type" rules of
Nothing -> nullassertion Nothing -> nullassertion
Just "=" -> nullassertion Just x ->
Just "==" -> nullassertion{batotal=True} case parseBalanceAssertionType $ T.unpack x of
Just "=*" -> nullassertion{bainclusive=True} Just (total, inclusive) -> nullassertion{batotal=total, bainclusive=inclusive}
Just "==*" -> nullassertion{batotal=True, bainclusive=True} Nothing -> error' . T.unpack $ T.unlines -- PARTIAL:
Just x -> error' . T.unpack $ T.unlines -- PARTIAL: [ "balance-type \"" <> x <>"\" is invalid. Use =, ==, =* or ==*."
[ "balance-type \"" <> x <>"\" is invalid. Use =, ==, =* or ==*." , showRecord record
, showRecord record , showRules rules record
, showRules rules record ]
]
-- | Detect from a balance assertion's syntax (=, ==, =*, ==*)
-- whether it is (a) total (multi-commodity) and (b) subaccount-inclusive.
-- Returns nothing if invalid syntax was provided.
parseBalanceAssertionType :: String -> Maybe (Bool, Bool)
parseBalanceAssertionType = \case
"=" -> Just (False, False)
"==" -> Just (True, False)
"=*" -> Just (False, True )
"==*" -> Just (True, True )
_ -> Nothing
-- | Figure out the account name specified for posting N, if any. -- | Figure out the account name specified for posting N, if any.
-- And whether it is the default unknown account (which may be -- And whether it is the default unknown account (which may be