imp: lib: Hledger.Read.Common: rename/add amount parsing helpers

removed:
 amountp'
 mamountp'

added:
 parseamount
 parseamount'
 parsemixedamount
 parsemixedamount'
This commit is contained in:
Simon Michael 2022-10-01 18:30:51 -10:00
parent 82a503cbf5
commit de5a97600c

View File

@ -76,8 +76,6 @@ module Hledger.Read.Common (
-- ** amounts -- ** amounts
spaceandamountormissingp, spaceandamountormissingp,
amountp, amountp,
amountp',
mamountp',
amountpwithmultiplier, amountpwithmultiplier,
commoditysymbolp, commoditysymbolp,
priceamountp, priceamountp,
@ -86,6 +84,10 @@ module Hledger.Read.Common (
numberp, numberp,
fromRawNumber, fromRawNumber,
rawnumberp, rawnumberp,
parseamount,
parseamount',
parsemixedamount,
parsemixedamount',
-- ** comments -- ** comments
isLineCommentStart, isLineCommentStart,
@ -244,7 +246,7 @@ commodityStyleFromRawOpts rawOpts =
foldM (\r -> fmap (\(c,a) -> M.insert c a r) . parseCommodity) mempty optList foldM (\r -> fmap (\(c,a) -> M.insert c a r) . parseCommodity) mempty optList
where where
optList = listofstringopt "commodity-style" rawOpts optList = listofstringopt "commodity-style" rawOpts
parseCommodity optStr = case amountp'' optStr of parseCommodity optStr = case parseamount optStr of
Left _ -> Left optStr Left _ -> Left optStr
Right (Amount acommodity _ astyle _) -> Right (acommodity, astyle) Right (Amount acommodity _ astyle _) -> Right (acommodity, astyle)
@ -799,20 +801,24 @@ amountwithoutpricep mult = do
uncurry parseErrorAtRegion posRegion errMsg uncurry parseErrorAtRegion posRegion errMsg
Right (q,p,d,g) -> pure (q, Precision p, d, g) Right (q,p,d,g) -> pure (q, Precision p, d, g)
-- | Try to parse an amount from a string -- | Try to parse a single-commodity amount from a string
amountp'' :: String -> Either HledgerParseErrors Amount parseamount :: String -> Either HledgerParseErrors Amount
amountp'' s = runParser (evalStateT (amountp <* eof) nulljournal) "" (T.pack s) parseamount s = runParser (evalStateT (amountp <* eof) nulljournal) "" (T.pack s)
-- | Parse an amount from a string, or get an error. -- | Parse a single-commodity amount from a string, or get an error.
amountp' :: String -> Amount parseamount' :: String -> Amount
amountp' s = parseamount' s =
case amountp'' s of case parseamount s of
Right amt -> amt Right amt -> amt
Left err -> error' $ show err -- PARTIAL: XXX should throwError Left err -> error' $ show err -- PARTIAL: XXX should throwError
-- | Parse a mixed amount from a string, or get an error. -- | Like parseamount', but returns a MixedAmount.
mamountp' :: String -> MixedAmount parsemixedamount :: String -> Either HledgerParseErrors MixedAmount
mamountp' = mixedAmount . amountp' parsemixedamount = fmap mixedAmount . parseamount
-- | Like parseamount', but returns a MixedAmount.
parsemixedamount' :: String -> MixedAmount
parsemixedamount' = mixedAmount . parseamount'
-- | Parse a minus or plus sign followed by zero or more spaces, -- | Parse a minus or plus sign followed by zero or more spaces,
-- or nothing, returning a function that negates or does nothing. -- or nothing, returning a function that negates or does nothing.