{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.Simple.Flag
-- Copyright   :  Isaac Jones 2003-2004
--                Duncan Coutts 2007
-- License     :  BSD3
--
-- Maintainer  :  cabal-devel@haskell.org
-- Portability :  portable
--
-- Defines the 'Flag' type and it's 'Monoid' instance,  see
-- <http://www.haskell.org/pipermail/cabal-devel/2007-December/001509.html>
-- for an explanation.
--
-- Split off from "Distribution.Simple.Setup" to break import cycles.
module Distribution.Simple.Flag (
  Flag(..),
  allFlags,
  toFlag,
  fromFlag,
  fromFlagOrDefault,
  flagElim,
  flagToMaybe,
  flagToList,
  maybeToFlag,
  BooleanFlag(..) ) where

import Prelude ()
import Distribution.Compat.Prelude hiding (get)
import Distribution.Compat.Stack

-- ------------------------------------------------------------
-- * Flag type
-- ------------------------------------------------------------

-- | All flags are monoids, they come in two flavours:
--
-- 1. list flags eg
--
-- > --ghc-option=foo --ghc-option=bar
--
-- gives us all the values ["foo", "bar"]
--
-- 2. singular value flags, eg:
--
-- > --enable-foo --disable-foo
--
-- gives us Just False
-- So this Flag type is for the latter singular kind of flag.
-- Its monoid instance gives us the behaviour where it starts out as
-- 'NoFlag' and later flags override earlier ones.
--
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
$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
/= :: 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
$cfrom :: forall a x. Flag a -> Rep (Flag a) x
from :: forall x. Flag a -> Rep (Flag a) x
$cto :: forall a x. Rep (Flag a) x -> Flag a
to :: forall x. Rep (Flag a) x -> Flag a
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
$cshowsPrec :: forall a. Show a => Int -> Flag a -> ShowS
showsPrec :: Int -> Flag a -> ShowS
$cshow :: forall a. Show a => Flag a -> String
show :: Flag a -> String
$cshowList :: forall a. Show a => [Flag a] -> ShowS
showList :: [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
$creadsPrec :: forall a. Read a => Int -> ReadS (Flag a)
readsPrec :: Int -> ReadS (Flag a)
$creadList :: forall a. Read a => ReadS [Flag a]
readList :: ReadS [Flag a]
$creadPrec :: forall a. Read a => ReadPrec (Flag a)
readPrec :: ReadPrec (Flag a)
$creadListPrec :: forall a. Read a => ReadPrec [Flag a]
readListPrec :: ReadPrec [Flag a]
Read, Typeable)

instance Binary a => Binary (Flag a)
instance Structured a => Structured (Flag a)

instance Functor Flag where
  fmap :: forall a b. (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) <*> :: forall a b. 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 :: forall a. 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 :: forall a. a -> Flag a
toFlag = a -> Flag a
forall a. a -> Flag a
Flag

fromFlag :: WithCallStack (Flag a -> a)
fromFlag :: forall a. WithCallStack (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 :: forall a. 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 :: forall a. 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

-- | @since 3.4.0.0
flagElim :: b -> (a -> b) -> Flag a -> b
flagElim :: forall b a. b -> (a -> b) -> Flag a -> b
flagElim b
n a -> b
_ Flag a
NoFlag   = b
n
flagElim b
_ a -> b
f (Flag a
x) = a -> b
f a
x

flagToList :: Flag a -> [a]
flagToList :: forall a. 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 :: forall a. 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

-- | Types that represent boolean flags.
class BooleanFlag a where
    asBool :: a -> Bool

instance BooleanFlag Bool where
  asBool :: Bool -> Bool
asBool = Bool -> Bool
forall a. a -> a
id