ai-agent-diff-patch-0.0.1.0: src/AIAgent/DiffPatch/Unified/Interface/FileIO.hs
{-# LANGUAGE OverloadedStrings #-}
-- | Interface layer: concrete file I/O implementation for IOFunc.
-- This module is responsible for raw I/O only: reading bytes from disk and
-- writing bytes to disk. All domain logic (line-ending detection, normalisation,
-- restoration) belongs to the ProjectedContext layer.
--
-- IOFunc / AppData values are assembled directly in the Boot layer (Unified.hs)
-- without using Data.Default, keeping construction explicit and visible.
module AIAgent.DiffPatch.Unified.Interface.FileIO
( read
, write
) where
import Prelude hiding (read)
import qualified Data.ByteString as BS
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import Data.Text.Encoding.Error (lenientDecode)
-- | Read a file in binary mode and decode as UTF-8 (lenient).
-- Returns raw Text with original line endings preserved.
-- Line-ending normalisation is a domain concern and is handled in ProjectedContext.
read :: FilePath -> IO T.Text
read path = do
bs <- BS.readFile path
return (TE.decodeUtf8With lenientDecode bs)
-- | Write Text to a file as UTF-8 binary.
-- The caller is responsible for any line-ending conversion before calling this.
write :: FilePath -> T.Text -> IO ()
write path txt =
BS.writeFile path (TE.encodeUtf8 txt)