{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
module Headroom.Command.Readers
( licenseReader
, licenseTypeReader
, regexReader
, parseLicenseAndFileType
)
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
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)
parseLicenseAndFileType (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)
]
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)
]
regexReader :: ReadM Regex
regexReader :: ReadM Regex
regexReader = (String -> Either String Regex) -> ReadM Regex
forall a. (String -> Either String a) -> ReadM a
eitherReader String -> Either String Regex
parse
where 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)
parseLicenseAndFileType :: Text -> Maybe (LicenseType, FileType)
parseLicenseAndFileType :: Text -> Maybe (LicenseType, FileType)
parseLicenseAndFileType Text
raw
| [Text
rawLicenseType, Text
rawFileType] <- Text -> Text -> [Text]
TP.splitOn Text
":" Text
raw = do
LicenseType
licenseType <- Text -> Maybe LicenseType
forall a. EnumExtra a => Text -> Maybe a
textToEnum Text
rawLicenseType
FileType
fileType <- Text -> Maybe FileType
forall a. EnumExtra a => Text -> Maybe a
textToEnum Text
rawFileType
(LicenseType, FileType) -> Maybe (LicenseType, FileType)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (LicenseType
licenseType, FileType
fileType)
| Bool
otherwise = Maybe (LicenseType, FileType)
forall a. Maybe a
Nothing