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

{-|
Module      : Headroom.Command.Readers
Description : Custom readers for /optparse-applicative/ library
Copyright   : (c) 2019-2021 Vaclav Svejcar
License     : BSD-3-Clause
Maintainer  : vaclav.svejcar@gmail.com
Stability   : experimental
Portability : POSIX

This module contains custom readers required by the /optparse-applicative/
library to parse data types such as 'LicenseType' or 'FileType'.
-}

module Headroom.Command.Readers
  ( licenseReader
  , licenseTypeReader
  , regexReader
  , parseLicense
  )
where

import           Data.Either.Combinators             ( maybeToRight )
import           Headroom.Configuration.Types        ( LicenseType )
import           Headroom.Data.EnumExtra             ( EnumExtra(..) )
import           Headroom.Data.Regex                 ( Regex(..)
                                                     , compile
                                                     )
import           Headroom.FileType.Types             ( FileType(..) )
import           Options.Applicative
import           RIO
import qualified RIO.Text                           as T
import qualified RIO.Text.Partial                   as TP


-- | Reader for tuple of 'LicenseType' and 'FileType'.
licenseReader :: ReadM (LicenseType, FileType)
licenseReader :: ReadM (LicenseType, FileType)
licenseReader = (String -> Either String (LicenseType, FileType))
-> ReadM (LicenseType, FileType)
forall a. (String -> Either String a) -> ReadM a
eitherReader String -> Either String (LicenseType, FileType)
parseLicense'
 where
  parseLicense' :: String -> Either String (LicenseType, FileType)
parseLicense' String
raw = String
-> Maybe (LicenseType, FileType)
-> Either String (LicenseType, FileType)
forall b a. b -> Maybe a -> Either b a
maybeToRight String
errMsg (Text -> Maybe (LicenseType, FileType)
parseLicense (Text -> Maybe (LicenseType, FileType))
-> Text -> Maybe (LicenseType, FileType)
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack String
raw)
  errMsg :: String
errMsg = Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat
    [ Text
"invalid license/file type, must be in format 'licenseType:fileType' "
    , Text
"(e.g. bsd3:haskell)"
    , Text
"\nAvailable license types: "
    , Text -> Text
T.toLower (EnumExtra LicenseType => Text
forall a. EnumExtra a => Text
allValuesToText @LicenseType)
    , Text
"\nAvailable file types: "
    , Text -> Text
T.toLower (EnumExtra FileType => Text
forall a. EnumExtra a => Text
allValuesToText @FileType)
    ]


-- | Reader for 'LicenseType'.
licenseTypeReader :: ReadM LicenseType
licenseTypeReader :: ReadM LicenseType
licenseTypeReader = (String -> Either String LicenseType) -> ReadM LicenseType
forall a. (String -> Either String a) -> ReadM a
eitherReader String -> Either String LicenseType
forall a. EnumExtra a => String -> Either String a
parseLicenseType
 where
  parseLicenseType :: String -> Either String a
parseLicenseType String
raw = String -> Maybe a -> Either String a
forall b a. b -> Maybe a -> Either b a
maybeToRight String
errMsg (Text -> Maybe a
forall a. EnumExtra a => Text -> Maybe a
textToEnum (Text -> Maybe a) -> Text -> Maybe a
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack String
raw)
  errMsg :: String
errMsg = Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat
    [ Text
"invalid license type, available options: "
    , Text -> Text
T.toLower (EnumExtra LicenseType => Text
forall a. EnumExtra a => Text
allValuesToText @LicenseType)
    ]


-- | Reader for 'Regex'.
regexReader :: ReadM Regex
regexReader :: ReadM Regex
regexReader =
  let parse :: String -> Either String Regex
parse String
input = (SomeException -> String)
-> Either SomeException Regex -> Either String Regex
forall a1 a2 b. (a1 -> a2) -> Either a1 b -> Either a2 b
mapLeft SomeException -> String
forall e. Exception e => e -> String
displayException (Text -> Either SomeException Regex
forall (m :: * -> *). MonadThrow m => Text -> m Regex
compile (Text -> Either SomeException Regex)
-> (String -> Text) -> String -> Either SomeException Regex
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack (String -> Either SomeException Regex)
-> String -> Either SomeException Regex
forall a b. (a -> b) -> a -> b
$ String
input)
  in  (String -> Either String Regex) -> ReadM Regex
forall a. (String -> Either String a) -> ReadM a
eitherReader String -> Either String Regex
parse


-- | Parses 'LicenseType' and 'FileType' from the input string,
-- formatted as @licenseType:fileType@.
--
-- >>> parseLicense "bsd3:haskell"
-- Just (BSD3,Haskell)
parseLicense :: Text -> Maybe (LicenseType, FileType)
parseLicense :: Text -> Maybe (LicenseType, FileType)
parseLicense Text
raw
  | [Text
lt, Text
ft] <- Text -> Text -> [Text]
TP.splitOn Text
":" Text
raw = (,) (LicenseType -> FileType -> (LicenseType, FileType))
-> Maybe LicenseType -> Maybe (FileType -> (LicenseType, FileType))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe LicenseType
forall a. EnumExtra a => Text -> Maybe a
textToEnum Text
lt Maybe (FileType -> (LicenseType, FileType))
-> Maybe FileType -> Maybe (LicenseType, FileType)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Maybe FileType
forall a. EnumExtra a => Text -> Maybe a
textToEnum Text
ft
  | Bool
otherwise                      = Maybe (LicenseType, FileType)
forall a. Maybe a
Nothing