{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
module Distribution.Simple.Flag (
Flag(..),
allFlags,
toFlag,
fromFlag,
fromFlagOrDefault,
flagToMaybe,
flagToList,
maybeToFlag,
BooleanFlag(..) ) where
import Prelude ()
import Distribution.Compat.Prelude hiding (get)
import Distribution.Compat.Stack
data Flag a = Flag a | NoFlag deriving (Flag a -> Flag a -> Bool
(Flag a -> Flag a -> Bool)
-> (Flag a -> Flag a -> Bool) -> Eq (Flag a)
forall a. Eq a => Flag a -> Flag a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Flag a -> Flag a -> Bool
$c/= :: forall a. Eq a => Flag a -> Flag a -> Bool
== :: Flag a -> Flag a -> Bool
$c== :: forall a. Eq a => Flag a -> Flag a -> Bool
Eq, (forall x. Flag a -> Rep (Flag a) x)
-> (forall x. Rep (Flag a) x -> Flag a) -> Generic (Flag a)
forall x. Rep (Flag a) x -> Flag a
forall x. Flag a -> Rep (Flag a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Flag a) x -> Flag a
forall a x. Flag a -> Rep (Flag a) x
$cto :: forall a x. Rep (Flag a) x -> Flag a
$cfrom :: forall a x. Flag a -> Rep (Flag a) x
Generic, Int -> Flag a -> ShowS
[Flag a] -> ShowS
Flag a -> String
(Int -> Flag a -> ShowS)
-> (Flag a -> String) -> ([Flag a] -> ShowS) -> Show (Flag a)
forall a. Show a => Int -> Flag a -> ShowS
forall a. Show a => [Flag a] -> ShowS
forall a. Show a => Flag a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Flag a] -> ShowS
$cshowList :: forall a. Show a => [Flag a] -> ShowS
show :: Flag a -> String
$cshow :: forall a. Show a => Flag a -> String
showsPrec :: Int -> Flag a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Flag a -> ShowS
Show, ReadPrec [Flag a]
ReadPrec (Flag a)
Int -> ReadS (Flag a)
ReadS [Flag a]
(Int -> ReadS (Flag a))
-> ReadS [Flag a]
-> ReadPrec (Flag a)
-> ReadPrec [Flag a]
-> Read (Flag a)
forall a. Read a => ReadPrec [Flag a]
forall a. Read a => ReadPrec (Flag a)
forall a. Read a => Int -> ReadS (Flag a)
forall a. Read a => ReadS [Flag a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Flag a]
$creadListPrec :: forall a. Read a => ReadPrec [Flag a]
readPrec :: ReadPrec (Flag a)
$creadPrec :: forall a. Read a => ReadPrec (Flag a)
readList :: ReadS [Flag a]
$creadList :: forall a. Read a => ReadS [Flag a]
readsPrec :: Int -> ReadS (Flag a)
$creadsPrec :: forall a. Read a => Int -> ReadS (Flag a)
Read)
instance Binary a => Binary (Flag a)
instance Functor Flag where
fmap :: (a -> b) -> Flag a -> Flag b
fmap a -> b
f (Flag a
x) = b -> Flag b
forall a. a -> Flag a
Flag (a -> b
f a
x)
fmap a -> b
_ Flag a
NoFlag = Flag b
forall a. Flag a
NoFlag
instance Applicative Flag where
(Flag a -> b
x) <*> :: Flag (a -> b) -> Flag a -> Flag b
<*> Flag a
y = a -> b
x (a -> b) -> Flag a -> Flag b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Flag a
y
Flag (a -> b)
NoFlag <*> Flag a
_ = Flag b
forall a. Flag a
NoFlag
pure :: a -> Flag a
pure = a -> Flag a
forall a. a -> Flag a
Flag
instance Monoid (Flag a) where
mempty :: Flag a
mempty = Flag a
forall a. Flag a
NoFlag
mappend :: Flag a -> Flag a -> Flag a
mappend = Flag a -> Flag a -> Flag a
forall a. Semigroup a => a -> a -> a
(<>)
instance Semigroup (Flag a) where
Flag a
_ <> :: Flag a -> Flag a -> Flag a
<> f :: Flag a
f@(Flag a
_) = Flag a
f
Flag a
f <> Flag a
NoFlag = Flag a
f
instance Bounded a => Bounded (Flag a) where
minBound :: Flag a
minBound = a -> Flag a
forall a. a -> Flag a
toFlag a
forall a. Bounded a => a
minBound
maxBound :: Flag a
maxBound = a -> Flag a
forall a. a -> Flag a
toFlag a
forall a. Bounded a => a
maxBound
instance Enum a => Enum (Flag a) where
fromEnum :: Flag a -> Int
fromEnum = a -> Int
forall a. Enum a => a -> Int
fromEnum (a -> Int) -> (Flag a -> a) -> Flag a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Flag a -> a
forall a. WithCallStack (Flag a -> a)
fromFlag
toEnum :: Int -> Flag a
toEnum = a -> Flag a
forall a. a -> Flag a
toFlag (a -> Flag a) -> (Int -> a) -> Int -> Flag a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a
forall a. Enum a => Int -> a
toEnum
enumFrom :: Flag a -> [Flag a]
enumFrom (Flag a
a) = (a -> Flag a) -> [a] -> [Flag a]
forall a b. (a -> b) -> [a] -> [b]
map a -> Flag a
forall a. a -> Flag a
toFlag ([a] -> [Flag a]) -> (a -> [a]) -> a -> [Flag a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
forall a. Enum a => a -> [a]
enumFrom (a -> [Flag a]) -> a -> [Flag a]
forall a b. (a -> b) -> a -> b
$ a
a
enumFrom Flag a
_ = []
enumFromThen :: Flag a -> Flag a -> [Flag a]
enumFromThen (Flag a
a) (Flag a
b) = a -> Flag a
forall a. a -> Flag a
toFlag (a -> Flag a) -> [a] -> [Flag a]
forall a b. (a -> b) -> [a] -> [b]
`map` a -> a -> [a]
forall a. Enum a => a -> a -> [a]
enumFromThen a
a a
b
enumFromThen Flag a
_ Flag a
_ = []
enumFromTo :: Flag a -> Flag a -> [Flag a]
enumFromTo (Flag a
a) (Flag a
b) = a -> Flag a
forall a. a -> Flag a
toFlag (a -> Flag a) -> [a] -> [Flag a]
forall a b. (a -> b) -> [a] -> [b]
`map` a -> a -> [a]
forall a. Enum a => a -> a -> [a]
enumFromTo a
a a
b
enumFromTo Flag a
_ Flag a
_ = []
enumFromThenTo :: Flag a -> Flag a -> Flag a -> [Flag a]
enumFromThenTo (Flag a
a) (Flag a
b) (Flag a
c) = a -> Flag a
forall a. a -> Flag a
toFlag (a -> Flag a) -> [a] -> [Flag a]
forall a b. (a -> b) -> [a] -> [b]
`map` a -> a -> a -> [a]
forall a. Enum a => a -> a -> a -> [a]
enumFromThenTo a
a a
b a
c
enumFromThenTo Flag a
_ Flag a
_ Flag a
_ = []
toFlag :: a -> Flag a
toFlag :: a -> Flag a
toFlag = a -> Flag a
forall a. a -> Flag a
Flag
fromFlag :: WithCallStack (Flag a -> a)
fromFlag :: Flag a -> a
fromFlag (Flag a
x) = a
x
fromFlag Flag a
NoFlag = String -> a
forall a. HasCallStack => String -> a
error String
"fromFlag NoFlag. Use fromFlagOrDefault"
fromFlagOrDefault :: a -> Flag a -> a
fromFlagOrDefault :: a -> Flag a -> a
fromFlagOrDefault a
_ (Flag a
x) = a
x
fromFlagOrDefault a
def Flag a
NoFlag = a
def
flagToMaybe :: Flag a -> Maybe a
flagToMaybe :: Flag a -> Maybe a
flagToMaybe (Flag a
x) = a -> Maybe a
forall a. a -> Maybe a
Just a
x
flagToMaybe Flag a
NoFlag = Maybe a
forall a. Maybe a
Nothing
flagToList :: Flag a -> [a]
flagToList :: Flag a -> [a]
flagToList (Flag a
x) = [a
x]
flagToList Flag a
NoFlag = []
allFlags :: [Flag Bool] -> Flag Bool
allFlags :: [Flag Bool] -> Flag Bool
allFlags [Flag Bool]
flags = if (Flag Bool -> Bool) -> [Flag Bool] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\Flag Bool
f -> Bool -> Flag Bool -> Bool
forall a. a -> Flag a -> a
fromFlagOrDefault Bool
False Flag Bool
f) [Flag Bool]
flags
then Bool -> Flag Bool
forall a. a -> Flag a
Flag Bool
True
else Flag Bool
forall a. Flag a
NoFlag
maybeToFlag :: Maybe a -> Flag a
maybeToFlag :: Maybe a -> Flag a
maybeToFlag Maybe a
Nothing = Flag a
forall a. Flag a
NoFlag
maybeToFlag (Just a
x) = a -> Flag a
forall a. a -> Flag a
Flag a
x
class BooleanFlag a where
asBool :: a -> Bool
instance BooleanFlag Bool where
asBool :: Bool -> Bool
asBool = Bool -> Bool
forall a. a -> a
id