packages feed

papa-0.7.0: src/Papa/Base/System/IO.hs

{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}

-- | Handles: opening, closing, flushing and buffering, returning their
-- 'Control.Exception.IOException' in 'IOExcept' instead of throwing it.
--
-- "System.IO"'s versions, which throw, are in "Papa.Unsafe.IO", with the
-- functions that read and write 'Data.String.String's in the locale's encoding.
module Papa.Base.System.IO (
  module P,
  openFile,
  withFile,
  hClose,
  hFlush,
  hIsEOF,
  hSetBuffering,
) where

import Control.Category ((.))
import Control.Monad ((>>=))
import Control.Monad.Except (liftEither, runExceptT)
import Data.Bool (Bool)
import Papa.Base.Control.Exception (bracket)
import Papa.Base.System.IO.Except (IOExcept, exceptIO)
import System.IO as P (
  BufferMode (BlockBuffering, LineBuffering, NoBuffering),
  FilePath,
  Handle,
  IOMode (AppendMode, ReadMode, ReadWriteMode, WriteMode),
  stderr,
  stdin,
  stdout,
 )
import qualified System.IO as IO

-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import qualified Papa.Unsafe.IO as U
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range

-- | Open a file, returning the 'Control.Exception.IOException' if it cannot
-- be opened.
--
-- ==== __Examples__
--
-- >>> either (anyOf _IOException (\i -> has _NoSuchThing (the errorType i))) (const False) <$> runExceptT (openFile "/nonexistent/papa" ReadMode)
-- True
--
-- >>> runExceptT (openFile "/dev/null" ReadMode >>= \h -> hIsEOF h <* hClose h)
-- Right True
--
-- @since 0.7.0
openFile :: FilePath -> IOMode -> IOExcept Handle
openFile p m =
  exceptIO (IO.openFile p m)
{-# INLINE openFile #-}

-- | Open a file, use its handle, and close it, whether or not the use
-- fails. The 'Control.Exception.IOException' of the open, the use or the
-- close is returned.
--
-- The handle is closed by 'bracket' over 'IO.openFile' and 'IO.hClose', not
-- by "System.IO"'s @withFile@, which rewrites the exceptions thrown by the
-- use to name the file.
--
-- ==== __Examples__
--
-- >>> runExceptT (withFile "/dev/null" ReadMode hIsEOF)
-- Right True
--
-- >>> either (anyOf _IOException (\i -> has _NoSuchThing (the errorType i))) (const False) <$> runExceptT (withFile "/nonexistent/papa" ReadMode hIsEOF)
-- True
--
-- The handle is closed when the use fails:
--
-- >>> do { h <- newIORef Nothing; _ <- runExceptT (withFile "/dev/null" ReadMode (\x -> exceptIO (writeIORef h (Just x)) *> openFile "/nonexistent/papa" ReadMode)); readIORef h >>= traverse U.hIsClosed }
-- Just True
--
-- >>> check $ withTests 20 $ property $ do m <- forAll (Gen.element [ReadMode, WriteMode, AppendMode, ReadWriteMode]); r <- evalIO (runExceptT (withFile "/dev/null" m (const (pure ())))); r === Right ()
-- ...passed 20 tests.
-- True
--
-- @since 0.7.0
withFile :: FilePath -> IOMode -> (Handle -> IOExcept a) -> IOExcept a
withFile p m use =
  exceptIO (bracket (IO.openFile p m) IO.hClose (runExceptT . use)) >>= liftEither
{-# INLINE withFile #-}

-- | Close a handle, returning the 'Control.Exception.IOException' of the
-- flush of its buffer.
--
-- ==== __Examples__
--
-- >>> runExceptT (openFile "/dev/null" ReadMode >>= hClose)
-- Right ()
--
-- Closing a closed handle is not a failure:
--
-- >>> runExceptT (openFile "/dev/null" ReadMode >>= \h -> hClose h *> hClose h)
-- Right ()
--
-- >>> check $ withTests 20 $ property $ do m <- forAll (Gen.element [ReadMode, WriteMode]); r <- evalIO (runExceptT (openFile "/dev/null" m >>= hClose)); r === Right ()
-- ...passed 20 tests.
-- True
--
-- @since 0.7.0
hClose :: Handle -> IOExcept ()
hClose =
  exceptIO . IO.hClose
{-# INLINE hClose #-}

-- | Write a handle's buffer, returning the 'Control.Exception.IOException'
-- if it cannot be written.
--
-- ==== __Examples__
--
-- >>> runExceptT (withFile "/dev/null" WriteMode hFlush)
-- Right ()
--
-- A closed handle cannot be flushed:
--
-- >>> either (has (_IOException . errorType . _IllegalOperation)) (const False) <$> runExceptT (openFile "/dev/null" WriteMode >>= \h -> hClose h *> hFlush h)
-- True
--
-- >>> check $ withTests 20 $ property $ do b <- forAll Gen.bool; r <- evalIO (runExceptT (withFile "/dev/null" WriteMode (\h -> bool (pure ()) (hClose h) b *> hFlush h))); isRight r === not b
-- ...passed 20 tests.
-- True
--
-- @since 0.7.0
hFlush :: Handle -> IOExcept ()
hFlush =
  exceptIO . IO.hFlush
{-# INLINE hFlush #-}

-- | Whether a handle is at the end of its input, returning the
-- 'Control.Exception.IOException' if the handle cannot be read.
--
-- ==== __Examples__
--
-- >>> runExceptT (withFile "/dev/null" ReadMode hIsEOF)
-- Right True
--
-- >>> either (has (_IOException . errorType . _IllegalOperation)) (const False) <$> runExceptT (withFile "/dev/null" WriteMode hIsEOF)
-- True
--
-- >>> check $ withTests 20 $ property $ do m <- forAll (Gen.element [ReadMode, WriteMode]); r <- evalIO (runExceptT (withFile "/dev/null" m hIsEOF)); isRight r === (m == ReadMode)
-- ...passed 20 tests.
-- True
--
-- @since 0.7.0
hIsEOF :: Handle -> IOExcept Bool
hIsEOF =
  exceptIO . IO.hIsEOF
{-# INLINE hIsEOF #-}

-- | Set a handle's buffering, returning the 'Control.Exception.IOException'
-- if it cannot be set.
--
-- ==== __Examples__
--
-- >>> runExceptT (withFile "/dev/null" WriteMode (\h -> hSetBuffering h NoBuffering))
-- Right ()
--
-- >>> either (has (_IOException . errorType . _IllegalOperation)) (const False) <$> runExceptT (openFile "/dev/null" WriteMode >>= \h -> hClose h *> hSetBuffering h LineBuffering)
-- True
--
-- >>> check $ withTests 20 $ property $ do b <- forAll (Gen.element [NoBuffering, LineBuffering, BlockBuffering Nothing]); r <- evalIO (runExceptT (withFile "/dev/null" WriteMode (\h -> hSetBuffering h b *> exceptIO (U.hGetBuffering h)))); r === Right b
-- ...passed 20 tests.
-- True
--
-- @since 0.7.0
hSetBuffering :: Handle -> BufferMode -> IOExcept ()
hSetBuffering h =
  exceptIO . IO.hSetBuffering h
{-# INLINE hSetBuffering #-}