{-|
Copyright   : (c) Hisaket VioletRed, 2022
License     : AGPL-3.0-or-later
Maintainer  : hisaket@outlook.jp
Stability   : experimental
Portability : POSIX

Example\:

>>> import qualified Polysemy.SequentialAccess as SA
>>> import qualified Polysemy.SequentialAccess.Text as SAT
>>> import Polysemy ( runFinal, embedToFinal, embed )
>>> import Polysemy.Resource ( resourceToIOFinal )
>>> import Polysemy.Path
>>> import Polysemy.FS.Scoped ( scopedFile, AccessMode (RwAccess) )
>>> import Control.Monad.Extra ( whenM )
>>> import System.Directory ( doesFileExist, removeFile )
>>> import Control.Exception ( throwIO )
>>> :{
runFinal $ embedToFinal $ resourceToIOFinal $ rwAccessToIO do 
    let path = [absfile|/tmp/polysemy-scoped-fs-test|]
    embed $ whenM (doesFileExist $ toFilePath path) $
        throwIO $ userError "Abort to prevent overwriting."
    scopedFile @RwAccess path do
        SA.extend "This text will be deleted."
        SA.resize SA.NullSize
        SA.extend "foo"
        SA.extend "bar"
        SA.seek SA.TOF
        embed . print =<< SA.read SA.ToEnd
    embed $ removeFile $ toFilePath path
:}
"foobar"

-}

module Polysemy.FS.Scoped.Text where

import Polysemy.FS.Scoped.Text.Internal
    ( readToIO, extendToIO, clearToIO, cursorToIO, scopedTextFileToIO )

import Polysemy ( embed, Embed, Members, interpret )
import Polysemy.FS.Scoped
    ( Access
    , AccessMode (ReadAccess, AppendAccess, RwAccess, WriteAccess)
    , Format (TextFormat)
    )
import qualified Polysemy.SequentialAccess.Text as SAT
import qualified System.IO as IO
import qualified Data.Text.IO as TIO
import qualified Polysemy.SequentialAccess as SA
import Control.Category ( (>>>) )
import Polysemy.Resource ( Resource )


-- | An interpreter for read open mode with text.
readAccessToIO
    ::  Members '[Embed IO, Resource] r
    =>  Access TextFormat ReadAccess (SAT.ReadLine ': SAT.ReadToEnd ': SAT.Cursor) r b
readAccessToIO :: Access 'TextFormat 'ReadAccess (ReadLine : ReadToEnd : Cursor) r b
readAccessToIO =
    IOMode
-> (Handle -> InterpretersFor (ReadLine : ReadToEnd : Cursor) r)
-> (forall handle.
    Sem
      (ScopedFile
         (Mode 'TextFormat 'ReadAccess)
         (ReadLine : ReadToEnd : Cursor)
         b
         handle
         : r)
      a)
-> Sem r a
forall k (r :: EffectRow) (es :: EffectRow) (mode :: k) b a.
(Members '[Embed IO, Resource] r, KnownList es) =>
IOMode
-> (Handle -> InterpretersFor es r)
-> (forall handle'. Sem (ScopedFile mode es b handle' : r) a)
-> Sem r a
scopedTextFileToIO IOMode
IO.ReadMode
        \Handle
h -> Handle
-> Sem
     (ReadLine : ReadToEnd : GetPosition TriPosition : Seek Ends : r) a
-> Sem (GetPosition TriPosition : Seek Ends : r) a
forall (r :: EffectRow) a.
Member (Embed IO) r =>
Handle -> Sem (ReadLine : ReadToEnd : r) a -> Sem r a
readToIO Handle
h (Sem
   (ReadLine : ReadToEnd : GetPosition TriPosition : Seek Ends : r) a
 -> Sem (GetPosition TriPosition : Seek Ends : r) a)
-> (Sem (GetPosition TriPosition : Seek Ends : r) a -> Sem r a)
-> Sem
     (ReadLine : ReadToEnd : GetPosition TriPosition : Seek Ends : r) a
-> Sem r a
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Handle -> Sem (Append Cursor r) a -> Sem r a
forall (r :: EffectRow) a.
Member (Embed IO) r =>
Handle -> Sem (Append Cursor r) a -> Sem r a
cursorToIO Handle
h

