dev: lib: Hledger.Data.Amount: drop amount, use nullamt always

This commit is contained in:
Simon Michael 2022-08-23 12:14:32 +01:00
parent c80c72d7cd
commit bfb632dd8e
3 changed files with 17 additions and 19 deletions

View File

@ -49,7 +49,6 @@ module Hledger.Data.Amount (
isNonsimpleCommodityChar, isNonsimpleCommodityChar,
quoteCommoditySymbolIfNeeded, quoteCommoditySymbolIfNeeded,
-- * Amount -- * Amount
amount,
nullamt, nullamt,
missingamt, missingamt,
num, num,
@ -255,24 +254,23 @@ instance Num Amount where
(-) = similarAmountsOp (-) (-) = similarAmountsOp (-)
(*) = similarAmountsOp (*) (*) = similarAmountsOp (*)
-- TODO: amount, num are clashy
-- | The empty simple amount. -- | The empty simple amount.
amount, nullamt :: Amount nullamt :: Amount
amount = Amount{acommodity="", aquantity=0, aprice=Nothing, astyle=amountstyle} nullamt = Amount{acommodity="", aquantity=0, aprice=Nothing, astyle=amountstyle}
nullamt = amount
-- | A temporary value for parsed transactions which had no amount specified. -- | A temporary value for parsed transactions which had no amount specified.
missingamt :: Amount missingamt :: Amount
missingamt = amount{acommodity="AUTO"} missingamt = nullamt{acommodity="AUTO"}
-- Handy amount constructors for tests. -- Handy amount constructors for tests.
-- usd/eur/gbp round their argument to a whole number of pennies/cents. -- usd/eur/gbp round their argument to a whole number of pennies/cents.
num n = amount{acommodity="", aquantity=n} -- XXX these are a bit clashy
hrs n = amount{acommodity="h", aquantity=n, astyle=amountstyle{asprecision=Precision 2, ascommodityside=R}} num n = nullamt{acommodity="", aquantity=n}
usd n = amount{acommodity="$", aquantity=roundTo 2 n, astyle=amountstyle{asprecision=Precision 2}} hrs n = nullamt{acommodity="h", aquantity=n, astyle=amountstyle{asprecision=Precision 2, ascommodityside=R}}
eur n = amount{acommodity="", aquantity=roundTo 2 n, astyle=amountstyle{asprecision=Precision 2}} usd n = nullamt{acommodity="$", aquantity=roundTo 2 n, astyle=amountstyle{asprecision=Precision 2}}
gbp n = amount{acommodity="£", aquantity=roundTo 2 n, astyle=amountstyle{asprecision=Precision 2}} eur n = nullamt{acommodity="", aquantity=roundTo 2 n, astyle=amountstyle{asprecision=Precision 2}}
per n = amount{acommodity="%", aquantity=n, astyle=amountstyle{asprecision=Precision 1, ascommodityside=R, ascommodityspaced=True}} gbp n = nullamt{acommodity="£", aquantity=roundTo 2 n, astyle=amountstyle{asprecision=Precision 2}}
per n = nullamt{acommodity="%", aquantity=n, astyle=amountstyle{asprecision=Precision 1, ascommodityside=R, ascommodityspaced=True}}
amt `at` priceamt = amt{aprice=Just $ UnitPrice priceamt} amt `at` priceamt = amt{aprice=Just $ UnitPrice priceamt}
amt @@ priceamt = amt{aprice=Just $ TotalPrice priceamt} amt @@ priceamt = amt{aprice=Just $ TotalPrice priceamt}
@ -287,7 +285,7 @@ similarAmountsOp :: (Quantity -> Quantity -> Quantity) -> Amount -> Amount -> Am
similarAmountsOp op Amount{acommodity=_, aquantity=q1, astyle=AmountStyle{asprecision=p1}} similarAmountsOp op Amount{acommodity=_, aquantity=q1, astyle=AmountStyle{asprecision=p1}}
Amount{acommodity=c2, aquantity=q2, astyle=s2@AmountStyle{asprecision=p2}} = Amount{acommodity=c2, aquantity=q2, astyle=s2@AmountStyle{asprecision=p2}} =
-- trace ("a1:"++showAmountDebug a1) $ trace ("a2:"++showAmountDebug a2) $ traceWith (("= :"++).showAmountDebug) -- trace ("a1:"++showAmountDebug a1) $ trace ("a2:"++showAmountDebug a2) $ traceWith (("= :"++).showAmountDebug)
amount{acommodity=c2, aquantity=q1 `op` q2, astyle=s2{asprecision=max p1 p2}} nullamt{acommodity=c2, aquantity=q1 `op` q2, astyle=s2{asprecision=max p1 p2}}
-- c1==c2 || q1==0 || q2==0 = -- c1==c2 || q1==0 || q2==0 =
-- otherwise = error "tried to do simple arithmetic with amounts in different commodities" -- otherwise = error "tried to do simple arithmetic with amounts in different commodities"
@ -982,7 +980,7 @@ tests_Amount = testGroup "Amount" [
amountCost (eur (-1)){aprice=Just $ TotalPrice $ usd (-2)} @?= usd (-2) amountCost (eur (-1)){aprice=Just $ TotalPrice $ usd (-2)} @?= usd (-2)
,testCase "amountLooksZero" $ do ,testCase "amountLooksZero" $ do
assertBool "" $ amountLooksZero amount assertBool "" $ amountLooksZero nullamt
assertBool "" $ amountLooksZero $ usd 0 assertBool "" $ amountLooksZero $ usd 0
,testCase "negating amounts" $ do ,testCase "negating amounts" $ do

View File

@ -193,7 +193,7 @@ amountValueAtDate priceoracle styles mto d a =
-- Make default display style use precision 2 instead of 0 ? -- Make default display style use precision 2 instead of 0 ?
-- Leave as is for now; mentioned in manual. -- Leave as is for now; mentioned in manual.
styleAmount styles styleAmount styles
amount{acommodity=comm, aquantity=rate * aquantity a} nullamt{acommodity=comm, aquantity=rate * aquantity a}
-- | Calculate the gain of each component amount, that is the difference -- | Calculate the gain of each component amount, that is the difference
-- between the valued amount and the value of the cost basis (see -- between the valued amount and the value of the cost basis (see

View File

@ -1480,24 +1480,24 @@ tests_Common = testGroup "Common" [
,testCase "unit price" $ assertParseEq amountp "$10 @ €0.5" ,testCase "unit price" $ assertParseEq amountp "$10 @ €0.5"
-- not precise enough: -- not precise enough:
-- (usd 10 `withPrecision` 0 `at` (eur 0.5 `withPrecision` 1)) -- `withStyle` asdecimalpoint=Just '.' -- (usd 10 `withPrecision` 0 `at` (eur 0.5 `withPrecision` 1)) -- `withStyle` asdecimalpoint=Just '.'
amount{ nullamt{
acommodity="$" acommodity="$"
,aquantity=10 -- need to test internal precision with roundTo ? I think not ,aquantity=10 -- need to test internal precision with roundTo ? I think not
,astyle=amountstyle{asprecision=Precision 0, asdecimalpoint=Nothing} ,astyle=amountstyle{asprecision=Precision 0, asdecimalpoint=Nothing}
,aprice=Just $ UnitPrice $ ,aprice=Just $ UnitPrice $
amount{ nullamt{
acommodity="" acommodity=""
,aquantity=0.5 ,aquantity=0.5
,astyle=amountstyle{asprecision=Precision 1, asdecimalpoint=Just '.'} ,astyle=amountstyle{asprecision=Precision 1, asdecimalpoint=Just '.'}
} }
} }
,testCase "total price" $ assertParseEq amountp "$10 @@ €5" ,testCase "total price" $ assertParseEq amountp "$10 @@ €5"
amount{ nullamt{
acommodity="$" acommodity="$"
,aquantity=10 ,aquantity=10
,astyle=amountstyle{asprecision=Precision 0, asdecimalpoint=Nothing} ,astyle=amountstyle{asprecision=Precision 0, asdecimalpoint=Nothing}
,aprice=Just $ TotalPrice $ ,aprice=Just $ TotalPrice $
amount{ nullamt{
acommodity="" acommodity=""
,aquantity=5 ,aquantity=5
,astyle=amountstyle{asprecision=Precision 0, asdecimalpoint=Nothing} ,astyle=amountstyle{asprecision=Precision 0, asdecimalpoint=Nothing}