{-# LANGUAGE NoImplicitPrelude #-}

module Stack.Types.GhcOptions
  ( GhcOptions (..)
  ) where

import           Data.Aeson.Types ( FromJSON (..), withText )
import           Data.Attoparsec.Args ( EscapingMode (Escaping), parseArgs )
import qualified Data.Text as T
import           Stack.Prelude

newtype GhcOptions = GhcOptions { GhcOptions -> [Text]
unGhcOptions :: [Text] }

instance FromJSON GhcOptions where
  parseJSON :: Value -> Parser GhcOptions
parseJSON = String -> (Text -> Parser GhcOptions) -> Value -> Parser GhcOptions
forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText String
"GhcOptions" ((Text -> Parser GhcOptions) -> Value -> Parser GhcOptions)
-> (Text -> Parser GhcOptions) -> Value -> Parser GhcOptions
forall a b. (a -> b) -> a -> b
$ \Text
t ->
    case EscapingMode -> Text -> Either String [String]
parseArgs EscapingMode
Escaping Text
t of
      Left String
e -> String -> Parser GhcOptions
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
e
      Right [String]
opts -> GhcOptions -> Parser GhcOptions
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GhcOptions -> Parser GhcOptions)
-> GhcOptions -> Parser GhcOptions
forall a b. (a -> b) -> a -> b
$ [Text] -> GhcOptions
GhcOptions ([Text] -> GhcOptions) -> [Text] -> GhcOptions
forall a b. (a -> b) -> a -> b
$ (String -> Text) -> [String] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map String -> Text
T.pack [String]
opts