{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE CPP     #-}

-- Copyright (C) 2019  Herbert Valerio Riedel
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.

module Internal
    ( module Control.Applicative
    , module Control.Monad
    , module Control.Exception
    , module Data.Word
    , unsafeIOToST, ST
    , Typeable
    , ByteString

    , LzEncoder(..)
    , LzDecoder(..)

    , intCast
    , int2cint

    , ExceptT(ExceptT), runExceptT, throwE, liftE
    ) where

import           Control.Applicative
import           Control.Exception
import           Control.Monad
import           Control.Monad.ST.Strict (ST)
import           Control.Monad.ST.Unsafe (unsafeIOToST)
import           Data.ByteString         (ByteString)
import           Data.Typeable           (Typeable)
import           Data.Word
import           Foreign
import           Foreign.C

#if defined(MIN_VERSION_int_cast)
import           Data.IntCast            (intCast)
#else
intCast :: (Integral a, Num b) => a -> b
intCast :: a -> b
intCast = a -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral
#endif

-- | Clamped conversion from 'Int' to non-negative 'CInt'
int2cint :: Int -> CInt
int2cint :: Int -> CInt
int2cint = Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CInt) -> (Int -> Int) -> Int -> CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
maxCInt (Int -> Int) -> (Int -> Int) -> Int -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Int
forall a. Ord a => a -> a -> a
max 0
  where
    maxCInt :: Int
    maxCInt :: Int
maxCInt = CInt -> Int
forall a b. (Integral a, Num b) => a -> b
intCast (CInt
forall a. Bounded a => a
maxBound :: CInt)

-- | @lzlib@ compressor handle.
newtype LzEncoder = LzEncoder (ForeignPtr LzEncoder)

-- | @lzlib@ decompressor handle.
newtype LzDecoder = LzDecoder (ForeignPtr LzDecoder)

----------------------------------------------------------------------------
-- local minimal ExceptT-like transformer
newtype ExceptT e m a = ExceptT (m (Either e a))

runExceptT :: ExceptT e m a -> m (Either e a)
runExceptT :: ExceptT e m a -> m (Either e a)
runExceptT (ExceptT m :: m (Either e a)
m) = m (Either e a)
m

throwE :: Applicative m => e -> ExceptT e m a
throwE :: e -> ExceptT e m a
throwE = m (Either e a) -> ExceptT e m a
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either e a) -> ExceptT e m a)
-> (e -> m (Either e a)) -> e -> ExceptT e m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either e a -> m (Either e a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either e a -> m (Either e a))
-> (e -> Either e a) -> e -> m (Either e a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> Either e a
forall a b. a -> Either a b
Left

liftE :: Applicative m => m a -> ExceptT e m a
liftE :: m a -> ExceptT e m a
liftE = m (Either e a) -> ExceptT e m a
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either e a) -> ExceptT e m a)
-> (m a -> m (Either e a)) -> m a -> ExceptT e m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Either e a) -> m a -> m (Either e a)
forall (f :: * -> *) a b. Applicative f => (a -> b) -> f a -> f b
liftA a -> Either e a
forall a b. b -> Either a b
Right

instance Functor m => Functor (ExceptT e m) where
    fmap :: (a -> b) -> ExceptT e m a -> ExceptT e m b
fmap f :: a -> b
f = m (Either e b) -> ExceptT e m b
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either e b) -> ExceptT e m b)
-> (ExceptT e m a -> m (Either e b))
-> ExceptT e m a
-> ExceptT e m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Either e a -> Either e b) -> m (Either e a) -> m (Either e b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> Either e a -> Either e b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) (m (Either e a) -> m (Either e b))
-> (ExceptT e m a -> m (Either e a))
-> ExceptT e m a
-> m (Either e b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExceptT e m a -> m (Either e a)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT

instance (Applicative m, Monad m) => Applicative (ExceptT e m) where
    pure :: a -> ExceptT e m a
pure = m (Either e a) -> ExceptT e m a
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either e a) -> ExceptT e m a)
-> (a -> m (Either e a)) -> a -> ExceptT e m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either e a -> m (Either e a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either e a -> m (Either e a))
-> (a -> Either e a) -> a -> m (Either e a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Either e a
forall a b. b -> Either a b
Right
    <*> :: ExceptT e m (a -> b) -> ExceptT e m a -> ExceptT e m b
(<*>) = ExceptT e m (a -> b) -> ExceptT e m a -> ExceptT e m b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
    m :: ExceptT e m a
m *> :: ExceptT e m a -> ExceptT e m b -> ExceptT e m b
*> k :: ExceptT e m b
k = ExceptT e m a
m ExceptT e m a -> (a -> ExceptT e m b) -> ExceptT e m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \_ -> ExceptT e m b
k

instance (Applicative m, Monad m) => Monad (ExceptT e m) where
    m :: ExceptT e m a
m >>= :: ExceptT e m a -> (a -> ExceptT e m b) -> ExceptT e m b
>>= k :: a -> ExceptT e m b
k = m (Either e b) -> ExceptT e m b
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT ((e -> m (Either e b))
-> (a -> m (Either e b)) -> Either e a -> m (Either e b)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either e b -> m (Either e b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either e b -> m (Either e b))
-> (e -> Either e b) -> e -> m (Either e b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> Either e b
forall a b. a -> Either a b
Left) (ExceptT e m b -> m (Either e b)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT e m b -> m (Either e b))
-> (a -> ExceptT e m b) -> a -> m (Either e b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ExceptT e m b
k) (Either e a -> m (Either e b)) -> m (Either e a) -> m (Either e b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ExceptT e m a -> m (Either e a)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT ExceptT e m a
m)
    -- legacy
    return :: a -> ExceptT e m a
return = a -> ExceptT e m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    >> :: ExceptT e m a -> ExceptT e m b -> ExceptT e m b
(>>) = ExceptT e m a -> ExceptT e m b -> ExceptT e m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)