{-# LANGUAGE OverloadedStrings #-}

module Test.Tasty.AutoCollect.ModuleType (
  ModuleType (..),
  parseModuleType,
) where

import Data.Char (isSpace)
import Data.Text (Text)
import qualified Data.Text as Text

import Test.Tasty.AutoCollect.Config
import Test.Tasty.AutoCollect.Constants

data ModuleType
  = ModuleMain AutoCollectConfigPartial
  | ModuleTest
  deriving (Int -> ModuleType -> ShowS
[ModuleType] -> ShowS
ModuleType -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ModuleType] -> ShowS
$cshowList :: [ModuleType] -> ShowS
show :: ModuleType -> String
$cshow :: ModuleType -> String
showsPrec :: Int -> ModuleType -> ShowS
$cshowsPrec :: Int -> ModuleType -> ShowS
Show, ModuleType -> ModuleType -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ModuleType -> ModuleType -> Bool
$c/= :: ModuleType -> ModuleType -> Bool
== :: ModuleType -> ModuleType -> Bool
$c== :: ModuleType -> ModuleType -> Bool
Eq)

parseModuleType :: Text -> Maybe ModuleType
parseModuleType :: Text -> Maybe ModuleType
parseModuleType = [Text] -> Maybe ModuleType
go forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text]
groupWhitespace
  where
    go :: [Text] -> Maybe ModuleType
go [] = forall a. Maybe a
Nothing
    go (Text
"{-" : Text
_ : Text
x : [Text]
rest)
      | String -> Bool
isMainComment (Text -> String
Text.unpack Text
x) =
          case Text -> Either Text AutoCollectConfigPartial
parseConfig forall a b. (a -> b) -> a -> b
$ [Text] -> Text
Text.concat forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
takeWhile (forall a. Eq a => a -> a -> Bool
/= Text
"-}") [Text]
rest of
            Right AutoCollectConfigPartial
cfg -> forall a. a -> Maybe a
Just (AutoCollectConfigPartial -> ModuleType
ModuleMain AutoCollectConfigPartial
cfg)
            Left Text
e -> forall a. String -> a
errorWithoutStackTrace forall a b. (a -> b) -> a -> b
$ String
"Could not parse configuration: " forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
e
      | String -> Bool
isTestComment (Text -> String
Text.unpack Text
x) = forall a. a -> Maybe a
Just ModuleType
ModuleTest
    go (Text
_ : [Text]
rest) = [Text] -> Maybe ModuleType
go [Text]
rest

-- | Group consecutive whitespace characters.
--
-- >>> groupWhitespace " a  bb  c "
-- [" ", "a", "  ", "bb", "  ", "c", " "]
groupWhitespace :: Text -> [Text]
groupWhitespace :: Text -> [Text]
groupWhitespace = (Char -> Char -> Bool) -> Text -> [Text]
Text.groupBy (\Char
c1 Char
c2 -> Char -> Bool
isSpace Char
c1 forall a. Eq a => a -> a -> Bool
== Char -> Bool
isSpace Char
c2)