This repository has been archived by the owner on Aug 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
24 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,34 @@ | ||
{-# LANGUAGE GeneralizedNewtypeDeriving, DeriveGeneric #-} | ||
module Oracles.DirectoryContent ( | ||
getDirectoryContent, directoryContentOracle, Match(..) | ||
directoryContent, directoryContentOracle, Match (..) | ||
) where | ||
|
||
import Base | ||
import GHC.Generics | ||
import System.Directory.Extra | ||
import GHC.Generics | ||
|
||
import Base | ||
|
||
newtype DirectoryContent = DirectoryContent (Match, FilePath) | ||
deriving (Binary, Eq, Hashable, NFData, Show, Typeable) | ||
|
||
data Match = Test FilePattern | Not (Match) | And [Match] | Or [Match] | ||
data Match = Test FilePattern | Not Match | And [Match] | Or [Match] | ||
deriving (Generic, Eq, Show, Typeable) | ||
instance Binary Match | ||
instance Hashable Match | ||
instance NFData Match | ||
|
||
matches :: Match -> FilePath -> Bool | ||
matches (Test m) f = m ?== f | ||
matches (Not m) f = not $ matches m f | ||
matches (And []) _ = True | ||
matches (And (m:ms)) f | matches m f = matches (And ms) f | ||
| otherwise = False | ||
matches (Or []) _ = False | ||
matches (Or (m:ms)) f | matches m f = True | ||
| otherwise = matches (Or ms) f | ||
matches (Test p) f = p ?== f | ||
matches (Not m) f = not $ matches m f | ||
matches (And ms) f = all (`matches` f) ms | ||
matches (Or ms) f = any (`matches` f) ms | ||
|
||
-- | Get the directory content recursively. | ||
getDirectoryContent :: Match -> FilePath -> Action [FilePath] | ||
getDirectoryContent expr dir = | ||
askOracle $ DirectoryContent (expr, dir) | ||
directoryContent :: Match -> FilePath -> Action [FilePath] | ||
directoryContent expr dir = askOracle $ DirectoryContent (expr, dir) | ||
|
||
directoryContentOracle :: Rules () | ||
directoryContentOracle = void $ addOracle oracle | ||
where | ||
oracle :: DirectoryContent -> Action [FilePath] | ||
oracle (DirectoryContent (expr, dir)) = | ||
liftIO $ filter (matches expr) <$> listFilesInside (return . matches expr) dir | ||
directoryContentOracle = void $ | ||
addOracle $ \(DirectoryContent (expr, dir)) -> liftIO $ | ||
filter (matches expr) <$> listFilesInside (return . matches expr) dir | ||
|
||
instance Binary Match | ||
instance Hashable Match | ||
instance NFData Match |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters