Skip to content

Commit d88cf68

Browse files
committed
dev: lib: drop dead cost-basis-inference helpers
journalInferPostingsCostBasis, postingInferCostBasis, postingUsesLots, and divideAmountAndCapPrecision were exported (in the case of the first and last) but had no callers in the pipeline. The two real callers of divideAmountAndCapPrecision were removed by the recent basis-check precision work; the journal-side cost-basis inference helpers were never wired in. Removing them makes defaultMaxDisplayPrecision genuinely display-only in all reachable code, matching its name.
1 parent 2bf3b95 commit d88cf68

2 files changed

Lines changed: 1 addition & 44 deletions

File tree

hledger-lib/Hledger/Data/Amount.hs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ module Hledger.Data.Amount (
6868
amountLooksZero,
6969
amountSetQuantity,
7070
divideAmount,
71-
divideAmountAndCapPrecision,
7271
multiplyAmount,
7372
invertAmount,
7473
-- ** styles
@@ -391,17 +390,6 @@ transformAmount f a@Amount{aquantity=q,acost=p} = a{aquantity=f q, acost=f' <$>
391390
divideAmount :: Quantity -> Amount -> Amount
392391
divideAmount n = transformAmount (/n)
393392

394-
-- | Divide an amount's quantity by some number, then round both internal and
395-
-- display precision to the quotient's natural precision capped at
396-
-- 'defaultMaxDisplayPrecision'. This avoids non-terminating divisions (eg 53/7)
397-
-- leaving 255 decimal digits of slop in the internal quantity, while still showing
398-
-- every decimal digit a clean division produces.
399-
divideAmountAndCapPrecision :: Quantity -> Amount -> Amount
400-
divideAmountAndCapPrecision n a =
401-
let a' = a{aquantity = aquantity a / n}
402-
p = min defaultMaxDisplayPrecision (amountInternalPrecision a')
403-
in setAmountInternalPrecision p a'
404-
405393
-- | Multiply an amount's quantity (and its total cost, if it has one) by a constant.
406394
multiplyAmount :: Quantity -> Amount -> Amount
407395
multiplyAmount n = transformAmount (*n)

hledger-lib/Hledger/Data/Journal.hs

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ module Hledger.Data.Journal (
4444
journalPostingsAddAccountTags,
4545
journalPostingsKeepAccountTagsOnly,
4646
journalPostingsAddCommodityTags,
47-
journalInferPostingsCostBasis,
4847
journalInferPostingsTransactedCost,
4948
journalCommodityUsesLots,
5049
journalCommodityLotsMethod,
@@ -157,7 +156,7 @@ import Data.List (foldl')
157156
#endif
158157
import Data.List.Extra (nubSort)
159158
import Data.Map.Strict qualified as M
160-
import Data.Maybe (catMaybes, fromMaybe, isJust, isNothing, mapMaybe, maybeToList)
159+
import Data.Maybe (catMaybes, fromMaybe, isNothing, mapMaybe, maybeToList)
161160
import Data.Ord (comparing)
162161
import Data.Set qualified as S
163162
import Data.Text (Text)
@@ -716,10 +715,6 @@ journalCommodityTags Journal{jdeclaredcommoditytags} c =
716715
journalCommodityUsesLots :: Journal -> CommoditySymbol -> Bool
717716
journalCommodityUsesLots j c = any ((== "lots") . T.toLower . fst) (journalCommodityTags j c)
718717

719-
-- | Does this posting have a 'lots:' tag (eg inherited from its account) ?
720-
postingUsesLots :: Posting -> Bool
721-
postingUsesLots p = any ((== "lots") . T.toLower . fst) (ptags p)
722-
723718
-- | Get the reduction method from a commodity's lots: tag value, if any.
724719
journalCommodityLotsMethod :: Journal -> CommoditySymbol -> Maybe ReductionMethod
725720
journalCommodityLotsMethod j c =
@@ -795,32 +790,6 @@ journalPostingsAddCommodityTags j = journalMapPostings addtags j
795790
where
796791
addtags p = p `postingAddTags` concatMap (journalCommodityTags j) (postingCommodities p)
797792

798-
-- | For acquire postings (positive amounts) whose commodity or account has a 'lots:' tag,
799-
-- infer cost basis (cost only, no date or label) from transacted cost.
800-
-- The lot date will later default to the transaction date.
801-
-- Must be called before journalClassifyLotPostings.
802-
journalInferPostingsCostBasis :: Journal -> Journal
803-
journalInferPostingsCostBasis j = journalMapPostings (postingInferCostBasis j) j
804-
805-
postingInferCostBasis :: Journal -> Posting -> Posting
806-
postingInferCostBasis j p = p{pamount = mapMixedAmount amountInferCostBasis $ pamount p}
807-
where
808-
amountInferCostBasis :: Amount -> Amount
809-
amountInferCostBasis a
810-
| aquantity a <= 0 = a -- only positive (acquire) amounts
811-
| isJust (acostbasis a) = a -- already has cost basis
812-
| Nothing <- acost a = a -- no transacted cost
813-
| not (journalCommodityUsesLots j (acommodity a) || postingUsesLots p) = a -- commodity/account not lot-tracked
814-
| Just cost <- acost a = a{acostbasis = Just (costToCostBasis (aquantity a) cost)}
815-
816-
costToCostBasis :: Quantity -> AmountCost -> CostBasis
817-
costToCostBasis qty cost = CostBasis{cbCost=Just ucost, cbDate=Nothing, cbLabel=Nothing}
818-
where
819-
ucost = case cost of
820-
UnitCost amt -> amt
821-
TotalCost amt | qty /= 0 -> divideAmountAndCapPrecision (abs qty) amt
822-
| otherwise -> amt
823-
824793
-- | For positive postings with a cost basis, which are not lot transfers,
825794
-- infer transacted cost from cost basis.
826795
-- Must be called after journalClassifyLotPostings so ptype tags are available.

0 commit comments

Comments
 (0)