{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}

module Stack.Types.GhcOptionKey
  ( GhcOptionKey (..)
  ) where

import           Data.Aeson.Types ( FromJSONKey (..), FromJSONKeyFunction (..) )
import qualified Data.Text as T
import           Stack.Prelude

data GhcOptionKey
  = GOKOldEverything
  | GOKEverything
  | GOKLocals
  | GOKTargets
  | GOKPackage !PackageName
  deriving (GhcOptionKey -> GhcOptionKey -> Bool
(GhcOptionKey -> GhcOptionKey -> Bool)
-> (GhcOptionKey -> GhcOptionKey -> Bool) -> Eq GhcOptionKey
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GhcOptionKey -> GhcOptionKey -> Bool
== :: GhcOptionKey -> GhcOptionKey -> Bool
$c/= :: GhcOptionKey -> GhcOptionKey -> Bool
/= :: GhcOptionKey -> GhcOptionKey -> Bool
Eq, Eq GhcOptionKey
Eq GhcOptionKey
-> (GhcOptionKey -> GhcOptionKey -> Ordering)
-> (GhcOptionKey -> GhcOptionKey -> Bool)
-> (GhcOptionKey -> GhcOptionKey -> Bool)
-> (GhcOptionKey -> GhcOptionKey -> Bool)
-> (GhcOptionKey -> GhcOptionKey -> Bool)
-> (GhcOptionKey -> GhcOptionKey -> GhcOptionKey)
-> (GhcOptionKey -> GhcOptionKey -> GhcOptionKey)
-> Ord GhcOptionKey
GhcOptionKey -> GhcOptionKey -> Bool
GhcOptionKey -> GhcOptionKey -> Ordering
GhcOptionKey -> GhcOptionKey -> GhcOptionKey
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: GhcOptionKey -> GhcOptionKey -> Ordering
compare :: GhcOptionKey -> GhcOptionKey -> Ordering
$c< :: GhcOptionKey -> GhcOptionKey -> Bool
< :: GhcOptionKey -> GhcOptionKey -> Bool
$c<= :: GhcOptionKey -> GhcOptionKey -> Bool
<= :: GhcOptionKey -> GhcOptionKey -> Bool
$c> :: GhcOptionKey -> GhcOptionKey -> Bool
> :: GhcOptionKey -> GhcOptionKey -> Bool
$c>= :: GhcOptionKey -> GhcOptionKey -> Bool
>= :: GhcOptionKey -> GhcOptionKey -> Bool
$cmax :: GhcOptionKey -> GhcOptionKey -> GhcOptionKey
max :: GhcOptionKey -> GhcOptionKey -> GhcOptionKey
$cmin :: GhcOptionKey -> GhcOptionKey -> GhcOptionKey
min :: GhcOptionKey -> GhcOptionKey -> GhcOptionKey
Ord)

instance FromJSONKey GhcOptionKey where
  fromJSONKey :: FromJSONKeyFunction GhcOptionKey
fromJSONKey = (Text -> Parser GhcOptionKey) -> FromJSONKeyFunction GhcOptionKey
forall a. (Text -> Parser a) -> FromJSONKeyFunction a
FromJSONKeyTextParser ((Text -> Parser GhcOptionKey) -> FromJSONKeyFunction GhcOptionKey)
-> (Text -> Parser GhcOptionKey)
-> FromJSONKeyFunction GhcOptionKey
forall a b. (a -> b) -> a -> b
$ \Text
t ->
    case Text
t of
      Text
"*" -> GhcOptionKey -> Parser GhcOptionKey
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure GhcOptionKey
GOKOldEverything
      Text
"$everything" -> GhcOptionKey -> Parser GhcOptionKey
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure GhcOptionKey
GOKEverything
      Text
"$locals" -> GhcOptionKey -> Parser GhcOptionKey
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure GhcOptionKey
GOKLocals
      Text
"$targets" -> GhcOptionKey -> Parser GhcOptionKey
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure GhcOptionKey
GOKTargets
      Text
_ ->
        case [Char] -> Maybe PackageName
parsePackageName ([Char] -> Maybe PackageName) -> [Char] -> Maybe PackageName
forall a b. (a -> b) -> a -> b
$ Text -> [Char]
T.unpack Text
t of
          Maybe PackageName
Nothing -> [Char] -> Parser GhcOptionKey
forall a. [Char] -> Parser a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char] -> Parser GhcOptionKey) -> [Char] -> Parser GhcOptionKey
forall a b. (a -> b) -> a -> b
$ [Char]
"Invalid package name: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
forall a. Show a => a -> [Char]
show Text
t
          Just PackageName
x -> GhcOptionKey -> Parser GhcOptionKey
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GhcOptionKey -> Parser GhcOptionKey)
-> GhcOptionKey -> Parser GhcOptionKey
forall a b. (a -> b) -> a -> b
$ PackageName -> GhcOptionKey
GOKPackage PackageName
x
  fromJSONKeyList :: FromJSONKeyFunction [GhcOptionKey]
fromJSONKeyList =
    (Text -> Parser [GhcOptionKey])
-> FromJSONKeyFunction [GhcOptionKey]
forall a. (Text -> Parser a) -> FromJSONKeyFunction a
FromJSONKeyTextParser ((Text -> Parser [GhcOptionKey])
 -> FromJSONKeyFunction [GhcOptionKey])
-> (Text -> Parser [GhcOptionKey])
-> FromJSONKeyFunction [GhcOptionKey]
forall a b. (a -> b) -> a -> b
$ \Text
_ -> [Char] -> Parser [GhcOptionKey]
forall a. [Char] -> Parser a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"GhcOptionKey.fromJSONKeyList"