-- | Provides the MonadHandleReader effect.
--
-- @since 0.1
module Effects.FileSystem.HandleReader
  ( -- * Effect
    MonadHandleReader (..),

    -- * UTF-8 Utils

    -- ** GetLine
    hGetLineUtf8,
    hGetLineUtf8Lenient,
    hGetLineUtf8ThrowM,

    -- ** GetContents
    hGetContentsUtf8,
    hGetContentsUtf8Lenient,
    hGetContentsUtf8ThrowM,

    -- ** Get
    hGetUtf8,
    hGetUtf8Lenient,
    hGetUtf8ThrowM,

    -- ** GetSome
    hGetSomeUtf8,
    hGetSomeUtf8Lenient,
    hGetSomeUtf8ThrowM,

    -- ** GetNonBlocking
    hGetNonBlockingUtf8,
    hGetNonBlockingUtf8Lenient,
    hGetNonBlockingUtf8ThrowM,

    -- * Reexports
    ByteString,
    Handle,
    Text,
    UnicodeException,
  )
where

import Control.Monad ((>=>))
import Control.Monad.Catch (MonadThrow)
import Control.Monad.Trans.Class (MonadTrans (lift))
import Control.Monad.Trans.Reader (ReaderT)
import Data.ByteString (ByteString)
import Data.ByteString.Char8 qualified as C8
import Data.Text (Text)
import Data.Text.Encoding.Error (UnicodeException)
import FileSystem.UTF8 qualified as FS.UTF8
import GHC.Stack (HasCallStack)
import System.IO (BufferMode, Handle)
import System.IO qualified as IO

-- | Represents handle reader effects.
--
-- @since 0.1
class (Monad m) => MonadHandleReader m where
  -- | Lifted 'IO.hIsEOF'.
  --
  -- @since 0.1
  hIsEOF :: (HasCallStack) => Handle -> m Bool

  -- | Lifted 'IO.hGetBuffering'.
  --
  -- @since 0.1
  hGetBuffering :: (HasCallStack) => Handle -> m BufferMode

  -- | Lifted 'IO.hIsOpen'.
  --
  -- @since 0.1
  hIsOpen :: (HasCallStack) => Handle -> m Bool

  -- | Lifted 'IO.hIsClosed'.
  --
  -- @since 0.1
  hIsClosed :: (HasCallStack) => Handle -> m Bool

  -- | Lifted 'IO.hIsReadable'.
  --
  -- @since 0.1
  hIsReadable :: (HasCallStack) => Handle -> m Bool

  -- | Lifted 'IO.hIsWritable'.
  --
  -- @since 0.1
  hIsWritable :: (HasCallStack) => Handle -> m Bool

  -- | Lifted 'IO.hIsSeekable'.
  --
  -- @since 0.1
  hIsSeekable :: (HasCallStack) => Handle -> m Bool

  -- | Lifted 'IO.hIsTerminalDevice'.
  --
  -- @since 0.1
  hIsTerminalDevice :: (HasCallStack) => Handle -> m Bool

  -- | Lifted 'IO.hGetEcho'.
  --
  -- @since 0.1
  hGetEcho :: (HasCallStack) => Handle -> m Bool

  -- | Lifted 'IO.hWaitForInput'.
  --
  -- @since 0.1
  hWaitForInput :: (HasCallStack) => Handle -> Int -> m Bool

  -- | Lifted 'IO.hReady'.
  --
  -- @since 0.1
  hReady :: (HasCallStack) => Handle -> m Bool

  -- | Lifted 'IO.hGetChar'.
  --
  -- @since 0.1
  hGetChar :: (HasCallStack) => Handle -> m Char

  -- | Lifted 'C8.hGetLine'.
  --
  -- @since 0.1
  hGetLine :: (HasCallStack) => Handle -> m ByteString

  -- | Lifted 'C8.hGetContents'.
  --
  -- @since 0.1
  hGetContents :: (HasCallStack) => Handle -> m ByteString

  -- | Lifted 'C8.hGet'.
  --
  -- @since 0.1
  hGet :: (HasCallStack) => Handle -> Int -> m ByteString

  -- | Lifted 'C8.hGetSome'.
  --
  -- @since 0.1
  hGetSome :: (HasCallStack) => Handle -> Int -> m ByteString

  -- | Lifted 'C8.hGetNonBlocking'.
  --
  -- @since 0.1
  hGetNonBlocking :: (HasCallStack) => Handle -> Int -> m ByteString

