{-# LANGUAGE CPP #-}

-- | Lifted "Data.ByteString".
-- Like the original module, you probably want to import this module qualified
-- to avoid name clashes with the functions provided by "Prelude", e.g.:
--
-- > import Data.ByteString (ByteString)
-- > import Data.ByteString qualified as BS
-- > import Effectful.FileSystem.IO.ByteString qualified as EBS
--
module Effectful.FileSystem.IO.ByteString
#if MIN_VERSION_bytestring(0,11,2)
  ( -- * Introducing and eliminating ByteStrings
    fromFilePath
  , toFilePath

    -- * Files
  , readFile
#else
  ( -- * Files
    readFile
#endif
  , writeFile
  , appendFile

    -- * I/O with Handles
  , hGetLine
  , hGetContents
  , hGet
  , hGetSome
  , hGetNonBlocking
  , hPut
  , hPutNonBlocking
  , hPutStr
  , hPutStrLn
  ) where

import Data.ByteString (ByteString)
import Data.ByteString qualified as BS
import Data.ByteString.Char8 qualified as BS8
import Prelude hiding (appendFile, readFile, writeFile)
import System.IO (Handle)

import Effectful
import Effectful.Dispatch.Static
import Effectful.FileSystem

----------------------------------------
-- Introducing and eliminating ByteStrings

#if MIN_VERSION_bytestring(0,11,2)
-- | Lifted 'BS.fromFilePath'.
fromFilePath :: FileSystem :> es => FilePath -> Eff es ByteString
fromFilePath :: forall (es :: [Effect]).
(FileSystem :> es) =>
FilePath -> Eff es ByteString
fromFilePath = IO ByteString -> Eff es ByteString
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO ByteString -> Eff es ByteString)
-> (FilePath -> IO ByteString) -> FilePath -> Eff es ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO ByteString
BS.fromFilePath

-- | Lifted 'BS.toFilePath'.
toFilePath :: FileSystem :> es => ByteString -> Eff es FilePath
toFilePath :: forall (es :: [Effect]).
(FileSystem :> es) =>
ByteString -> Eff es FilePath
toFilePath = IO FilePath -> Eff es FilePath
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO FilePath -> Eff es FilePath)
-> (ByteString -> IO FilePath) -> ByteString -> Eff es FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> IO FilePath
BS.toFilePath
#endif

----------------------------------------
-- Files

-- | Lifted 'BS8.readFile'.
readFile :: FileSystem :> es => FilePath -> Eff es ByteString
readFile :: forall (es :: [Effect]).
(FileSystem :> es) =>
FilePath -> Eff es ByteString
readFile = IO ByteString -> Eff es ByteString
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO ByteString -> Eff es ByteString)
-> (FilePath -> IO ByteString) -> FilePath -> Eff es ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO ByteString
BS8.readFile

-- | Lifted 'BS8.writeFile'.
writeFile :: FileSystem :> es => FilePath -> ByteString -> Eff es ()
writeFile :: forall (es :: [Effect]).
(FileSystem :> es) =>
FilePath -> ByteString -> Eff es ()
writeFile FilePath
fp = IO () -> Eff es ()
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO () -> Eff es ())
-> (ByteString -> IO ()) -> ByteString -> Eff es ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> ByteString -> IO ()
BS8.writeFile FilePath
fp

-- | Lifted 'BS8.appendFile'.
appendFile :: FileSystem :> es => FilePath -> ByteString -> Eff es ()
appendFile :: forall (es :: [Effect]).
(FileSystem :> es) =>
FilePath -> ByteString -> Eff es ()
appendFile FilePath
fp = IO () -> Eff es ()
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO () -> Eff es ())
-> (ByteString -> IO ()) -> ByteString -> Eff es ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> ByteString -> IO ()
BS8.appendFile FilePath
fp

----------------------------------------
-- I/O with Handles

-- | Lifted 'BS8.hGetLine'.
hGetLine :: FileSystem :> es => Handle -> Eff es ByteString
hGetLine :: forall (es :: [Effect]).
(FileSystem :> es) =>
Handle -> Eff es ByteString
hGetLine = IO ByteString -> Eff es ByteString
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO ByteString -> Eff es ByteString)
-> (Handle -> IO ByteString) -> Handle -> Eff es ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> IO ByteString
BS8.hGetLine

