Skip to content
Permalink
Browse files

Enumerate all `Severity` levels within `TextDecodingError`.

On failure to decode a `Severity` level from `Text`, enumerate all valid
`Severity` levels in the error message.
  • Loading branch information...
jonathanknowles committed Jun 17, 2019
1 parent b17bd4a commit f50ca2ad2ea48dede967759aa4de34e9889c13d2
Showing with 20 additions and 2 deletions.
  1. +1 −0 lib/cli/cardano-wallet-cli.cabal
  2. +18 −2 lib/cli/src/Cardano/CLI.hs
  3. +1 −0 nix/.stack.nix/cardano-wallet-cli.nix
@@ -32,6 +32,7 @@ library
base
, ansi-terminal
, docopt
, extra
, fmt
, iohk-monitoring
, text
@@ -1,9 +1,11 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeApplications #-}

-- |
-- Copyright: © 2018-2019 IOHK
@@ -45,6 +47,8 @@ import Control.Exception
( bracket )
import Data.Functor
( (<$) )
import Data.List.Extra
( enumerate )
import qualified Data.List.NonEmpty as NE
import Data.Text
( Text )
@@ -109,6 +113,12 @@ instance FromText (Port tag) where
instance ToText (Port tag) where
toText (Port p) = toText p

-- TODO: Remove this instance if `Severity` becomes an instance of `Bounded`:
-- See: https://github.com/input-output-hk/iohk-monitoring-framework/pull/340
instance Bounded (OptionValue Severity) where
minBound = OptionValue Debug
maxBound = OptionValue Emergency

instance FromText (OptionValue Severity) where
fromText t = case T.toLower t of
"alert" ->
@@ -129,7 +139,13 @@ instance FromText (OptionValue Severity) where
mk Warning
_ -> Left $ TextDecodingError $ show (T.unpack t)
<> " is not a recognized log severity level."
where mk = Right . OptionValue
<> " Please specify of the following values: "
<> T.unpack allValues
<> "."
where
mk = Right . OptionValue
allValues = T.intercalate ", " $ T.toLower . toText <$>
enumerate @(OptionValue Severity)

instance ToText (OptionValue Severity) where
toText = T.pack . show . getOptionValue
@@ -140,7 +156,7 @@ instance ToText (OptionValue Severity) where

-- | A wrapper to avoid orphan instances for types defined externally.
newtype OptionValue a = OptionValue { getOptionValue :: a }
deriving (Eq, Ord, Generic, Show)
deriving (Enum, Eq, Ord, Generic, Read, Show)

parseArgWith :: FromText a => Docopt -> Arguments -> Option -> IO a
parseArgWith cli args option = do

Some generated files are not rendered by default. Learn more.

0 comments on commit f50ca2a

Please sign in to comment.
You can’t perform that action at this time.