{-# 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 = eitherReader parseLicense
where
parseLicense raw = maybeToRight errMsg (parseLicenseAndFileType $ T.pack raw)
errMsg = T.unpack $ mconcat
[ "invalid license/file type, must be in format 'licenseType:fileType' "
, "(e.g. bsd3:haskell)"
, "\nAvailable license types: "
, T.toLower (allValuesToText @LicenseType)
, "\nAvailable file types: "
, T.toLower (allValuesToText @FileType)
]
licenseTypeReader :: ReadM LicenseType
licenseTypeReader = eitherReader parseLicenseType
where
parseLicenseType raw = maybeToRight errMsg (textToEnum $ T.pack raw)
errMsg = T.unpack $ mconcat
[ "invalid license type, available options: "
, T.toLower (allValuesToText @LicenseType)
]
regexReader :: ReadM Regex
regexReader = eitherReader parse
where parse input = mapLeft displayException (compile . T.pack $ input)
parseLicenseAndFileType :: Text -> Maybe (LicenseType, FileType)
parseLicenseAndFileType raw
| [rawLicenseType, rawFileType] <- TP.splitOn ":" raw = do
licenseType <- textToEnum rawLicenseType
fileType <- textToEnum rawFileType
pure (licenseType, fileType)
| otherwise = Nothing