-- | An interpreter for write open mode with text.
writeAccessToIO
    ::  Members '[Embed IO, Resource] r
    =>  Access TextFormat WriteAccess (SAT.Extend ': SAT.Clear ': SAT.Cursor) r b
writeAccessToIO :: Access 'TextFormat 'WriteAccess (Extend : Clear : Cursor) r b
writeAccessToIO =
    IOMode
-> (Handle -> InterpretersFor (Extend : Clear : Cursor) r)
-> (forall handle.
    Sem
      (ScopedFile
         (Mode 'TextFormat 'WriteAccess) (Extend : Clear : Cursor) b handle
         : r)
      a)
-> Sem r a
forall k (r :: EffectRow) (es :: EffectRow) (mode :: k) b a.
(Members '[Embed IO, Resource] r, KnownList es) =>
IOMode
-> (Handle -> InterpretersFor es r)
-> (forall handle'. Sem (ScopedFile mode es b handle' : r) a)
-> Sem r a
scopedTextFileToIO IOMode
IO.WriteMode
        \Handle
h -> Handle
-> Sem (Extend : Clear : GetPosition TriPosition : Seek Ends : r) a
-> Sem (Clear : GetPosition TriPosition : Seek Ends : r) a
forall (r :: EffectRow) a.
Member (Embed IO) r =>
Handle -> Sem (Extend : r) a -> Sem r a
extendToIO Handle
h (Sem (Extend : Clear : GetPosition TriPosition : Seek Ends : r) a
 -> Sem (Clear : GetPosition TriPosition : Seek Ends : r) a)
-> (Sem (Clear : GetPosition TriPosition : Seek Ends : r) a
    -> Sem r a)
-> Sem (Extend : Clear : GetPosition TriPosition : Seek Ends : r) a
-> Sem r a
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Handle
-> Sem (Clear : GetPosition TriPosition : Seek Ends : r) a
-> Sem (GetPosition TriPosition : Seek Ends : r) a
forall (r :: EffectRow) a.
Member (Embed IO) r =>
Handle -> Sem (Clear : r) a -> Sem r a
clearToIO Handle
h (Sem (Clear : GetPosition TriPosition : Seek Ends : r) a
 -> Sem (GetPosition TriPosition : Seek Ends : r) a)
-> (Sem (GetPosition TriPosition : Seek Ends : r) a -> Sem r a)
-> Sem (Clear : GetPosition TriPosition : Seek Ends : r) a
-> Sem r a
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Handle -> Sem (Append Cursor r) a -> Sem r a
forall (r :: EffectRow) a.
Member (Embed IO) r =>
Handle -> Sem (Append Cursor r) a -> Sem r a
cursorToIO Handle
h

-- | An interpreter for read and write open mode with text.
rwAccessToIO
    ::  Members '[Embed IO, Resource] r
    =>  Access TextFormat RwAccess
            (SAT.ReadLine ': SAT.ReadToEnd ': SAT.Extend ': SAT.Clear ': SAT.Cursor) r b
rwAccessToIO :: Access
  'TextFormat
  'RwAccess
  (ReadLine : ReadToEnd : Extend : Clear : Cursor)
  r
  b
rwAccessToIO =
    IOMode
-> (Handle
    -> InterpretersFor
         (ReadLine : ReadToEnd : Extend : Clear : Cursor) r)
-> (forall handle.
    Sem
      (ScopedFile
         (Mode 'TextFormat 'RwAccess)
         (ReadLine : ReadToEnd : Extend : Clear : Cursor)
         b
         handle
         : r)
      a)
-> Sem r a
forall k (r :: EffectRow) (es :: EffectRow) (mode :: k) b a.
(Members '[Embed IO, Resource] r, KnownList es) =>
IOMode
-> (Handle -> InterpretersFor es r)
-> (forall handle'. Sem (ScopedFile mode es b handle' : r) a)
-> Sem r a
scopedTextFileToIO IOMode
IO.ReadWriteMode
        \Handle
h -> Handle
-> Sem
     (ReadLine
        : ReadToEnd : Extend : Clear : GetPosition TriPosition : Seek Ends
        : r)
     a
