cardano-cli
Safe HaskellNone
LanguageHaskell2010

Cardano.CLI.Types.MonadWarning

Description

This module defines the MonadWarning type class, which provides a common interface for monads that support reporting warning messages without aborting the computation (unlike with exceptions, Either, or MonadFail, which either fail or return a value).

It also includes two functions that instantiate it into either a MonadIO (runWarningIO) or a StateT monad with a [String] as state (runWarningStateT) respectively.

In the case of MonadIO, warnings are printed to stderr. In the case of StateT, with a [String] state, warnings are added to the list in the state.

By using the MonadWarning type class, users can write code that remains agnostic to the specific monad in which it operates, and to easily change it at a later stage if necessary.

Example usage:

computeWithWarning :: (MonadWarning m) => Int -> m Int
computeWithWarning x = do
  when (x < 0) $ reportIssue "Input value is negative!"
  return (x * 2)

-- Using IO monad to perform computation and report warnings.
main :: IO ()
main = do
  result <- runWarningIO $ computeWithWarning (-4)
  putStrLn $ "Result: " ++ show result
Synopsis

Documentation

class Monad m => MonadWarning (m :: Type -> Type) where Source #

Type class for monads that support reporting warnings without aborting their execution in the process.

Methods

reportIssue Source #

Arguments

:: String

The warning message to report.

-> m ()

The action that reports the warning.

Report a non-fatal issue.

Instances

Instances details
MonadIO m => MonadWarning (WarningIO m) Source #

This instance prints the issue to stderr.

Instance details

Defined in Cardano.CLI.Types.MonadWarning

Methods

reportIssue :: String -> WarningIO m () Source #

Monad m => MonadWarning (WarningStateT m) Source #

This instance adds the issue to the [String] in the state.

Instance details

Defined in Cardano.CLI.Types.MonadWarning

data WarningIO (m :: Type -> Type) a Source #

Wrapper newtype for MonadIO with MonadWarning instance. This type is not meant to be constructed directly but just to serve as an instance of MonadWarning that can be converted to MonadIO. It is only necessary in order to avoid overlapping instances.

Instances

Instances details
MonadIO m => MonadIO (WarningIO m) Source # 
Instance details

Defined in Cardano.CLI.Types.MonadWarning

Methods

liftIO :: IO a -> WarningIO m a Source #

Applicative m => Applicative (WarningIO m) Source # 
Instance details

Defined in Cardano.CLI.Types.MonadWarning

Methods

pure :: a -> WarningIO m a Source #

(<*>) :: WarningIO m (a -> b) -> WarningIO m a -> WarningIO m b Source #

liftA2 :: (a -> b -> c) -> WarningIO m a -> WarningIO m b -> WarningIO m c Source #

(*>) :: WarningIO m a -> WarningIO m b -> WarningIO m b Source #

(<*) :: WarningIO m a -> WarningIO m b -> WarningIO m a Source #

Functor m => Functor (WarningIO m) Source # 
Instance details

Defined in Cardano.CLI.Types.MonadWarning

Methods

fmap :: (a -> b) -> WarningIO m a -> WarningIO m b Source #

(<$) :: a -> WarningIO m b -> WarningIO m a Source #

Monad m => Monad (WarningIO m) Source # 
Instance details

Defined in Cardano.CLI.Types.MonadWarning

Methods

(>>=) :: WarningIO m a -> (a -> WarningIO m b) -> WarningIO m b Source #

(>>) :: WarningIO m a -> WarningIO m b -> WarningIO m b Source #

return :: a -> WarningIO m a Source #

MonadIO m => MonadWarning (WarningIO m) Source #

This instance prints the issue to stderr.

Instance details

Defined in Cardano.CLI.Types.MonadWarning

Methods

reportIssue :: String -> WarningIO m () Source #

data WarningStateT (m :: Type -> Type) a Source #

Wrapper newtype for StateT [String] with MonadWarning instance. This type is not meant to be constructed directly but just to serve as an instance of MonadWarning that can be converted to StateT. It is only necessary in order to avoid overlapping instances.

Instances

Instances details
Monad m => Applicative (WarningStateT m) Source # 
Instance details

Defined in Cardano.CLI.Types.MonadWarning

Functor m => Functor (WarningStateT m) Source # 
Instance details

Defined in Cardano.CLI.Types.MonadWarning

Methods

fmap :: (a -> b) -> WarningStateT m a -> WarningStateT m b Source #

(<$) :: a -> WarningStateT m b -> WarningStateT m a Source #

Monad m => Monad (WarningStateT m) Source # 
Instance details

Defined in Cardano.CLI.Types.MonadWarning

Monad m => MonadWarning (WarningStateT m) Source #

This instance adds the issue to the [String] in the state.

Instance details

Defined in Cardano.CLI.Types.MonadWarning

Monad m => MonadState [String] (WarningStateT m) Source # 
Instance details

Defined in Cardano.CLI.Types.MonadWarning

Methods

get :: WarningStateT m [String] Source #

put :: [String] -> WarningStateT m () Source #

state :: ([String] -> (a, [String])) -> WarningStateT m a Source #

eitherToWarning :: MonadWarning m => a -> Either String a -> m a Source #

Convert an Either into a MonadWarning. If Either is Left it returns the default value (first parameter) and reports the String as an error. If Either is Right it just returns that value.

runWarningIO :: WarningIO m a -> m a Source #

Interpret a MonadWarning as a MonadIO by reporting warnings to stderr.

runWarningStateT :: WarningStateT m a -> StateT [String] m a Source #

Interpret a MonadWarning as a StateT [String] monad, by accumulating warnings into the state.