{-|
Description : Common Types
Copyright   : Profpatsch, 2018
License     : GPL-3
Stability   : experimental
-}
{-# LANGUAGE GeneralizedNewtypeDeriving, DeriveFunctor #-}
module Foreign.Nix.Shellout.Types where

import Control.Error
import Control.Monad.IO.Class (MonadIO)
import Data.Text (Text)
import Data.Bifunctor (Bifunctor (bimap))

-- | Calls a command that returns an error and the whole stderr on failure.
newtype NixAction e a = NixAction
  { NixAction e a -> ExceptT (NixActionError e) IO a
unNixAction :: ExceptT (NixActionError e) IO a }
  deriving (a -> NixAction e b -> NixAction e a
(a -> b) -> NixAction e a -> NixAction e b
(forall a b. (a -> b) -> NixAction e a -> NixAction e b)
-> (forall a b. a -> NixAction e b -> NixAction e a)
-> Functor (NixAction e)
forall a b. a -> NixAction e b -> NixAction e a
forall a b. (a -> b) -> NixAction e a -> NixAction e b
forall e a b. a -> NixAction e b -> NixAction e a
forall e a b. (a -> b) -> NixAction e a -> NixAction e b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> NixAction e b -> NixAction e a
$c<$ :: forall e a b. a -> NixAction e b -> NixAction e a
fmap :: (a -> b) -> NixAction e a -> NixAction e b
$cfmap :: forall e a b. (a -> b) -> NixAction e a -> NixAction e b
Functor, Functor (NixAction e)
a -> NixAction e a
Functor (NixAction e)
-> (forall a. a -> NixAction e a)
-> (forall a b.
    NixAction e (a -> b) -> NixAction e a -> NixAction e b)
-> (forall a b c.
    (a -> b -> c) -> NixAction e a -> NixAction e b -> NixAction e c)
-> (forall a b. NixAction e a -> NixAction e b -> NixAction e b)
-> (forall a b. NixAction e a -> NixAction e b -> NixAction e a)
-> Applicative (NixAction e)
NixAction e a -> NixAction e b -> NixAction e b
NixAction e a -> NixAction e b -> NixAction e a
NixAction e (a -> b) -> NixAction e a -> NixAction e b
(a -> b -> c) -> NixAction e a -> NixAction e b -> NixAction e c
forall e. Functor (NixAction e)
forall a. a -> NixAction e a
forall e a. a -> NixAction e a
forall a b. NixAction e a -> NixAction e b -> NixAction e a
forall a b. NixAction e a -> NixAction e b -> NixAction e b
forall a b. NixAction e (a -> b) -> NixAction e a -> NixAction e b
forall e a b. NixAction e a -> NixAction e b -> NixAction e a
forall e a b. NixAction e a -> NixAction e b -> NixAction e b
forall e a b.
NixAction e (a -> b) -> NixAction e a -> NixAction e b
forall a b c.
(a -> b -> c) -> NixAction e a -> NixAction e b -> NixAction e c
forall e a b c.
(a -> b -> c) -> NixAction e a -> NixAction e b -> NixAction e c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: NixAction e a -> NixAction e b -> NixAction e a
$c<* :: forall e a b. NixAction e a -> NixAction e b -> NixAction e a
*> :: NixAction e a -> NixAction e b -> NixAction e b
$c*> :: forall e a b. NixAction e a -> NixAction e b -> NixAction e b
liftA2 :: (a -> b -> c) -> NixAction e a -> NixAction e b -> NixAction e c
$cliftA2 :: forall e a b c.
(a -> b -> c) -> NixAction e a -> NixAction e b -> NixAction e c
<*> :: NixAction e (a -> b) -> NixAction e a -> NixAction e b
$c<*> :: forall e a b.
NixAction e (a -> b) -> NixAction e a -> NixAction e b
pure :: a -> NixAction e a
$cpure :: forall e a. a -> NixAction e a
$cp1Applicative :: forall e. Functor (NixAction e)
Applicative, Applicative (NixAction e)
a -> NixAction e a
Applicative (NixAction e)
-> (forall a b.
    NixAction e a -> (a -> NixAction e b) -> NixAction e b)
-> (forall a b. NixAction e a -> NixAction e b -> NixAction e b)
-> (forall a. a -> NixAction e a)
-> Monad (NixAction e)
NixAction e a -> (a -> NixAction e b) -> NixAction e b
NixAction e a -> NixAction e b -> NixAction e b
forall e. Applicative (NixAction e)
forall a. a -> NixAction e a
forall e a. a -> NixAction e a
forall a b. NixAction e a -> NixAction e b -> NixAction e b
forall a b. NixAction e a -> (a -> NixAction e b) -> NixAction e b
forall e a b. NixAction e a -> NixAction e b -> NixAction e b
forall e a b.
NixAction e a -> (a -> NixAction e b) -> NixAction e b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: a -> NixAction e a
$creturn :: forall e a. a -> NixAction e a
>> :: NixAction e a -> NixAction e b -> NixAction e b
$c>> :: forall e a b. NixAction e a -> NixAction e b -> NixAction e b
>>= :: NixAction e a -> (a -> NixAction e b) -> NixAction e b
$c>>= :: forall e a b.
NixAction e a -> (a -> NixAction e b) -> NixAction e b
$cp1Monad :: forall e. Applicative (NixAction e)
Monad, Monad (NixAction e)
Monad (NixAction e)
-> (forall a. IO a -> NixAction e a) -> MonadIO (NixAction e)
IO a -> NixAction e a
forall e. Monad (NixAction e)
forall a. IO a -> NixAction e a
forall e a. IO a -> NixAction e a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
liftIO :: IO a -> NixAction e a
$cliftIO :: forall e a. IO a -> NixAction e a
$cp1MonadIO :: forall e. Monad (NixAction e)
MonadIO)

-- | Combines the standard error of running a command with a more semantic
-- error type one should match on first.
data NixActionError e = NixActionError
  { NixActionError e -> Text
actionStderr :: Text
  , NixActionError e -> e
actionError :: e }
  deriving (Int -> NixActionError e -> ShowS
[NixActionError e] -> ShowS
NixActionError e -> String
(Int -> NixActionError e -> ShowS)
-> (NixActionError e -> String)
-> ([NixActionError e] -> ShowS)
-> Show (NixActionError e)
forall e. Show e => Int -> NixActionError e -> ShowS
forall e. Show e => [NixActionError e] -> ShowS
forall e. Show e => NixActionError e -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NixActionError e] -> ShowS
$cshowList :: forall e. Show e => [NixActionError e] -> ShowS
show :: NixActionError e -> String
$cshow :: forall e. Show e => NixActionError e -> String
showsPrec :: Int -> NixActionError e -> ShowS
$cshowsPrec :: forall e. Show e => Int -> NixActionError e -> ShowS
Show, a -> NixActionError b -> NixActionError a
(a -> b) -> NixActionError a -> NixActionError b
(forall a b. (a -> b) -> NixActionError a -> NixActionError b)
-> (forall a b. a -> NixActionError b -> NixActionError a)
-> Functor NixActionError
forall a b. a -> NixActionError b -> NixActionError a
forall a b. (a -> b) -> NixActionError a -> NixActionError b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> NixActionError b -> NixActionError a
$c<$ :: forall a b. a -> NixActionError b -> NixActionError a
fmap :: (a -> b) -> NixActionError a -> NixActionError b
$cfmap :: forall a b. (a -> b) -> NixActionError a -> NixActionError b
Functor)