-> Sem (Extend : Clear : GetPosition TriPosition : Seek Ends : r) a
forall (r :: EffectRow) a.
Member (Embed IO) r =>
Handle -> Sem (ReadLine : ReadToEnd : r) a -> Sem r a
readToIO Handle
h (Sem
   (ReadLine
      : ReadToEnd : Extend : Clear : GetPosition TriPosition : Seek Ends
      : r)
   a
 -> Sem
      (Extend : Clear : GetPosition TriPosition : Seek Ends : r) a)
-> (Sem
      (Extend : Clear : GetPosition TriPosition : Seek Ends : r) a
    -> Sem r a)
-> Sem
     (ReadLine
        : ReadToEnd : Extend : Clear : GetPosition TriPosition : Seek Ends
        : r)
     a
-> Sem r a
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Handle
-> Sem (Extend : Clear : GetPosition TriPosition : Seek Ends : r) a
-> Sem (Clear : GetPosition TriPosition : Seek Ends : r) a
forall (r :: EffectRow) a.
Member (Embed IO) r =>
Handle -> Sem (Extend : r) a -> Sem r a
extendToIO Handle
h (Sem (Extend : Clear : GetPosition TriPosition : Seek Ends : r) a
 -> Sem (Clear : GetPosition TriPosition : Seek Ends : r) a)
-> (Sem (Clear : GetPosition TriPosition : Seek Ends : r) a
    -> Sem r a)
-> Sem (Extend : Clear : GetPosition TriPosition : Seek Ends : r) a
-> Sem r a
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Handle
-> Sem (Clear : GetPosition TriPosition : Seek Ends : r) a
-> Sem (GetPosition TriPosition : Seek Ends : r) a
forall (r :: EffectRow) a.
Member (Embed IO) r =>
Handle -> Sem (Clear : r) a -> Sem r a
clearToIO Handle
h (Sem (Clear : GetPosition TriPosition : Seek Ends : r) a
 -> Sem (GetPosition TriPosition : Seek Ends : r) a)
-> (Sem (GetPosition TriPosition : Seek Ends : r) a -> Sem r a)
-> Sem (Clear : GetPosition TriPosition : Seek Ends : r) a
-> Sem r a
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Handle -> Sem (Append Cursor r) a -> Sem r a
forall (r :: EffectRow) a.
Member (Embed IO) r =>
Handle -> Sem (Append Cursor r) a -> Sem r a
cursorToIO Handle
h

-- | An interpreter for append open mode with text.
appendAccessToIO
    ::  Members '[Embed IO, Resource] r
    =>  Access TextFormat AppendAccess '[SAT.Append, SAT.Clear] r b
appendAccessToIO :: Access 'TextFormat 'AppendAccess '[Append, Clear] r b
appendAccessToIO =
    IOMode
-> (Handle -> InterpretersFor '[Append, Clear] r)
-> (forall handle.
    Sem
      (ScopedFile
         (Mode 'TextFormat 'AppendAccess) '[Append, Clear] b handle
         : r)
      a)
-> Sem r a
forall k (r :: EffectRow) (es :: EffectRow) (mode :: k) b a.
(Members '[Embed IO, Resource] r, KnownList es) =>
IOMode
-> (Handle -> InterpretersFor es r)
-> (forall handle'. Sem (ScopedFile mode es b handle' : r) a)
-> Sem r a
scopedTextFileToIO IOMode
IO.AppendMode
        \Handle
h -> Handle -> Sem (Clear : r) a -> Sem r a
forall (r :: EffectRow) a.
Member (Embed IO) r =>
Handle -> Sem (Clear : r) a -> Sem r a
clearToIO Handle
h (Sem (Clear : r) a -> Sem r a)
-> (Sem (Append : Clear : r) a -> Sem (Clear : r) a)
-> Sem (Append : Clear : r) a
-> Sem r a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (rInitial :: EffectRow) x.
 Append Text (Sem rInitial) x -> Sem (Clear : r) x)
-> Sem (Append : Clear : r) a -> Sem (Clear : r) a
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall (rInitial :: EffectRow) x. e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret \(SA.Append t) -> IO () -> Sem (Clear : r) ()
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
embed (IO () -> Sem (Clear : r) ()) -> IO () -> Sem (Clear : r) ()
forall a b. (a -> b) -> a -> b
$ Handle -> Text -> IO ()
TIO.hPutStr Handle
h Text
t