-- | Lifted 'BS8.hGetContents'.
hGetContents :: FileSystem :> es => Handle -> Eff es ByteString
hGetContents :: forall (es :: [Effect]).
(FileSystem :> es) =>
Handle -> Eff es ByteString
hGetContents = IO ByteString -> Eff es ByteString
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO ByteString -> Eff es ByteString)
-> (Handle -> IO ByteString) -> Handle -> Eff es ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> IO ByteString
BS8.hGetContents

-- | Lifted 'BS8.hGet'.
hGet :: FileSystem :> es => Handle -> Int -> Eff es ByteString
hGet :: forall (es :: [Effect]).
(FileSystem :> es) =>
Handle -> Int -> Eff es ByteString
hGet Handle
h = IO ByteString -> Eff es ByteString
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO ByteString -> Eff es ByteString)
-> (Int -> IO ByteString) -> Int -> Eff es ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Int -> IO ByteString
BS8.hGet Handle
h

-- | Lifted 'BS8.hGetSome'.
hGetSome :: FileSystem :> es => Handle -> Int -> Eff es ByteString
hGetSome :: forall (es :: [Effect]).
(FileSystem :> es) =>
Handle -> Int -> Eff es ByteString
hGetSome Handle
h = IO ByteString -> Eff es ByteString
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO ByteString -> Eff es ByteString)
-> (Int -> IO ByteString) -> Int -> Eff es ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Int -> IO ByteString
BS8.hGetSome Handle
h

-- | Lifted 'BS8.hGetNonBlocking'.
hGetNonBlocking :: FileSystem :> es => Handle -> Int -> Eff es ByteString
hGetNonBlocking :: forall (es :: [Effect]).
(FileSystem :> es) =>
Handle -> Int -> Eff es ByteString
hGetNonBlocking Handle
h = IO ByteString -> Eff es ByteString
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO ByteString -> Eff es ByteString)
-> (Int -> IO ByteString) -> Int -> Eff es ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Int -> IO ByteString
BS8.hGetNonBlocking Handle
h

-- | Lifted 'BS8.hPut'.
hPut :: FileSystem :> es => Handle -> ByteString -> Eff es ()
hPut :: forall (es :: [Effect]).
(FileSystem :> es) =>
Handle -> ByteString -> Eff es ()
hPut Handle
h = IO () -> Eff es ()
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO () -> Eff es ())
-> (ByteString -> IO ()) -> ByteString -> Eff es ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> ByteString -> IO ()
BS8.hPut Handle
h

-- | Lifted 'BS8.hPutNonBlocking'.
hPutNonBlocking :: FileSystem :> es => Handle -> ByteString -> Eff es ByteString
hPutNonBlocking :: forall (es :: [Effect]).
(FileSystem :> es) =>
Handle -> ByteString -> Eff es ByteString
hPutNonBlocking Handle
h = IO ByteString -> Eff es ByteString
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO ByteString -> Eff es ByteString)
-> (ByteString -> IO ByteString) -> ByteString -> Eff es ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> ByteString -> IO ByteString
BS8.hPutNonBlocking Handle
h

-- | Lifted 'BS8.hPutStr'.
hPutStr :: FileSystem :> es => Handle -> ByteString -> Eff es ()
hPutStr :: forall (es :: [Effect]).
(FileSystem :> es) =>
Handle -> ByteString -> Eff es ()
hPutStr Handle
h = IO () -> Eff es ()
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO () -> Eff es ())
-> (ByteString -> IO ()) -> ByteString -> Eff es ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> ByteString -> IO ()
BS8.hPutStr Handle
h

-- | Lifted 'BS8.hPutStrLn'.
hPutStrLn :: FileSystem :> es => Handle -> ByteString -> Eff es ()
hPutStrLn :: forall (es :: [Effect]).
(FileSystem :> es) =>
Handle -> ByteString -> Eff es ()
hPutStrLn Handle
h = IO () -> Eff es ()
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO () -> Eff es ())
-> (ByteString -> IO ()) -> ByteString -> Eff es ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> ByteString -> IO ()
BS8.hPutStrLn Handle
h