{-# LANGUAGE NoImplicitPrelude #-}

module Stack.Types.CompilerBuild
  ( CompilerBuild (..)
  , compilerBuildName
  , compilerBuildSuffix
  , parseCompilerBuild
  ) where

import           Stack.Prelude
import           Pantry.Internal.AesonExtended ( FromJSON, parseJSON, withText )
import           Data.Text as T

data CompilerBuild
  = CompilerBuildStandard
  | CompilerBuildSpecialized String
  deriving Int -> CompilerBuild -> ShowS
[CompilerBuild] -> ShowS
CompilerBuild -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CompilerBuild] -> ShowS
$cshowList :: [CompilerBuild] -> ShowS
show :: CompilerBuild -> String
$cshow :: CompilerBuild -> String
showsPrec :: Int -> CompilerBuild -> ShowS
$cshowsPrec :: Int -> CompilerBuild -> ShowS
Show

instance FromJSON CompilerBuild where
  -- Strange structuring is to give consistent error messages

  parseJSON :: Value -> Parser CompilerBuild
parseJSON =
    forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText
      String
"CompilerBuild"
      (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show) forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadThrow m => String -> m CompilerBuild
parseCompilerBuild forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack)

-- | Descriptive name for compiler build

compilerBuildName :: CompilerBuild -> String
compilerBuildName :: CompilerBuild -> String
compilerBuildName CompilerBuild
CompilerBuildStandard = String
"standard"
compilerBuildName (CompilerBuildSpecialized String
s) = String
s

-- | Suffix to use for filenames/directories constructed with compiler build

compilerBuildSuffix :: CompilerBuild -> String
compilerBuildSuffix :: CompilerBuild -> String
compilerBuildSuffix CompilerBuild
CompilerBuildStandard = String
""
compilerBuildSuffix (CompilerBuildSpecialized String
s) = Char
'-' forall a. a -> [a] -> [a]
: String
s

-- | Parse compiler build from a String.

parseCompilerBuild :: (MonadThrow m) => String -> m CompilerBuild
parseCompilerBuild :: forall (m :: * -> *). MonadThrow m => String -> m CompilerBuild
parseCompilerBuild String
"" = forall (f :: * -> *) a. Applicative f => a -> f a
pure CompilerBuild
CompilerBuildStandard
parseCompilerBuild String
"standard" = forall (f :: * -> *) a. Applicative f => a -> f a
pure CompilerBuild
CompilerBuildStandard
parseCompilerBuild String
name = forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> CompilerBuild
CompilerBuildSpecialized String
name)