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 Data.Text (Text)
import qualified Data.Text as T
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 = Text
type PatternText = Text
type RuleAction = FilePath -> IO ()
data Rule = Rule
{ name :: Text
, 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 = let packed = T.pack x in def { pattern = packed, name = packed}
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 -> Text -> Rule
r |# p = r { name = p }
add', modify', delete', addModify :: (FilePath -> IO a) -> Rule -> Rule
add' = flip (|+)
modify' = flip (|%)
delete' = flip (|-)
name' = flip (|#)
addModify = flip (|>)
data RuleIssue
= PatternCompliationFailed PatternText String
deriving Show
compilePattern :: FilePath -> PatternText -> Either RuleIssue (FilePath -> Bool)
compilePattern currentDir pattern = left (PatternCompliationFailed pattern) $ do
let absolutePattern = traceShowId $ encodeString currentDir <> "/" <> T.unpack pattern
p <- tryCompileWith compDefault absolutePattern
let test = match $ simplify p
return $ \x -> test $ encodeString x