module History.Cache (cached) where import Data.Binary (Binary, decodeFileOrFail, encodeFile) import Data.Text qualified as T import Git qualified import System.Directory (createDirectoryIfMissing, doesFileExist) import System.FilePath (()) cached :: Binary a => T.Text -> (T.Text -> IO a) -> IO a cached hash func = do root <- Git.getRootDir createDirectoryIfMissing True (root ".anissue") let file = (root ".anissue" T.unpack hash) doesFileExist file >>= \case True -> decodeFileOrFail file >>= \case Left _ -> generate file Right blob -> pure blob False -> generate file where generate file = do blob <- func hash encodeFile file blob pure blob