packages feed

hevm-0.51.2: src/EVM/Facts/Git.hs

-- This is a backend for the fact representation that uses a Git
-- repository as the store.

module EVM.Facts.Git
  ( saveFacts
  , loadFacts
  , RepoAt (..)
  ) where

import EVM.Facts (Fact (..), File (..), Path (..), Data (..), fileToFact, factToFile)

import Optics.Core
import Data.Set   (Set)
import Data.Set qualified as Set
import Data.Maybe (catMaybes)
import Restless.Git qualified as Git

newtype RepoAt = RepoAt String
  deriving (Eq, Ord, Show)

-- For modularity reasons, we have our own file data type that is
-- isomorphic with the one in the `restless-git` library.  We declare
-- the isomorphism so we can go between them easily.
fileRepr :: Iso' File Git.File
fileRepr = iso f g
  where
    f :: File -> Git.File
    f (File     (Path ps p)     (Data x)) =
      Git.File (Git.Path ps p) x

    g :: Git.File -> File
    g (Git.File (Git.Path ps p) x) =
      File     (Path ps p)     (Data x)

saveFacts :: RepoAt -> Set Fact -> IO ()
saveFacts (RepoAt repo) facts =
  Git.save repo "hevm execution"
    (Set.map (view fileRepr . factToFile) facts)

prune :: Ord a => Set (Maybe a) -> Set a
prune = Set.fromList . catMaybes . Set.toList

loadFacts :: RepoAt -> IO (Set Fact)
loadFacts (RepoAt src) =
  fmap
    (prune . Set.map (fileToFact . review fileRepr))
    (Git.load src)