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,
quoteCommoditySymbolIfNeeded,
-- * Amount
amount,
nullamt,
missingamt,
num,
@ -255,24 +254,23 @@ instance Num Amount where
(-) = similarAmountsOp (-)
(*) = similarAmountsOp (*)
-- TODO: amount, num are clashy
-- | The empty simple amount.
amount, nullamt :: Amount
amount = Amount{acommodity="", aquantity=0, aprice=Nothing, astyle=amountstyle}
nullamt = amount
nullamt :: Amount
nullamt = Amount{acommodity="", aquantity=0, aprice=Nothing, astyle=amountstyle}
-- | A temporary value for parsed transactions which had no amount specified.
missingamt :: Amount
missingamt = amount{acommodity="AUTO"}
missingamt = nullamt{acommodity="AUTO"}
-- Handy amount constructors for tests.
-- usd/eur/gbp round their argument to a whole number of pennies/cents.
num n = amount{acommodity="", aquantity=n}
hrs n = amount{acommodity="h", aquantity=n, astyle=amountstyle{asprecision=Precision 2, ascommodityside=R}}
usd n = amount{acommodity="$", aquantity=roundTo 2 n, astyle=amountstyle{asprecision=Precision 2}}
eur n = amount{acommodity="", aquantity=roundTo 2 n, astyle=amountstyle{asprecision=Precision 2}}
gbp n = amount{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}}
-- XXX these are a bit clashy
num n = nullamt{acommodity="", aquantity=n}
hrs n = nullamt{acommodity="h", aquantity=n, astyle=amountstyle{asprecision=Precision 2, ascommodityside=R}}
usd n = nullamt{acommodity="$", aquantity=roundTo 2 n, astyle=amountstyle{asprecision=Precision 2}}
eur n = nullamt{acommodity="", aquantity=roundTo 2 n, astyle=amountstyle{asprecision=Precision 2}}
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 @@ 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}}
Amount{acommodity=c2, aquantity=q2, astyle=s2@AmountStyle{asprecision=p2}} =
-- 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 =
-- 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)
,testCase "amountLooksZero" $ do
assertBool "" $ amountLooksZero amount
assertBool "" $ amountLooksZero nullamt
assertBool "" $ amountLooksZero $ usd 0
,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 ?
-- Leave as is for now; mentioned in manual.
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
-- 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"
-- not precise enough:
-- (usd 10 `withPrecision` 0 `at` (eur 0.5 `withPrecision` 1)) -- `withStyle` asdecimalpoint=Just '.'
amount{
nullamt{
acommodity="$"
,aquantity=10 -- need to test internal precision with roundTo ? I think not
,astyle=amountstyle{asprecision=Precision 0, asdecimalpoint=Nothing}
,aprice=Just $ UnitPrice $
amount{
nullamt{
acommodity=""
,aquantity=0.5
,astyle=amountstyle{asprecision=Precision 1, asdecimalpoint=Just '.'}
}
}
,testCase "total price" $ assertParseEq amountp "$10 @@ €5"
amount{
nullamt{
acommodity="$"
,aquantity=10
,astyle=amountstyle{asprecision=Precision 0, asdecimalpoint=Nothing}
,aprice=Just $ TotalPrice $
amount{
nullamt{
acommodity=""
,aquantity=5
,astyle=amountstyle{asprecision=Precision 0, asdecimalpoint=Nothing}