module Twitch.Rule where
import Data.Map (Map)
import Control.Applicative
import Control.Monad
import Filesystem.Path
import Filesystem.Path.CurrentOS
import Prelude hiding (FilePath)
import System.Directory
import System.FilePath.Glob
import Data.Monoid
import Data.Time.Clock
import Data.String
import Data.Default
import Control.Arrow
import Data.Either
import Debug.Trace
import System.FSNotify (WatchManager)
type Name = String
type PatternText = String
type RuleAction = FilePath -> IO ()
data Rule = Rule
{ name :: String
, pattern :: PatternText
, add :: RuleAction
, modify :: RuleAction
, delete :: RuleAction
}
instance Default Rule where
def = Rule
{ name = ""
, pattern = ""
, add = def
, modify = def
, delete = def
}
instance IsString Rule where
fromString x = def { pattern = x, name = x}
infixl 8 |+, |%, |-, |>, |#
(|+), (|%), (|-), (|>) :: Rule -> (FilePath -> IO a) -> Rule
x |+ f = x { add = void . f }
x |% f = x { modify = void . f }
x |- f = x { delete = void . f }
x |> f = x |+ f |% f
(|#) :: Rule -> String -> Rule
r |# p = r { name = p }
addF, modifyF, deleteF, addModifyF :: (FilePath -> IO a) -> Rule -> Rule
addF = flip (|+)
modifyF = flip (|%)
deleteF = flip (|-)
nameF = flip (|#)
addModifyF = flip (|>)
data RuleIssue
= PatternCompliationFailed PatternText String
deriving Show
compilePattern :: FilePath
-> PatternText
-> Either RuleIssue (FilePath -> Bool)
compilePattern currentDir pattern = left (PatternCompliationFailed pattern) $ do
let absolutePattern = encodeString currentDir <> "/" <> pattern
p <- tryCompileWith compDefault absolutePattern
let test = match $ simplify p
return $ \x -> test $ encodeString x