{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE InstanceSigs #-}
module Cardano.CLI.Types.MonadWarning
( MonadWarning (..)
, WarningIO
, WarningStateT
, eitherToWarning
, runWarningIO
, runWarningStateT
)
where
import Control.Monad.IO.Class (MonadIO, liftIO)
import Control.Monad.State (MonadState (..))
import Control.Monad.Trans.State (StateT)
import System.IO (hPutStrLn, stderr)
class Monad m => MonadWarning m where
reportIssue
:: String
-> m ()
newtype WarningIO m a = WarningIO
{ forall (m :: * -> *) a. WarningIO m a -> m a
runWarningIO :: m a
}
deriving ((forall a b. (a -> b) -> WarningIO m a -> WarningIO m b)
-> (forall a b. a -> WarningIO m b -> WarningIO m a)
-> Functor (WarningIO m)
forall a b. a -> WarningIO m b -> WarningIO m a
forall a b. (a -> b) -> WarningIO m a -> WarningIO m b
forall (m :: * -> *) a b.
Functor m =>
a -> WarningIO m b -> WarningIO m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> WarningIO m a -> WarningIO m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> WarningIO m a -> WarningIO m b
fmap :: forall a b. (a -> b) -> WarningIO m a -> WarningIO m b
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> WarningIO m b -> WarningIO m a
<$ :: forall a b. a -> WarningIO m b -> WarningIO m a
Functor, Functor (WarningIO m)
Functor (WarningIO m) =>
(forall a. a -> WarningIO m a)
-> (forall a b.
WarningIO m (a -> b) -> WarningIO m a -> WarningIO m b)
-> (forall a b c.
(a -> b -> c) -> WarningIO m a -> WarningIO m b -> WarningIO m c)
-> (forall a b. WarningIO m a -> WarningIO m b -> WarningIO m b)
-> (forall a b. WarningIO m a -> WarningIO m b -> WarningIO m a)
-> Applicative (WarningIO m)
forall a. a -> WarningIO m a
forall a b. WarningIO m a -> WarningIO m b -> WarningIO m a
forall a b. WarningIO m a -> WarningIO m b -> WarningIO m b
forall a b. WarningIO m (a -> b) -> WarningIO m a -> WarningIO m b
forall a b c.
(a -> b -> c) -> WarningIO m a -> WarningIO m b -> WarningIO m 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
forall (m :: * -> *). Applicative m => Functor (WarningIO m)
forall (m :: * -> *) a. Applicative m => a -> WarningIO m a
forall (m :: * -> *) a b.
Applicative m =>
WarningIO m a -> WarningIO m b -> WarningIO m a
forall (m :: * -> *) a b.
Applicative m =>
WarningIO m a -> WarningIO m b -> WarningIO m b
forall (m :: * -> *) a b.
Applicative m =>
WarningIO m (a -> b) -> WarningIO m a -> WarningIO m b
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> WarningIO m a -> WarningIO m b -> WarningIO m c
$cpure :: forall (m :: * -> *) a. Applicative m => a -> WarningIO m a
pure :: forall a. a -> WarningIO m a
$c<*> :: forall (m :: * -> *) a b.
Applicative m =>
WarningIO m (a -> b) -> WarningIO m a -> WarningIO m b
<*> :: forall a b. WarningIO m (a -> b) -> WarningIO m a -> WarningIO m b
$cliftA2 :: forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> WarningIO m a -> WarningIO m b -> WarningIO m c
liftA2 :: forall a b c.
(a -> b -> c) -> WarningIO m a -> WarningIO m b -> WarningIO m c
$c*> :: forall (m :: * -> *) a b.
Applicative m =>
WarningIO m a -> WarningIO m b -> WarningIO m b
*> :: forall a b. WarningIO m a -> WarningIO m b -> WarningIO m b
$c<* :: forall (m :: * -> *) a b.
Applicative m =>
WarningIO m a -> WarningIO m b -> WarningIO m a
<* :: forall a b. WarningIO m a -> WarningIO m b -> WarningIO m a
Applicative, Applicative (WarningIO m)
Applicative (WarningIO m) =>
(forall a b.
WarningIO m a -> (a -> WarningIO m b) -> WarningIO m b)
-> (forall a b. WarningIO m a -> WarningIO m b -> WarningIO m b)
-> (forall a. a -> WarningIO m a)
-> Monad (WarningIO m)
forall a. a -> WarningIO m a
forall a b. WarningIO m a -> WarningIO m b -> WarningIO m b
forall a b. WarningIO m a -> (a -> WarningIO m b) -> WarningIO m b
forall (m :: * -> *). Monad m => Applicative (WarningIO m)
forall (m :: * -> *) a. Monad m => a -> WarningIO m a
forall (m :: * -> *) a b.
Monad m =>
WarningIO m a -> WarningIO m b -> WarningIO m b
forall (m :: * -> *) a b.
Monad m =>
WarningIO m a -> (a -> WarningIO m b) -> WarningIO m 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
$c>>= :: forall (m :: * -> *) a b.
Monad m =>
WarningIO m a -> (a -> WarningIO m b) -> WarningIO m b
>>= :: forall a b. WarningIO m a -> (a -> WarningIO m b) -> WarningIO m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
WarningIO m a -> WarningIO m b -> WarningIO m b
>> :: forall a b. WarningIO m a -> WarningIO m b -> WarningIO m b
$creturn :: forall (m :: * -> *) a. Monad m => a -> WarningIO m a
return :: forall a. a -> WarningIO m a
Monad, Monad (WarningIO m)
Monad (WarningIO m) =>
(forall a. IO a -> WarningIO m a) -> MonadIO (WarningIO m)
forall a. IO a -> WarningIO m a
forall (m :: * -> *).
Monad m =>
(forall a. IO a -> m a) -> MonadIO m
forall (m :: * -> *). MonadIO m => Monad (WarningIO m)
forall (m :: * -> *) a. MonadIO m => IO a -> WarningIO m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> WarningIO m a
liftIO :: forall a. IO a -> WarningIO m a
MonadIO)
instance MonadIO m => MonadWarning (WarningIO m) where
reportIssue :: String -> WarningIO m ()
reportIssue :: String -> WarningIO m ()
reportIssue String
issue = IO () -> WarningIO m ()
forall a. IO a -> WarningIO m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> String -> IO ()
hPutStrLn Handle
stderr String
issue)
newtype WarningStateT m a = WarningStateT
{ forall (m :: * -> *) a. WarningStateT m a -> StateT [String] m a
runWarningStateT :: StateT [String] m a
}
deriving ((forall a b. (a -> b) -> WarningStateT m a -> WarningStateT m b)
-> (forall a b. a -> WarningStateT m b -> WarningStateT m a)
-> Functor (WarningStateT m)
forall a b. a -> WarningStateT m b -> WarningStateT m a
forall a b. (a -> b) -> WarningStateT m a -> WarningStateT m b
forall (m :: * -> *) a b.
Functor m =>
a -> WarningStateT m b -> WarningStateT m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> WarningStateT m a -> WarningStateT m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> WarningStateT m a -> WarningStateT m b
fmap :: forall a b. (a -> b) -> WarningStateT m a -> WarningStateT m b
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> WarningStateT m b -> WarningStateT m a
<$ :: forall a b. a -> WarningStateT m b -> WarningStateT m a
Functor, Functor (WarningStateT m)
Functor (WarningStateT m) =>
(forall a. a -> WarningStateT m a)
-> (forall a b.
WarningStateT m (a -> b) -> WarningStateT m a -> WarningStateT m b)
-> (forall a b c.
(a -> b -> c)
-> WarningStateT m a -> WarningStateT m b -> WarningStateT m c)
-> (forall a b.
WarningStateT m a -> WarningStateT m b -> WarningStateT m b)
-> (forall a b.
WarningStateT m a -> WarningStateT m b -> WarningStateT m a)
-> Applicative (WarningStateT m)
forall a. a -> WarningStateT m a
forall a b.
WarningStateT m a -> WarningStateT m b -> WarningStateT m a
forall a b.
WarningStateT m a -> WarningStateT m b -> WarningStateT m b
forall a b.
WarningStateT m (a -> b) -> WarningStateT m a -> WarningStateT m b
forall a b c.
(a -> b -> c)
-> WarningStateT m a -> WarningStateT m b -> WarningStateT m c
forall (m :: * -> *). Monad m => Functor (WarningStateT m)
forall (m :: * -> *) a. Monad m => a -> WarningStateT m a
forall (m :: * -> *) a b.
Monad m =>
WarningStateT m a -> WarningStateT m b -> WarningStateT m a
forall (m :: * -> *) a b.
Monad m =>
WarningStateT m a -> WarningStateT m b -> WarningStateT m b
forall (m :: * -> *) a b.
Monad m =>
WarningStateT m (a -> b) -> WarningStateT m a -> WarningStateT m b
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c)
-> WarningStateT m a -> WarningStateT m b -> WarningStateT m 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
$cpure :: forall (m :: * -> *) a. Monad m => a -> WarningStateT m a
pure :: forall a. a -> WarningStateT m a
$c<*> :: forall (m :: * -> *) a b.
Monad m =>
WarningStateT m (a -> b) -> WarningStateT m a -> WarningStateT m b
<*> :: forall a b.
WarningStateT m (a -> b) -> WarningStateT m a -> WarningStateT m b
$cliftA2 :: forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c)
-> WarningStateT m a -> WarningStateT m b -> WarningStateT m c
liftA2 :: forall a b c.
(a -> b -> c)
-> WarningStateT m a -> WarningStateT m b -> WarningStateT m c
$c*> :: forall (m :: * -> *) a b.
Monad m =>
WarningStateT m a -> WarningStateT m b -> WarningStateT m b
*> :: forall a b.
WarningStateT m a -> WarningStateT m b -> WarningStateT m b
$c<* :: forall (m :: * -> *) a b.
Monad m =>
WarningStateT m a -> WarningStateT m b -> WarningStateT m a
<* :: forall a b.
WarningStateT m a -> WarningStateT m b -> WarningStateT m a
Applicative, Applicative (WarningStateT m)
Applicative (WarningStateT m) =>
(forall a b.
WarningStateT m a -> (a -> WarningStateT m b) -> WarningStateT m b)
-> (forall a b.
WarningStateT m a -> WarningStateT m b -> WarningStateT m b)
-> (forall a. a -> WarningStateT m a)
-> Monad (WarningStateT m)
forall a. a -> WarningStateT m a
forall a b.
WarningStateT m a -> WarningStateT m b -> WarningStateT m b
forall a b.
WarningStateT m a -> (a -> WarningStateT m b) -> WarningStateT m b
forall (m :: * -> *). Monad m => Applicative (WarningStateT m)
forall (m :: * -> *) a. Monad m => a -> WarningStateT m a
forall (m :: * -> *) a b.
Monad m =>
WarningStateT m a -> WarningStateT m b -> WarningStateT m b
forall (m :: * -> *) a b.
Monad m =>
WarningStateT m a -> (a -> WarningStateT m b) -> WarningStateT m 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
$c>>= :: forall (m :: * -> *) a b.
Monad m =>
WarningStateT m a -> (a -> WarningStateT m b) -> WarningStateT m b
>>= :: forall a b.
WarningStateT m a -> (a -> WarningStateT m b) -> WarningStateT m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
WarningStateT m a -> WarningStateT m b -> WarningStateT m b
>> :: forall a b.
WarningStateT m a -> WarningStateT m b -> WarningStateT m b
$creturn :: forall (m :: * -> *) a. Monad m => a -> WarningStateT m a
return :: forall a. a -> WarningStateT m a
Monad, MonadState [String])
instance Monad m => MonadWarning (WarningStateT m) where
reportIssue :: String -> WarningStateT m ()
reportIssue :: String -> WarningStateT m ()
reportIssue String
issue = ([String] -> ((), [String])) -> WarningStateT m ()
forall a. ([String] -> (a, [String])) -> WarningStateT m a
forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state (\[String]
x -> ((), String
issue String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
x))
eitherToWarning :: MonadWarning m => a -> Either String a -> m a
eitherToWarning :: forall (m :: * -> *) a.
MonadWarning m =>
a -> Either String a -> m a
eitherToWarning a
def = (String -> m a) -> (a -> m a) -> Either String a -> m a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\String
issue -> do String -> m ()
forall (m :: * -> *). MonadWarning m => String -> m ()
reportIssue String
issue; a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
def) a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return