{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeFamilies #-}
module Options.Harg.Sources.DefaultStr where
import Data.Functor.Compose (Compose (..))
import Data.Kind (Type)
import GHC.Generics (Generic)
import qualified Data.Barbie as B
import Options.Harg.Sources.Types
import Options.Harg.Types
data DefaultStrSource (f :: Type -> Type) = DefaultStrSource
deriving (Generic, B.FunctorB, B.TraversableB, B.ProductB)
data DefaultStrSourceVal = DefaultStrSourceVal
instance GetSource DefaultStrSource f where
type SourceVal DefaultStrSource = DefaultStrSourceVal
getSource HargCtx{..} _
= pure DefaultStrSourceVal
instance
B.FunctorB a => RunSource DefaultStrSourceVal a where
runSource DefaultStrSourceVal opt
= [runDefaultStrSource opt]
runDefaultStrSource
:: forall a f.
( B.FunctorB a
, Applicative f
)
=> a (Compose Opt f)
-> a (Compose SourceRunResult f)
runDefaultStrSource
= B.bmap go
where
go :: Compose Opt f x -> Compose SourceRunResult f x
go (Compose opt@Opt{..})
= case _optDefaultStr of
Nothing
-> Compose $ pure <$> OptNotFound
Just str
-> Compose $ tryParse str
where
tryParse
= either
(OptFoundNoParse . toOptError opt (Just "DefaultStrSource"))
OptParsed
. _optReader