-- |
-- License: GPL-3.0-or-later
-- Copyright: Oleg Grenrus
module CabalFmt.Options (
    Mode (..),
    Options (..),
    defaultOptions,
    OptionsMorphism, mkOptionsMorphism, runOptionsMorphism,
    HasOptions(..),
    ) where

import Distribution.Compat.Lens (LensLike')

import qualified Distribution.CabalSpecVersion       as C

data Mode
    = ModeStdout
    | ModeInplace
    | ModeCheck
  deriving (Mode -> Mode -> Bool
(Mode -> Mode -> Bool) -> (Mode -> Mode -> Bool) -> Eq Mode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Mode -> Mode -> Bool
$c/= :: Mode -> Mode -> Bool
== :: Mode -> Mode -> Bool
$c== :: Mode -> Mode -> Bool
Eq, Int -> Mode -> ShowS
[Mode] -> ShowS
Mode -> String
(Int -> Mode -> ShowS)
-> (Mode -> String) -> ([Mode] -> ShowS) -> Show Mode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Mode] -> ShowS
$cshowList :: [Mode] -> ShowS
show :: Mode -> String
$cshow :: Mode -> String
showsPrec :: Int -> Mode -> ShowS
$cshowsPrec :: Int -> Mode -> ShowS
Show)

data Options = Options
    { Options -> Bool
optError       :: !Bool
    , Options -> Int
optIndent      :: !Int
    , Options -> Bool
optTabular     :: !Bool
    , Options -> Bool
optCabalFile   :: !Bool
    , Options -> CabalSpecVersion
optSpecVersion :: !C.CabalSpecVersion
    , Options -> Mode
optMode        :: !Mode
    }
  deriving Int -> Options -> ShowS
[Options] -> ShowS
Options -> String
(Int -> Options -> ShowS)
-> (Options -> String) -> ([Options] -> ShowS) -> Show Options
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Options] -> ShowS
$cshowList :: [Options] -> ShowS
show :: Options -> String
$cshow :: Options -> String
showsPrec :: Int -> Options -> ShowS
$cshowsPrec :: Int -> Options -> ShowS
Show

defaultOptions :: Options
defaultOptions :: Options
defaultOptions = Options :: Bool -> Int -> Bool -> Bool -> CabalSpecVersion -> Mode -> Options
Options
    { optError :: Bool
optError       = Bool
False
    , optIndent :: Int
optIndent      = Int
2
    , optTabular :: Bool
optTabular     = Bool
True
    , optCabalFile :: Bool
optCabalFile   = Bool
True
    , optSpecVersion :: CabalSpecVersion
optSpecVersion = CabalSpecVersion
C.cabalSpecLatest
    , optMode :: Mode
optMode        = Mode
ModeStdout
    }

newtype OptionsMorphism = OM (Options -> Options)

runOptionsMorphism :: OptionsMorphism -> Options -> Options
runOptionsMorphism :: OptionsMorphism -> Options -> Options
runOptionsMorphism (OM Options -> Options
f) = Options -> Options
f

mkOptionsMorphism :: (Options -> Options) -> OptionsMorphism
mkOptionsMorphism :: (Options -> Options) -> OptionsMorphism
mkOptionsMorphism = (Options -> Options) -> OptionsMorphism
OM

instance Semigroup OptionsMorphism where
    OM Options -> Options
f <> :: OptionsMorphism -> OptionsMorphism -> OptionsMorphism
<> OM Options -> Options
g = (Options -> Options) -> OptionsMorphism
OM (Options -> Options
g (Options -> Options) -> (Options -> Options) -> Options -> Options
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Options -> Options
f)

instance Monoid OptionsMorphism where
    mempty :: OptionsMorphism
mempty  = (Options -> Options) -> OptionsMorphism
OM Options -> Options
forall a. a -> a
id
    mappend :: OptionsMorphism -> OptionsMorphism -> OptionsMorphism
mappend = OptionsMorphism -> OptionsMorphism -> OptionsMorphism
forall a. Semigroup a => a -> a -> a
(<>)

class HasOptions e where
    options :: Functor f => LensLike' f e Options

instance HasOptions Options where
    options :: LensLike' f Options Options
options = LensLike' f Options Options
forall a. a -> a
id