module Darcs.Patch.Apply
(
Apply(..)
, applyToPaths
, applyToTree
, applyToState
, maybeApplyToTree
, effectOnPaths
) where
import Darcs.Prelude
import Control.Exception ( catch, IOException )
import Darcs.Util.Path ( AnchoredPath )
import Darcs.Util.Tree ( Tree )
import Darcs.Patch.ApplyMonad ( ApplyMonad(..), withFileNames, ApplyMonadTrans(..) )
import Darcs.Patch.Invert ( Invert(..) )
import Darcs.Patch.Witnesses.Ordered ( FL(..), RL(..) )
class Apply p where
type ApplyState p :: (* -> *) -> *
apply :: ApplyMonad (ApplyState p) m => p wX wY -> m ()
unapply :: ApplyMonad (ApplyState p) m => p wX wY -> m ()
default unapply :: (ApplyMonad (ApplyState p) m, Invert p) => p wX wY -> m ()
unapply = apply . invert
instance Apply p => Apply (FL p) where
type ApplyState (FL p) = ApplyState p
apply NilFL = return ()
apply (p:>:ps) = apply p >> apply ps
unapply NilFL = return ()
unapply (p:>:ps) = unapply ps >> unapply p
instance Apply p => Apply (RL p) where
type ApplyState (RL p) = ApplyState p
apply NilRL = return ()
apply (ps:<:p) = apply ps >> apply p
unapply NilRL = return ()
unapply (ps:<:p) = unapply p >> unapply ps
effectOnPaths :: (Apply p, ApplyState p ~ Tree)
=> p wX wY
-> [AnchoredPath]
-> [AnchoredPath]
effectOnPaths p fps = fps' where
(_, fps', _) = applyToPaths p Nothing fps
applyToPaths :: (Apply p, ApplyState p ~ Tree)
=> p wX wY
-> Maybe [(AnchoredPath, AnchoredPath)]
-> [AnchoredPath]
-> ([AnchoredPath], [AnchoredPath], [(AnchoredPath, AnchoredPath)])
applyToPaths pa ofpos fs = withFileNames ofpos fs (apply pa)
applyToTree :: (Apply p, Monad m, ApplyState p ~ Tree)
=> p wX wY
-> Tree m
-> m (Tree m)
applyToTree = applyToState
applyToState :: forall p m wX wY. (Apply p, ApplyMonadTrans (ApplyState p) m)
=> p wX wY
-> (ApplyState p) m
-> m ((ApplyState p) m)
applyToState patch t = snd <$> runApplyMonad (apply patch) t
maybeApplyToTree :: (Apply p, ApplyState p ~ Tree) => p wX wY -> Tree IO
-> IO (Maybe (Tree IO))
maybeApplyToTree patch tree =
(Just `fmap` applyToTree patch tree) `catch` (\(_ :: IOException) -> return Nothing)