#include "gadts.h"
module Darcs.Patch.Apply
(
Apply(..)
, applyToFilePaths
, applyToTree
, applyToState
, effectOnFilePaths
) where
import Prelude hiding ( catch, pi )
import Control.Applicative ( (<$>) )
import Control.Arrow ( (***) )
import Storage.Hashed.Tree( Tree )
import Storage.Hashed.Monad( virtualTreeMonad )
import Darcs.Patch.ApplyMonad ( ApplyMonad(..), withFileNames, ApplyMonadTrans(..) )
import Darcs.Patch.FileName( fn2fp, fp2fn )
import Darcs.Witnesses.Ordered ( FL(..), RL(..) )
class Apply p where
type ApplyState p :: (* -> *) -> *
apply :: ApplyMonad m (ApplyState p) => p C(x y) -> m ()
instance Apply p => Apply (FL p) where
type ApplyState (FL p) = ApplyState p
apply NilFL = return ()
apply (p:>:ps) = apply p >> apply ps
instance Apply p => Apply (RL p) where
type ApplyState (RL p) = ApplyState p
apply NilRL = return ()
apply (p:<:ps) = apply ps >> apply p
effectOnFilePaths :: (Apply p, ApplyState p ~ Tree)
=> p C(x y)
-> [FilePath]
-> [FilePath]
effectOnFilePaths p fps = fps' where
(_, fps', _) = applyToFilePaths p Nothing fps
applyToFilePaths :: (Apply p, ApplyState p ~ Tree)
=> p C(x y)
-> Maybe [(FilePath, FilePath)]
-> [FilePath]
-> ([FilePath], [FilePath], [(FilePath, FilePath)])
applyToFilePaths pa ofpos fs = toFPs $ withFileNames ofnos fns (apply pa) where
fns = map fp2fn fs
ofnos = map (fp2fn *** fp2fn) <$> ofpos
toFPs (affected, new, renames) =
(map fn2fp affected, map fn2fp new, map (fn2fp *** fn2fp) renames)
applyToTree :: (Apply p, Functor m, Monad m, ApplyState p ~ Tree)
=> p C(x y)
-> Tree m
-> m (Tree m)
applyToTree patch t = snd <$> virtualTreeMonad (apply patch) t
applyToState :: forall p m C(x y). (Apply p, ApplyMonadTrans m (ApplyState p))
=> p C(x y)
-> (ApplyState p) m
-> m ((ApplyState p) m)
applyToState patch t = snd <$> runApplyMonad (apply patch) t