pwncash/lib/Internal/Utils.hs

505 lines
16 KiB
Haskell
Raw Normal View History

{-# LANGUAGE FlexibleContexts #-}
2022-12-11 17:51:11 -05:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
2022-12-11 17:51:11 -05:00
2023-01-28 19:32:56 -05:00
module Internal.Utils
( compareDate
, intervalMaybeBounds
, fmtRational
, matches
, fromGregorian'
, resolveBounds
, leftToMaybe
, dec2Rat
, concatEithers2
, concatEither2
, parseRational
, showError
, unlessLeft
, unlessLefts
, inMaybeBounds
, acntPath2Text
, showT
2023-01-28 19:32:56 -05:00
)
where
2022-12-11 17:51:11 -05:00
2023-01-25 20:52:27 -05:00
import Data.Time.Format.ISO8601
import GHC.Real
import Internal.Types
import RIO
2023-01-25 20:52:27 -05:00
import qualified RIO.List as L
import qualified RIO.Map as M
import qualified RIO.Text as T
import RIO.Time
import Text.Regex.TDFA
2022-12-11 17:51:11 -05:00
2023-01-27 23:33:34 -05:00
--------------------------------------------------------------------------------
2023-01-28 19:32:56 -05:00
-- dates
gregTup :: Gregorian -> (Integer, Int, Int)
gregTup Gregorian {..} =
( fromIntegral gYear
, fromIntegral gMonth
, fromIntegral gDay
)
gregMTup :: GregorianM -> (Integer, Int)
gregMTup GregorianM {..} =
( fromIntegral gmYear
, fromIntegral gmMonth
)
data YMD_ = Y_ Integer | YM_ Integer Int | YMD_ Integer Int Int
fromMatchYMD :: MatchYMD -> YMD_
fromMatchYMD m = case m of
2023-01-28 19:32:56 -05:00
Y y -> Y_ $ fromIntegral y
YM g -> uncurry YM_ $ gregMTup g
YMD g -> uncurry3 YMD_ $ gregTup g
compareDate :: MatchDate -> Day -> Ordering
compareDate (On md) x =
case fromMatchYMD md of
2023-01-25 23:04:54 -05:00
Y_ y' -> compare y y'
YM_ y' m' -> compare (y, m) (y', m')
YMD_ y' m' d' -> compare (y, m, d) (y', m', d')
2022-12-11 17:51:11 -05:00
where
(y, m, d) = toGregorian x
2023-01-25 23:04:54 -05:00
compareDate (In md offset) x = do
2023-01-28 19:32:56 -05:00
case fromMatchYMD md of
2023-01-25 23:04:54 -05:00
Y_ y' -> compareRange y' y
YM_ y' m' -> compareRange (toMonth y' m') $ toMonth y m
YMD_ y' m' d' ->
let s = toModifiedJulianDay $ fromGregorian y' m' d'
in compareRange s $ toModifiedJulianDay x
2022-12-11 17:51:11 -05:00
where
(y, m, _) = toGregorian x
compareRange start z
2022-12-11 17:51:11 -05:00
| z < start = LT
2022-12-22 20:13:03 -05:00
| otherwise = if (start + fromIntegral offset - 1) < z then GT else EQ
toMonth year month = (year * 12) + fromIntegral month
2022-12-11 17:51:11 -05:00
2023-01-28 19:32:56 -05:00
-- boundsFromGregorian :: (Gregorian, Gregorian) -> Bounds
-- boundsFromGregorian (a, b) = (fromGregorian' a, fromGregorian' b)
2022-12-11 17:51:11 -05:00
2023-01-28 19:32:56 -05:00
fromGregorian' :: Gregorian -> Day
fromGregorian' = uncurry3 fromGregorian . gregTup
2022-12-11 17:51:11 -05:00
2023-01-28 19:32:56 -05:00
-- inBounds :: Bounds -> Day -> Bool
-- inBounds (d0, d1) x = d0 <= x && x <= d1
2023-01-25 23:04:54 -05:00
2023-01-27 23:33:34 -05:00
inMaybeBounds :: MaybeBounds -> Day -> Bool
inMaybeBounds (d0, d1) x = maybe True (x >=) d0 && maybe True (x <=) d1
2022-12-11 17:51:11 -05:00
2023-01-28 19:32:56 -05:00
intervalMaybeBounds :: Interval -> MaybeBounds
intervalMaybeBounds Interval {intStart = s, intEnd = e} =
(fmap fromGregorian' s, fmap fromGregorian' e)
2022-12-11 17:51:11 -05:00
2023-01-27 23:33:34 -05:00
resolveBounds :: MonadUnliftIO m => MaybeBounds -> m Bounds
resolveBounds (s, e) = do
s' <- maybe getDay return s
e' <- maybe (addGregorianYearsClip 50 <$> getDay) return e
return (s', e')
where
getDay = utctDay <$> getCurrentTime
--------------------------------------------------------------------------------
-- matching
2022-12-11 17:51:11 -05:00
2023-01-26 23:41:45 -05:00
matches :: Match -> TxRecord -> EitherErrs (MatchRes RawTx)
matches Match {..} r@TxRecord {..} = do
2023-01-28 20:03:58 -05:00
res <- concatEither3 val other desc $ \x y z -> x && y && z
if date && res
2023-01-27 23:33:34 -05:00
then maybe (Right MatchSkip) (fmap MatchPass . convert) mTx
2023-01-25 23:04:54 -05:00
else Right MatchFail
2022-12-11 17:51:11 -05:00
where
2023-01-26 23:41:45 -05:00
val = valMatches mVal trAmount
2023-01-28 19:32:56 -05:00
date = maybe True (`dateMatches` trDate) mDate
2023-01-27 23:33:34 -05:00
other = foldM (\a o -> (a &&) <$> otherMatches trOther o) True mOther
2023-01-26 23:41:45 -05:00
desc = maybe (return True) (matchMaybe trDesc) mDesc
2023-01-27 23:33:34 -05:00
convert (ToTx cur a ss) = toTx cur a ss r
2022-12-11 17:51:11 -05:00
2023-01-26 23:41:45 -05:00
toTx :: SplitCur -> SplitAcnt -> [ExpSplit] -> TxRecord -> EitherErrs RawTx
toTx sc sa toSplits r@TxRecord {..} =
concatEithers2 acRes ssRes $ \(a_, c_) ss_ ->
let fromSplit =
Split
{ sAcnt = a_
, sCurrency = c_
, sValue = Just trAmount
, sComment = ""
}
in Tx
{ txTags = []
, txDate = trDate
, txDescr = trDesc
, txSplits = fromSplit : ss_
2023-01-25 23:04:54 -05:00
}
2023-01-26 23:41:45 -05:00
where
2023-01-27 23:33:34 -05:00
acRes = concatEithers2 (resolveAcnt r sa) (resolveCurrency r sc) (,)
ssRes = concatEithersL $ fmap (resolveSplit r) toSplits
2023-01-28 20:03:58 -05:00
valMatches :: MatchVal -> Rational -> EitherErr Bool
valMatches MatchVal {..} x
| Just d_ <- mvDen, d_ >= p = Left $ MatchValPrecisionError d_ p
| otherwise =
Right $
checkMaybe (s ==) mvSign
&& checkMaybe (n ==) mvNum
&& checkMaybe ((d * fromIntegral p ==) . fromIntegral) mvDen
2023-01-27 23:33:34 -05:00
where
(n, d) = properFraction $ abs x
p = 10 ^ mvPrec
s = signum x >= 0
checkMaybe = maybe True
2023-01-28 19:32:56 -05:00
dateMatches :: MatchDate -> Day -> Bool
dateMatches md = (EQ ==) . compareDate md
2023-01-27 23:33:34 -05:00
otherMatches :: M.Map T.Text T.Text -> MatchOther -> EitherErr Bool
otherMatches dict m = case m of
2023-01-28 20:03:58 -05:00
Val (Field n mv) -> valMatches mv =<< (readRational =<< lookup_ MatchNumeric n)
2023-01-27 23:33:34 -05:00
Desc (Field n md) -> (`matchMaybe` md) =<< lookup_ MatchText n
where
lookup_ t n = lookupErr (MatchField t) n dict
resolveSplit :: TxRecord -> ExpSplit -> EitherErrs RawSplit
resolveSplit r s@Split {sAcnt = a, sValue = v, sCurrency = c} =
concatEithers2 acRes valRes $
\(a_, c_) v_ -> (s {sAcnt = a_, sValue = v_, sCurrency = c_})
where
acRes = concatEithers2 (resolveAcnt r a) (resolveCurrency r c) (,)
valRes = plural $ mapM (resolveValue r) v
resolveValue :: TxRecord -> SplitNum -> EitherErr Rational
resolveValue r s = case s of
(LookupN t) -> readRational =<< lookupErr SplitValField t (trOther r)
(ConstN c) -> Right $ dec2Rat c
AmountN -> Right $ trAmount r
resolveAcnt :: TxRecord -> SplitAcnt -> EitherErrs T.Text
resolveAcnt = resolveSplitField AcntField
resolveCurrency :: TxRecord -> SplitCur -> EitherErrs T.Text
resolveCurrency = resolveSplitField CurField
resolveSplitField :: SplitIDType -> TxRecord -> SplitAcnt -> EitherErrs T.Text
resolveSplitField t TxRecord {trOther = o} s = case s of
ConstT p -> Right p
LookupT f -> plural $ lookup_ f o
MapT (Field f m) -> plural $ do
k <- lookup_ f o
lookup_ k m
Map2T (Field (f1, f2) m) -> do
(k1, k2) <- concatEither2 (lookup_ f1 o) (lookup_ f2 o) (,)
plural $ lookup_ (k1, k2) m
where
lookup_ :: (Ord k, Show k) => k -> M.Map k v -> EitherErr v
lookup_ = lookupErr (SplitIDField t)
lookupErr :: (Ord k, Show k) => LookupSuberr -> k -> M.Map k v -> EitherErr v
lookupErr what k m = case M.lookup k m of
Just x -> Right x
_ -> Left $ LookupError what $ showT k
2022-12-11 17:51:11 -05:00
parseRational :: MonadFail m => T.Text -> T.Text -> m Rational
parseRational pat s = case ms of
[sign, x, ""] -> uncurry (*) <$> readWhole sign x
[sign, x, y] -> do
d <- readT "decimal" y
let p = 10 ^ T.length y
(k, w) <- readWhole sign x
return $ k * (w + d % p)
_ -> msg "malformed decimal"
2022-12-11 17:51:11 -05:00
where
(_, _, _, ms) = (s =~ pat) :: (T.Text, T.Text, T.Text, [T.Text])
readT what t = case readMaybe $ T.unpack t of
Just d -> return $ fromInteger d
_ -> msg $ T.unwords ["could not parse", what, t]
msg m =
fail $
T.unpack $
T.concat
[ m
, "; pattern = "
, pat
, "; query = "
, s
]
2022-12-11 17:51:11 -05:00
readSign x
| x == "-" = return (-1)
| x == "+" || x == "" = return 1
| otherwise = msg $ T.append "invalid sign: " x
readWhole sign x = do
w <- readT "whole number" x
k <- readSign sign
return (k, w)
2023-01-25 23:04:54 -05:00
readRational :: T.Text -> EitherErr Rational
readRational s = case T.split (== '.') s of
[x] -> maybe err (return . fromInteger) $ readT x
[x, y] -> case (readT x, readT y) of
(Just x', Just y') ->
let p = 10 ^ T.length y
k = if x' >= 0 then 1 else -1
in return $ fromInteger x' + k * y' % p
_ -> err
_ -> err
2022-12-11 17:51:11 -05:00
where
2023-01-25 23:04:54 -05:00
readT = readMaybe . T.unpack
err = Left $ ConversionError s
2022-12-11 17:51:11 -05:00
-- TODO smells like a lens
2023-01-28 19:32:56 -05:00
-- mapTxSplits :: (a -> b) -> Tx a -> Tx b
-- mapTxSplits f t@Tx {txSplits = ss} = t {txSplits = fmap f ss}
2022-12-11 17:51:11 -05:00
fmtRational :: Natural -> Rational -> T.Text
fmtRational precision x = T.concat [s, txt n', ".", pad 2 "0" $ txt d']
where
s = if x >= 0 then "" else "-"
x'@(n :% d) = abs x
2022-12-11 17:51:11 -05:00
p = 10 ^ precision
n' = div n d
d' = (\(a :% b) -> div a b) ((x' - fromIntegral n') * p)
2022-12-11 17:51:11 -05:00
txt = T.pack . show
pad i c z = T.append (T.replicate (i - T.length z) c) z
dec2Rat :: Decimal -> Rational
2022-12-14 23:59:23 -05:00
dec2Rat D {..} =
k * (fromIntegral whole + (fromIntegral decimal % (10 ^ precision)))
2022-12-11 17:51:11 -05:00
where
2022-12-14 23:59:23 -05:00
k = if sign then 1 else -1
2022-12-11 17:51:11 -05:00
acntPath2Text :: AcntPath -> T.Text
acntPath2Text (AcntPath t cs) = T.intercalate "/" (atName t : cs)
2023-01-24 23:24:41 -05:00
2023-01-27 23:33:34 -05:00
--------------------------------------------------------------------------------
-- error display
2023-01-24 23:24:41 -05:00
showError :: InsertError -> [T.Text]
showError (StatementError ts ms) = (showTx <$> ts) ++ (showMatch <$> ms)
showError other = (: []) $ case other of
(RegexError re) -> T.append "could not make regex from pattern: " re
(ConversionError x) -> T.append "Could not convert to rational number: " x
(InsertIOError msg) -> T.append "IO Error: " msg
2023-01-28 20:03:58 -05:00
(MatchValPrecisionError d p) ->
T.unwords ["Match denominator", showT d, "must be less than", showT p]
2023-01-27 23:33:34 -05:00
(LookupError t f) ->
T.unwords
[ "Could not find field"
, singleQuote f
, "when resolving"
, what
]
where
what = case t of
SplitIDField st ->
T.unwords
[ "split"
, case st of AcntField -> "account"; CurField -> "currency"
, "ID"
]
SplitValField -> "split value"
MatchField mt ->
T.unwords
[ case mt of MatchNumeric -> "numeric"; MatchText -> "text"
, "match"
]
(AllocationError t dp) -> T.concat [msg, ": datepattern=", showT dp]
where
msg = case t of
NoAllocations -> "No post-tax allocations present"
ExceededTotal -> "Allocations exceed total income"
MissingBlank -> "No blank allocation to balance"
TooManyBlanks -> "Cannot balance multiple blank allocations"
2023-01-25 20:52:27 -05:00
(BalanceError t cur rss) ->
2023-01-27 23:33:34 -05:00
T.unwords
2023-01-25 20:52:27 -05:00
[ msg
2023-01-27 23:33:34 -05:00
, "for currency"
2023-01-25 20:52:27 -05:00
, singleQuote cur
2023-01-27 23:33:34 -05:00
, "and for splits"
2023-01-25 20:52:27 -05:00
, splits
]
where
msg = case t of
TooFewSplits -> "Need at least two splits to balance"
NotOneBlank -> "Exactly one split must be blank"
2023-01-28 20:03:58 -05:00
splits = T.intercalate ", " $ fmap (singleQuote . showSplit) rss
2023-01-25 20:52:27 -05:00
showTx :: TxRecord -> T.Text
showTx TxRecord {trDate = d, trAmount = v, trDesc = e, trFile = f} =
T.append "Unmatched transaction: " $
keyVals
[ ("path", T.pack f)
, ("date", T.pack $ iso8601Show d)
, ("value", showT (fromRational v :: Float))
, ("description", doubleQuote e)
]
showMatch :: Match -> T.Text
showMatch Match {mDate = d, mVal = v, mDesc = e, mOther = o, mTimes = n, mPriority = p} =
T.append "Unused match: " $ keyVals [(x, y) | (x, Just y) <- kvs]
where
kvs =
[ ("date", showMatchDate <$> d)
, ("val", showMatchVal v)
, ("desc", e)
, ("other", others)
2023-01-28 20:05:26 -05:00
, ("counter", Just $ maybe "Inf" showT n)
2023-01-25 20:52:27 -05:00
, ("priority", Just $ showT p)
]
others = case o of
[] -> Nothing
2023-01-28 20:03:58 -05:00
xs -> Just $ singleQuote $ T.concat $ showMatchOther <$> xs
2023-01-25 20:52:27 -05:00
2023-01-28 18:52:28 -05:00
-- | Convert match date to text
2023-01-28 19:32:56 -05:00
-- Single date matches will just show the single date, and ranged matches will
-- show an interval like [YY-MM-DD, YY-MM-DD)
2023-01-25 20:52:27 -05:00
showMatchDate :: MatchDate -> T.Text
showMatchDate md = case md of
2023-01-28 18:52:28 -05:00
(On x) -> showMatchYMD x
2023-01-28 19:32:56 -05:00
(In start n) -> T.concat ["[", showMatchYMD start, " ", showYMD_ end, ")"]
2023-01-28 18:52:28 -05:00
where
2023-01-28 19:32:56 -05:00
-- TODO not DRY (this shifting thing happens during the comparison
-- function (kinda)
end = case fromMatchYMD start of
Y_ y -> Y_ $ y + fromIntegral n
YM_ y m ->
let (y_, m_) = divMod (m + fromIntegral n - 1) 12
in YM_ (y + fromIntegral y_) (m + m_ + 1)
YMD_ y m d ->
uncurry3 YMD_ $
toGregorian $
addDays (fromIntegral n) $
fromGregorian y m d
2023-01-28 18:52:28 -05:00
-- | convert YMD match to text
showMatchYMD :: MatchYMD -> T.Text
2023-01-28 19:32:56 -05:00
showMatchYMD = showYMD_ . fromMatchYMD
showYMD_ :: YMD_ -> T.Text
showYMD_ md =
2023-01-28 18:52:28 -05:00
T.intercalate "-" $ L.take 3 (fmap showT digits ++ L.repeat "*")
where
digits = case md of
2023-01-28 19:32:56 -05:00
Y_ y -> [fromIntegral y]
YM_ y m -> [fromIntegral y, m]
YMD_ y m d -> [fromIntegral y, m, d]
2023-01-25 20:52:27 -05:00
showMatchVal :: MatchVal -> Maybe T.Text
2023-01-28 18:52:28 -05:00
showMatchVal MatchVal {mvSign = Nothing, mvNum = Nothing, mvDen = Nothing} = Nothing
2023-01-28 20:03:58 -05:00
showMatchVal MatchVal {..} = Just $ singleQuote $ keyVals [(k, v) | (k, Just v) <- kvs]
2023-01-28 18:52:28 -05:00
where
2023-01-28 20:03:58 -05:00
kvs =
[ ("sign", (\s -> if s then "+" else "-") <$> mvSign)
, ("numerator", showT <$> mvNum)
, ("denominator", showT <$> mvDen)
, ("precision", Just $ showT mvPrec)
]
2023-01-25 20:52:27 -05:00
showMatchOther :: MatchOther -> T.Text
2023-01-28 20:03:58 -05:00
showMatchOther (Desc (Field f re)) =
T.unwords ["desc field", singleQuote f, "with re", singleQuote re]
showMatchOther (Val (Field f mv)) =
T.unwords
[ "val field"
, singleQuote f
, "with match value"
, singleQuote $ fromMaybe "*" $ showMatchVal mv
]
2023-01-25 20:52:27 -05:00
showSplit :: RawSplit -> T.Text
showSplit Split {sAcnt = a, sValue = v, sComment = c} =
2023-01-28 20:03:58 -05:00
keyVals
[ ("account", a)
, ("value", T.pack $ show ((fromRational <$> v) :: Maybe Float))
, ("comment", doubleQuote c)
]
2023-01-25 20:52:27 -05:00
singleQuote :: T.Text -> T.Text
singleQuote t = T.concat ["'", t, "'"]
doubleQuote :: T.Text -> T.Text
doubleQuote t = T.concat ["'", t, "'"]
keyVal :: T.Text -> T.Text -> T.Text
keyVal a b = T.concat [a, "=", b]
keyVals :: [(T.Text, T.Text)] -> T.Text
keyVals = T.intercalate "; " . fmap (uncurry keyVal)
2023-01-26 23:41:45 -05:00
2023-01-27 23:33:34 -05:00
showT :: Show a => a -> T.Text
showT = T.pack . show
--------------------------------------------------------------------------------
-- pure error processing
2023-01-26 23:41:45 -05:00
concatEither2 :: Either x a -> Either x b -> (a -> b -> c) -> Either [x] c
concatEither2 a b fun = case (a, b) of
(Right a_, Right b_) -> Right $ fun a_ b_
_ -> Left $ catMaybes [leftToMaybe a, leftToMaybe b]
2023-01-28 20:03:58 -05:00
concatEither3 :: Either x a -> Either x b -> Either x c -> (a -> b -> c -> d) -> Either [x] d
concatEither3 a b c fun = case (a, b, c) of
(Right a_, Right b_, Right c_) -> Right $ fun a_ b_ c_
_ -> Left $ catMaybes [leftToMaybe a, leftToMaybe b, leftToMaybe c]
2023-01-26 23:41:45 -05:00
concatEithers2 :: Either [x] a -> Either [x] b -> (a -> b -> c) -> Either [x] c
2023-01-27 23:33:34 -05:00
concatEithers2 a b = merge . concatEither2 a b
2023-01-26 23:41:45 -05:00
2023-01-28 19:32:56 -05:00
-- concatEithers3
-- :: Either [x] a
-- -> Either [x] b
-- -> Either [x] c
-- -> (a -> b -> c -> d)
-- -> Either [x] d
-- concatEithers3 a b c = merge . concatEither3 a b c
2023-01-26 23:41:45 -05:00
concatEitherL :: [Either x a] -> Either [x] [a]
concatEitherL as = case partitionEithers as of
([], bs) -> Right bs
(es, _) -> Left es
concatEithersL :: [Either [x] a] -> Either [x] [a]
2023-01-27 23:33:34 -05:00
concatEithersL = merge . concatEitherL
2023-01-26 23:41:45 -05:00
leftToMaybe :: Either a b -> Maybe a
leftToMaybe (Left a) = Just a
leftToMaybe _ = Nothing
2023-01-27 20:31:13 -05:00
unlessLeft :: (Monad m, MonadPlus n) => Either a b -> (b -> m ()) -> m (n a)
unlessLeft (Left es) _ = return (return es)
unlessLeft (Right rs) f = f rs >> return mzero
unlessLefts :: (Monad m, MonadPlus n) => Either (n a) b -> (b -> m ()) -> m (n a)
unlessLefts (Left es) _ = return es
unlessLefts (Right rs) f = f rs >> return mzero
2023-01-27 23:33:34 -05:00
plural :: Either a b -> Either [a] b
plural = first (: [])
merge :: Either [[a]] b -> Either [a] b
merge = first concat
--------------------------------------------------------------------------------
-- random functions
-- when bifunctor fails...
2023-01-28 19:32:56 -05:00
-- thrice :: (a -> d) -> (b -> e) -> (c -> f) -> (a, b, c) -> (d, e, f)
-- thrice f1 f2 f3 (x, y, z) = (f1 x, f2 y, f3 z)
2023-01-27 23:33:34 -05:00
uncurry3 :: (a -> b -> c -> d) -> (a, b, c) -> d
uncurry3 f (a, b, c) = f a b c
2023-01-28 19:32:56 -05:00
-- lpad :: a -> Int -> [a] -> [a]
-- lpad c n s = replicate (n - length s) c ++ s
2023-01-27 23:33:34 -05:00
2023-01-28 19:32:56 -05:00
-- rpad :: a -> Int -> [a] -> [a]
-- rpad c n s = s ++ replicate (n - length s) c
2023-01-27 23:33:34 -05:00
2023-01-28 20:03:58 -05:00
-- lpadT :: Char -> Int -> T.Text -> T.Text
-- lpadT c n s = T.append (T.replicate (n - T.length s) (T.singleton c)) s
2023-01-28 18:52:28 -05:00
2023-01-27 23:33:34 -05:00
matchMaybe :: RegexContext Regex query b => query -> T.Text -> EitherErr b
matchMaybe q re = first (const $ RegexError re) $ pureTry $ q =~ re