{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_HADDOCK prune not-home #-}

{- |
Module      : Test.KeyedVals.CheckHandle
Copyright   : (c) 2022 Tim Emiola
Maintainer  : Tim Emiola <adetokunbo@emio.la>
SPDX-License-Identifier: BSD3
-}
module Test.KeyedVals.Prelude (
  -- * functions
  orThrowHandleErr,
  throwHandleErr,
  withGlobOf,

  -- * module re-eports
  module Control.Monad,
  module Control.Exception,
  module Data.ByteString,
  module Data.List.NonEmpty,
  module Numeric.Natural,
  module KeyedVals.Handle,
  module Test.Hspec,
  module Test.Hspec.Benri,
  module Test.KeyedVals.Types,
) where

import Control.Exception (throwIO)
import Control.Monad (void)
import Data.ByteString (ByteString)
import Data.List.NonEmpty (NonEmpty (..))
import KeyedVals.Handle (Glob, Handle, HandleErr, Key, close, mkGlob)
import Numeric.Natural (Natural)
import Test.Hspec
import Test.Hspec.Benri
import Test.KeyedVals.Types


withGlobOf :: ByteString -> (Glob -> IO a) -> IO a
withGlobOf :: forall a. ByteString -> (Glob -> IO a) -> IO a
withGlobOf ByteString
x Glob -> IO a
action = do
  case ByteString -> Maybe Glob
mkGlob ByteString
x of
    Maybe Glob
Nothing -> forall e a. Exception e => e -> IO a
throwIO forall a b. (a -> b) -> a -> b
$ String -> IOError
userError String
"bad test pattern"
    Just Glob
g -> Glob -> IO a
action Glob
g


throwHandleErr :: HandleErr -> IO ()
throwHandleErr :: HandleErr -> IO ()
throwHandleErr = forall e a. Exception e => e -> IO a
throwIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IOError
userError forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show


orThrowHandleErr :: IO (Either HandleErr ()) -> IO ()
orThrowHandleErr :: IO (Either HandleErr ()) -> IO ()
orThrowHandleErr IO (Either HandleErr ())
action = IO (Either HandleErr ())
action forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either HandleErr -> IO ()
throwHandleErr forall (f :: * -> *) a. Applicative f => a -> f a
pure