{-# LANGUAGE TypeApplications #-}
module Options where
import Options.Applicative
import Data.Bifunctor (bimap, first)
import Data.Either.Extra
import qualified Data.Text as Text
import Control.Monad
import Text.Read (readEither)
import AlBhed (Volume)
import qualified AlBhed
import Location (Location)
import qualified Location
import Data.PrintableText (PrintableText)
import qualified Data.PrintableText as PrintableText
volume :: Parser Volume
volume = argument parseVolume (metavar "VOLUME")
location :: Parser Location
location = Location.Location
<$> argument parsePrintableText (metavar "AREA")
<*> optional (argument parsePrintableText (metavar "SECTION"))
parseVolume :: ReadM Volume
parseVolume = eitherReader $
(first parseError . input) >=> volume
where
parseError = const "Failed to parse volume number"
input = readEither @Int
volume = maybeToEither "Volume must be 1 to 26" . AlBhed.toVolume
nothing :: ReadM (Maybe a)
nothing = pure Nothing
parsePrintableText :: ReadM PrintableText
parsePrintableText = eitherReader $
input >=> location
where
input = Right
location = maybeToEither "Location can't contain only whitespace" . PrintableText.fromText . Text.pack