packages feed

ai-agent-diff-patch-0.0.1.0: src/AIAgent/DiffPatch/Unified/ApplicationBase/Control.hs

{-# LANGUAGE OverloadedStrings #-}

-- | ApplicationBase layer: assembles ProjectedContext projections into
-- the public use-cases and provides the monad-transformer
-- stack (AppContext) with its execution engine (runProjectedContext).
module AIAgent.DiffPatch.Unified.ApplicationBase.Control where

import Control.Monad.Reader  (runReaderT)
import Control.Monad.Except  (runExceptT)

import AIAgent.DiffPatch.Unified.CoreModel.Type
import AIAgent.DiffPatch.Unified.ProjectedContext.Context
  ( patchPC, diffPC
  )

-- ---------------------------------------------------------------------------
-- AppContext execution engine
-- ---------------------------------------------------------------------------

-- | Execute an AppContext action, lowering it into IO.
-- Returns Right on success or Left with an error message on failure.
runProjectedContext :: AppData -> AppContext a -> IO (Either String a)
runProjectedContext appDat ctx =
  runExceptT $ runReaderT ctx appDat

-- ---------------------------------------------------------------------------
-- Use-case runners (Boot layer calls these with an assembled IOFunc)
-- ---------------------------------------------------------------------------

-- | Apply a patch to a file, writing the result via '_writeIOFunc'.
-- Pass 'StdIO.write' for stdout output, or 'FileIO.write for file write-back.
runPatch :: IOFunc -> FilePath -> String -> IO (Either String ())
runPatch ioFunc origFile patchText =
  runProjectedContext (AppData ioFunc) (patchPC origFile patchText)

-- | Alias: runPatchFile and runPatch share the same implementation;
-- the caller differentiates them by supplying a different IOFunc.
runPatchFile :: IOFunc -> FilePath -> String -> IO (Either String ())
runPatchFile = runPatch

-- | Run the diff use-case, writing the result via '_writeIOFunc'.
runDiff :: IOFunc -> FilePath -> FilePath -> IO (Either String ())
runDiff ioFunc oldFile newFile =
  runProjectedContext (AppData ioFunc) (diffPC oldFile newFile)