-- | @since 0.1
instance MonadHandleReader IO where
  hIsEOF :: HasCallStack => Handle -> IO Bool
hIsEOF = Handle -> IO Bool
IO.hIsEOF
  {-# INLINEABLE hIsEOF #-}
  hGetBuffering :: HasCallStack => Handle -> IO BufferMode
hGetBuffering = Handle -> IO BufferMode
IO.hGetBuffering
  {-# INLINEABLE hGetBuffering #-}
  hIsOpen :: HasCallStack => Handle -> IO Bool
hIsOpen = Handle -> IO Bool
IO.hIsOpen
  {-# INLINEABLE hIsOpen #-}
  hIsClosed :: HasCallStack => Handle -> IO Bool
hIsClosed = Handle -> IO Bool
IO.hIsClosed
  {-# INLINEABLE hIsClosed #-}
  hIsReadable :: HasCallStack => Handle -> IO Bool
hIsReadable = Handle -> IO Bool
IO.hIsReadable
  {-# INLINEABLE hIsReadable #-}
  hIsWritable :: HasCallStack => Handle -> IO Bool
hIsWritable = Handle -> IO Bool
IO.hIsWritable
  {-# INLINEABLE hIsWritable #-}
  hIsSeekable :: HasCallStack => Handle -> IO Bool
hIsSeekable = Handle -> IO Bool
IO.hIsSeekable
  {-# INLINEABLE hIsSeekable #-}
  hIsTerminalDevice :: HasCallStack => Handle -> IO Bool
hIsTerminalDevice = Handle -> IO Bool
IO.hIsTerminalDevice
  {-# INLINEABLE hIsTerminalDevice #-}
  hGetEcho :: HasCallStack => Handle -> IO Bool
hGetEcho = Handle -> IO Bool
IO.hGetEcho
  {-# INLINEABLE hGetEcho #-}
  hWaitForInput :: HasCallStack => Handle -> Int -> IO Bool
hWaitForInput = Handle -> Int -> IO Bool
IO.hWaitForInput
  {-# INLINEABLE hWaitForInput #-}
  hReady :: HasCallStack => Handle -> IO Bool
hReady = Handle -> IO Bool
IO.hReady
  {-# INLINEABLE hReady #-}
  hGetChar :: HasCallStack => Handle -> IO Char
hGetChar = Handle -> IO Char
IO.hGetChar
  {-# INLINEABLE hGetChar #-}
  hGetLine :: HasCallStack => Handle -> IO ByteString
hGetLine = Handle -> IO ByteString
C8.hGetLine
  {-# INLINEABLE hGetLine #-}
  hGetContents :: HasCallStack => Handle -> IO ByteString
hGetContents = Handle -> IO ByteString
C8.hGetContents
  {-# INLINEABLE hGetContents #-}
  hGet :: HasCallStack => Handle -> Int -> IO ByteString
hGet = Handle -> Int -> IO ByteString
C8.hGet
  {-# INLINEABLE hGet #-}
  hGetSome :: HasCallStack => Handle -> Int -> IO ByteString
hGetSome = Handle -> Int -> IO ByteString
C8.hGetSome
  {-# INLINEABLE hGetSome #-}
  hGetNonBlocking :: HasCallStack => Handle -> Int -> IO ByteString
hGetNonBlocking = Handle -> Int -> IO ByteString
C8.hGetNonBlocking
  {-# INLINEABLE hGetNonBlocking #-}

-- | @since 0.1
instance (MonadHandleReader m) => MonadHandleReader (ReaderT e m) where
  hIsEOF :: HasCallStack => Handle -> ReaderT e m Bool
hIsEOF = m Bool -> ReaderT e m Bool
forall (m :: * -> *) a. Monad m => m a -> ReaderT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m Bool -> ReaderT e m Bool)
-> (Handle -> m Bool) -> Handle -> ReaderT e m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m Bool
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m Bool
hIsEOF
  {-# INLINEABLE hIsEOF #-}
  hGetBuffering :: HasCallStack => Handle -> ReaderT e m BufferMode
hGetBuffering = m BufferMode -> ReaderT e m BufferMode
forall (m :: * -> *) a. Monad m => m a -> ReaderT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m BufferMode -> ReaderT e m BufferMode)
-> (Handle -> m BufferMode) -> Handle -> ReaderT e m BufferMode
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m BufferMode
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m BufferMode
hGetBuffering
  {-# INLINEABLE hGetBuffering #-}
  hIsOpen :: HasCallStack => Handle -> ReaderT e m Bool
hIsOpen = m Bool -> ReaderT e m Bool
forall (m :: * -> *) a. Monad m => m a -> ReaderT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m Bool -> ReaderT e m Bool)
-> (Handle -> m Bool) -> Handle -> ReaderT e m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m Bool
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m Bool
hIsOpen
  {-# INLINEABLE hIsOpen #-}
  hIsClosed :: HasCallStack => Handle -> ReaderT e m Bool
hIsClosed = m Bool -> ReaderT e m Bool
forall (m :: * -> *) a. Monad m => m a -> ReaderT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m Bool -> ReaderT e m Bool)
-> (Handle -> m Bool) -> Handle -> ReaderT e m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m Bool
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m Bool
hIsClosed
  {-# INLINEABLE hIsClosed #-}
  hIsReadable :: HasCallStack => Handle -> ReaderT e m Bool
hIsReadable = m Bool -> ReaderT e m Bool
forall (m :: * -> *) a. Monad m => m a -> ReaderT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m Bool -> ReaderT e m Bool)
-> (Handle -> m Bool) -> Handle -> ReaderT e m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m Bool
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m Bool
hIsReadable
  {-# INLINEABLE hIsReadable #-}
  hIsWritable :: HasCallStack => Handle -> ReaderT e m Bool
hIsWritable = m Bool -> ReaderT e m Bool
forall (m :: * -> *) a. Monad m => m a -> ReaderT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m Bool -> ReaderT e m Bool)
-> (Handle -> m Bool) -> Handle -> ReaderT e m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m Bool
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m Bool
hIsWritable
  {-# INLINEABLE hIsWritable #-}
  hIsSeekable :: HasCallStack => Handle -> ReaderT e m Bool
hIsSeekable = m Bool -> ReaderT e m Bool
forall (m :: * -> *) a. Monad m => m a -> ReaderT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m Bool -> ReaderT e m Bool)
-> (Handle -> m Bool) -> Handle -> ReaderT e m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m Bool
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m Bool
hIsSeekable
  {-# INLINEABLE hIsSeekable #-}
  hIsTerminalDevice :: HasCallStack => Handle -> ReaderT e m Bool
hIsTerminalDevice = m Bool -> ReaderT e m Bool
forall (m :: * -> *) a. Monad m => m a -> ReaderT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m Bool -> ReaderT e m Bool)
-> (Handle -> m Bool) -> Handle -> ReaderT e m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m Bool
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m Bool
hIsTerminalDevice
  {-# INLINEABLE hIsTerminalDevice #-}
  hGetEcho :: HasCallStack => Handle -> ReaderT e m Bool
hGetEcho = m Bool -> ReaderT e m Bool
forall (m :: * -> *) a. Monad m => m a -> ReaderT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m Bool -> ReaderT e m Bool)
-> (Handle -> m Bool) -> Handle -> ReaderT e m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m Bool
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m Bool
hGetEcho
  {-# INLINEABLE hGetEcho #-}
  hWaitForInput :: HasCallStack => Handle -> Int -> ReaderT e m Bool
hWaitForInput Handle
h = m Bool -> ReaderT e m Bool
forall (m :: * -> *) a. Monad m => m a -> ReaderT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m Bool -> ReaderT e m Bool)
-> (Int -> m Bool) -> Int -> ReaderT e m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Int -> m Bool
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> Int -> m Bool
hWaitForInput Handle
h
  {-# INLINEABLE hWaitForInput #-}
  hReady :: HasCallStack => Handle -> ReaderT e m Bool
hReady = m Bool -> ReaderT e m Bool
forall (m :: * -> *) a. Monad m => m a -> ReaderT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m Bool -> ReaderT e m Bool)
-> (Handle -> m Bool) -> Handle -> ReaderT e m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m Bool
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m Bool
hReady
  {-# INLINEABLE hReady #-}
  hGetChar :: HasCallStack => Handle -> ReaderT e m Char
hGetChar = m Char -> ReaderT e m Char
forall (m :: * -> *) a. Monad m => m a -> ReaderT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m Char -> ReaderT e m Char)
-> (Handle -> m Char) -> Handle -> ReaderT e m Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m Char
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m Char
hGetChar
  {-# INLINEABLE hGetChar #-}
  hGetLine :: HasCallStack => Handle -> ReaderT e m ByteString
hGetLine = m ByteString -> ReaderT e m ByteString
forall (m :: * -> *) a. Monad m => m a -> ReaderT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m ByteString -> ReaderT e m ByteString)
-> (Handle -> m ByteString) -> Handle -> ReaderT e m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m ByteString
hGetLine
  {-# INLINEABLE hGetLine #-}
  hGetContents :: HasCallStack => Handle -> ReaderT e m ByteString
hGetContents = m ByteString -> ReaderT e m ByteString
forall (m :: * -> *) a. Monad m => m a -> ReaderT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m ByteString -> ReaderT e m ByteString)
-> (Handle -> m ByteString) -> Handle -> ReaderT e m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m ByteString
hGetContents
  {-# INLINEABLE hGetContents #-}
  hGet :: HasCallStack => Handle -> Int -> ReaderT e m ByteString
hGet Handle
h = m ByteString -> ReaderT e m ByteString
forall (m :: * -> *) a. Monad m => m a -> ReaderT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m ByteString -> ReaderT e m ByteString)
-> (Int -> m ByteString) -> Int -> ReaderT e m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Int -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> Int -> m ByteString
hGet Handle
h
  {-# INLINEABLE hGet #-}
  hGetSome :: HasCallStack => Handle -> Int -> ReaderT e m ByteString
hGetSome Handle
h = m ByteString -> ReaderT e m ByteString
forall (m :: * -> *) a. Monad m => m a -> ReaderT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m ByteString -> ReaderT e m ByteString)
-> (Int -> m ByteString) -> Int -> ReaderT e m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Int -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> Int -> m ByteString
hGetSome Handle
h
  {-# INLINEABLE hGetSome #-}
  hGetNonBlocking :: HasCallStack => Handle -> Int -> ReaderT e m ByteString
hGetNonBlocking Handle
h = m ByteString -> ReaderT e m ByteString
forall (m :: * -> *) a. Monad m => m a -> ReaderT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m ByteString -> ReaderT e m ByteString)
-> (Int -> m ByteString) -> Int -> ReaderT e m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Int -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> Int -> m ByteString
hGetNonBlocking Handle
h
  {-# INLINEABLE hGetNonBlocking #-}

-- | 'hGetLine' that attempts a UTF-8 conversion.
--
-- @since 0.1
hGetLineUtf8 ::
  ( HasCallStack,
    MonadHandleReader m
  ) =>
  Handle ->
  m (Either UnicodeException Text)
hGetLineUtf8 :: forall (m :: * -> *).
(HasCallStack, MonadHandleReader m) =>
Handle -> m (Either UnicodeException Text)
hGetLineUtf8 = (ByteString -> Either UnicodeException Text)
-> m ByteString -> m (Either UnicodeException Text)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Either UnicodeException Text
FS.UTF8.decodeUtf8 (m ByteString -> m (Either UnicodeException Text))
-> (Handle -> m ByteString)
-> Handle
-> m (Either UnicodeException Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m ByteString
hGetLine
{-# INLINEABLE hGetLineUtf8 #-}

-- | 'hGetLine' that converts to UTF-8 in lenient mode.
--
-- @since 0.1
hGetLineUtf8Lenient :: (HasCallStack, MonadHandleReader m) => Handle -> m Text
hGetLineUtf8Lenient :: forall (m :: * -> *).
(HasCallStack, MonadHandleReader m) =>
Handle -> m Text
hGetLineUtf8Lenient = (ByteString -> Text) -> m ByteString -> m Text
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Text
FS.UTF8.decodeUtf8Lenient (m ByteString -> m Text)
-> (Handle -> m ByteString) -> Handle -> m Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m ByteString
hGetLine
{-# INLINEABLE hGetLineUtf8Lenient #-}

-- | 'hGetLine' that throws 'UnicodeException' if UTF-8 conversion fails.
--
-- @since 0.1
hGetLineUtf8ThrowM ::
  ( HasCallStack,
    MonadHandleReader m,
    MonadThrow m
  ) =>
  Handle ->
  m Text
hGetLineUtf8ThrowM :: forall (m :: * -> *).
(HasCallStack, MonadHandleReader m, MonadThrow m) =>
Handle -> m Text
hGetLineUtf8ThrowM = Handle -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m ByteString
hGetLine (Handle -> m ByteString)
-> (ByteString -> m Text) -> Handle -> m Text
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> ByteString -> m Text
forall (m :: * -> *).
(HasCallStack, MonadThrow m) =>
ByteString -> m Text
FS.UTF8.decodeUtf8ThrowM
{-# INLINEABLE hGetLineUtf8ThrowM #-}

-- | 'hGetContents' that attempts a UTF-8 conversion.
--
-- @since 0.1
hGetContentsUtf8 ::
  ( HasCallStack,
    MonadHandleReader m
  ) =>
  Handle ->
  m (Either UnicodeException Text)
hGetContentsUtf8 :: forall (m :: * -> *).
(HasCallStack, MonadHandleReader m) =>
Handle -> m (Either UnicodeException Text)
hGetContentsUtf8 = (ByteString -> Either UnicodeException Text)
-> m ByteString -> m (Either UnicodeException Text)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Either UnicodeException Text
FS.UTF8.decodeUtf8 (m ByteString -> m (Either UnicodeException Text))
-> (Handle -> m ByteString)
-> Handle
-> m (Either UnicodeException Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m ByteString
hGetContents
{-# INLINEABLE hGetContentsUtf8 #-}

-- | 'hGetContents' that converts to UTF-8 in lenient mode.
--
-- @since 0.1
hGetContentsUtf8Lenient ::
  ( HasCallStack,
    MonadHandleReader m
  ) =>
  Handle ->
  m Text
hGetContentsUtf8Lenient :: forall (m :: * -> *).
(HasCallStack, MonadHandleReader m) =>
Handle -> m Text
hGetContentsUtf8Lenient = (ByteString -> Text) -> m ByteString -> m Text
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Text
FS.UTF8.decodeUtf8Lenient (m ByteString -> m Text)
-> (Handle -> m ByteString) -> Handle -> m Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m ByteString
hGetContents
{-# INLINEABLE hGetContentsUtf8Lenient #-}

-- | 'hGetContents' that throws 'UnicodeException' if UTF-8 conversion fails.
--
-- @since 0.1
hGetContentsUtf8ThrowM ::
  ( HasCallStack,
    MonadHandleReader m,
    MonadThrow m
  ) =>
  Handle ->
  m Text
hGetContentsUtf8ThrowM :: forall (m :: * -> *).
(HasCallStack, MonadHandleReader m, MonadThrow m) =>
Handle -> m Text
hGetContentsUtf8ThrowM = Handle -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> m ByteString
hGetContents (Handle -> m ByteString)
-> (ByteString -> m Text) -> Handle -> m Text
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> ByteString -> m Text
forall (m :: * -> *).
(HasCallStack, MonadThrow m) =>
ByteString -> m Text
FS.UTF8.decodeUtf8ThrowM
{-# INLINEABLE hGetContentsUtf8ThrowM #-}

-- | 'hGet' that attempts a UTF-8 conversion.
--
-- @since 0.1
hGetUtf8 ::
  ( HasCallStack,
    MonadHandleReader m
  ) =>
  Handle ->
  Int ->
  m (Either UnicodeException Text)
hGetUtf8 :: forall (m :: * -> *).
(HasCallStack, MonadHandleReader m) =>
Handle -> Int -> m (Either UnicodeException Text)
hGetUtf8 Handle
h = (ByteString -> Either UnicodeException Text)
-> m ByteString -> m (Either UnicodeException Text)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Either UnicodeException Text
FS.UTF8.decodeUtf8 (m ByteString -> m (Either UnicodeException Text))
-> (Int -> m ByteString) -> Int -> m (Either UnicodeException Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Int -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> Int -> m ByteString
hGet Handle
h
{-# INLINEABLE hGetUtf8 #-}

-- | 'hGet' that converts to UTF-8 in lenient mode.
--
-- @since 0.1
hGetUtf8Lenient ::
  ( HasCallStack,
    MonadHandleReader m
  ) =>
  Handle ->
  Int ->
  m Text
hGetUtf8Lenient :: forall (m :: * -> *).
(HasCallStack, MonadHandleReader m) =>
Handle -> Int -> m Text
hGetUtf8Lenient Handle
h = (ByteString -> Text) -> m ByteString -> m Text
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Text
FS.UTF8.decodeUtf8Lenient (m ByteString -> m Text) -> (Int -> m ByteString) -> Int -> m Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Int -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> Int -> m ByteString
hGet Handle
h
{-# INLINEABLE hGetUtf8Lenient #-}

-- | 'hGet' that throws 'UnicodeException' if UTF-8 conversion fails.
--
-- @since 0.1
hGetUtf8ThrowM ::
  ( HasCallStack,
    MonadHandleReader m,
    MonadThrow m
  ) =>
  Handle ->
  Int ->
  m Text
hGetUtf8ThrowM :: forall (m :: * -> *).
(HasCallStack, MonadHandleReader m, MonadThrow m) =>
Handle -> Int -> m Text
hGetUtf8ThrowM Handle
h = Handle -> Int -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> Int -> m ByteString
hGet Handle
h (Int -> m ByteString) -> (ByteString -> m Text) -> Int -> m Text
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> ByteString -> m Text
forall (m :: * -> *).
(HasCallStack, MonadThrow m) =>
ByteString -> m Text
FS.UTF8.decodeUtf8ThrowM
{-# INLINEABLE hGetUtf8ThrowM #-}

-- | 'hGetSome' that attempts a UTF-8 conversion.
--
-- @since 0.1
hGetSomeUtf8 ::
  ( HasCallStack,
    MonadHandleReader m
  ) =>
  Handle ->
  Int ->
  m (Either UnicodeException Text)
hGetSomeUtf8 :: forall (m :: * -> *).
(HasCallStack, MonadHandleReader m) =>
Handle -> Int -> m (Either UnicodeException Text)
hGetSomeUtf8 Handle
h = (ByteString -> Either UnicodeException Text)
-> m ByteString -> m (Either UnicodeException Text)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Either UnicodeException Text
FS.UTF8.decodeUtf8 (m ByteString -> m (Either UnicodeException Text))
-> (Int -> m ByteString) -> Int -> m (Either UnicodeException Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Int -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> Int -> m ByteString
hGetSome Handle
h
{-# INLINEABLE hGetSomeUtf8 #-}

-- | 'hGetSome' that converts to UTF-8 in lenient mode.
--
-- @since 0.1
hGetSomeUtf8Lenient ::
  ( HasCallStack,
    MonadHandleReader m
  ) =>
  Handle ->
  Int ->
  m Text
hGetSomeUtf8Lenient :: forall (m :: * -> *).
(HasCallStack, MonadHandleReader m) =>
Handle -> Int -> m Text
hGetSomeUtf8Lenient Handle
h = (ByteString -> Text) -> m ByteString -> m Text
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Text
FS.UTF8.decodeUtf8Lenient (m ByteString -> m Text) -> (Int -> m ByteString) -> Int -> m Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Int -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> Int -> m ByteString
hGetSome Handle
h
{-# INLINEABLE hGetSomeUtf8Lenient #-}

-- | 'hGetSome' that throws 'UnicodeException' if UTF-8 conversion fails.
--
-- @since 0.1
hGetSomeUtf8ThrowM ::
  ( HasCallStack,
    MonadHandleReader m,
    MonadThrow m
  ) =>
  Handle ->
  Int ->
  m Text
hGetSomeUtf8ThrowM :: forall (m :: * -> *).
(HasCallStack, MonadHandleReader m, MonadThrow m) =>
Handle -> Int -> m Text
hGetSomeUtf8ThrowM Handle
h = Handle -> Int -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> Int -> m ByteString
hGetSome Handle
h (Int -> m ByteString) -> (ByteString -> m Text) -> Int -> m Text
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> ByteString -> m Text
forall (m :: * -> *).
(HasCallStack, MonadThrow m) =>
ByteString -> m Text
FS.UTF8.decodeUtf8ThrowM
{-# INLINEABLE hGetSomeUtf8ThrowM #-}

-- | 'hGetNonBlocking' that attempts a UTF-8 conversion.
--
-- @since 0.1
hGetNonBlockingUtf8 ::
  ( HasCallStack,
    MonadHandleReader m
  ) =>
  Handle ->
  Int ->
  m (Either UnicodeException Text)
hGetNonBlockingUtf8 :: forall (m :: * -> *).
(HasCallStack, MonadHandleReader m) =>
Handle -> Int -> m (Either UnicodeException Text)
hGetNonBlockingUtf8 Handle
h = (ByteString -> Either UnicodeException Text)
-> m ByteString -> m (Either UnicodeException Text)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Either UnicodeException Text
FS.UTF8.decodeUtf8 (m ByteString -> m (Either UnicodeException Text))
-> (Int -> m ByteString) -> Int -> m (Either UnicodeException Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Int -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> Int -> m ByteString
hGetNonBlocking Handle
h
{-# INLINEABLE hGetNonBlockingUtf8 #-}

-- | 'hGetNonBlocking' that converts to UTF-8 in lenient mode.
--
-- @since 0.1
hGetNonBlockingUtf8Lenient ::
  ( HasCallStack,
    MonadHandleReader m
  ) =>
  Handle ->
  Int ->
  m Text
hGetNonBlockingUtf8Lenient :: forall (m :: * -> *).
(HasCallStack, MonadHandleReader m) =>
Handle -> Int -> m Text
hGetNonBlockingUtf8Lenient Handle
h = (ByteString -> Text) -> m ByteString -> m Text
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Text
FS.UTF8.decodeUtf8Lenient (m ByteString -> m Text) -> (Int -> m ByteString) -> Int -> m Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Int -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> Int -> m ByteString
hGetNonBlocking Handle
h
{-# INLINEABLE hGetNonBlockingUtf8Lenient #-}

-- | 'hGetNonBlocking' that throws 'UnicodeException' if UTF-8 conversion fails.
--
-- @since 0.1
hGetNonBlockingUtf8ThrowM ::
  ( HasCallStack,
    MonadHandleReader m,
    MonadThrow m
  ) =>
  Handle ->
  Int ->
  m Text
hGetNonBlockingUtf8ThrowM :: forall (m :: * -> *).
(HasCallStack, MonadHandleReader m, MonadThrow m) =>
Handle -> Int -> m Text
hGetNonBlockingUtf8ThrowM Handle
h = Handle -> Int -> m ByteString
forall (m :: * -> *).
(MonadHandleReader m, HasCallStack) =>
Handle -> Int -> m ByteString
hGetNonBlocking Handle
h (Int -> m ByteString) -> (ByteString -> m Text) -> Int -> m Text
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> ByteString -> m Text
forall (m :: * -> *).
(HasCallStack, MonadThrow m) =>
ByteString -> m Text
FS.UTF8.decodeUtf8ThrowM
{-# INLINEABLE hGetNonBlockingUtf8ThrowM #-}