-- | Run a 'NixAction' without having to go through 'ExceptT' first.
runNixAction :: NixAction e a
             -> IO (Either (NixActionError e) a)
runNixAction :: NixAction e a -> IO (Either (NixActionError e) a)
runNixAction = ExceptT (NixActionError e) IO a -> IO (Either (NixActionError e) a)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT (NixActionError e) IO a
 -> IO (Either (NixActionError e) a))
-> (NixAction e a -> ExceptT (NixActionError e) IO a)
-> NixAction e a
-> IO (Either (NixActionError e) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixAction e a -> ExceptT (NixActionError e) IO a
forall e a. NixAction e a -> ExceptT (NixActionError e) IO a
unNixAction

instance Bifunctor NixAction where
  bimap :: (a -> b) -> (c -> d) -> NixAction a c -> NixAction b d
bimap a -> b
f c -> d
g = ExceptT (NixActionError b) IO d -> NixAction b d
forall e a. ExceptT (NixActionError e) IO a -> NixAction e a
NixAction (ExceptT (NixActionError b) IO d -> NixAction b d)
-> (NixAction a c -> ExceptT (NixActionError b) IO d)
-> NixAction a c
-> NixAction b d
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NixActionError a -> NixActionError b)
-> (c -> d)
-> ExceptT (NixActionError a) IO c
-> ExceptT (NixActionError b) IO d
forall (m :: * -> *) e f a b.
Functor m =>
(e -> f) -> (a -> b) -> ExceptT e m a -> ExceptT f m b
bimapExceptT ((a -> b) -> NixActionError a -> NixActionError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) c -> d
g (ExceptT (NixActionError a) IO c
 -> ExceptT (NixActionError b) IO d)
-> (NixAction a c -> ExceptT (NixActionError a) IO c)
-> NixAction a c
-> ExceptT (NixActionError b) IO d
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NixAction a c -> ExceptT (NixActionError a) IO c
forall e a. NixAction e a -> ExceptT (NixActionError e) IO a
unNixAction

-- | A path in the nix store. It carries a phantom @a@ to differentiate
-- between 'Derivation' files and 'Realized' paths.
newtype StorePath a = StorePath
  { StorePath a -> String
unStorePath :: FilePath }
  deriving (StorePath a -> StorePath a -> Bool
(StorePath a -> StorePath a -> Bool)
-> (StorePath a -> StorePath a -> Bool) -> Eq (StorePath a)
forall a. StorePath a -> StorePath a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StorePath a -> StorePath a -> Bool
$c/= :: forall a. StorePath a -> StorePath a -> Bool
== :: StorePath a -> StorePath a -> Bool
$c== :: forall a. StorePath a -> StorePath a -> Bool
Eq, Int -> StorePath a -> ShowS
[StorePath a] -> ShowS
StorePath a -> String
(Int -> StorePath a -> ShowS)
-> (StorePath a -> String)
-> ([StorePath a] -> ShowS)
-> Show (StorePath a)
forall a. Int -> StorePath a -> ShowS
forall a. [StorePath a] -> ShowS
forall a. StorePath a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StorePath a] -> ShowS
$cshowList :: forall a. [StorePath a] -> ShowS
show :: StorePath a -> String
$cshow :: forall a. StorePath a -> String
showsPrec :: Int -> StorePath a -> ShowS
$cshowsPrec :: forall a. Int -> StorePath a -> ShowS
Show)

-- | A nix derivation is a complete build instruction that can be realized.
data Derivation
-- | Once a derivation is realized, the finished output can be used.
data Realized