-- | Provides common functionality for battery services.
module Navi.Services.Battery.Common
  ( batteryAppDecoder,
  )
where

import Navi.Prelude
import Pythia.Services.Battery
  ( BatteryApp
      ( BatteryAppAcpi,
        BatteryAppSysFs,
        BatteryAppUPower
      ),
  )

-- | TOML decoder for 'BatteryApp'.
--
-- @since 0.1
batteryAppDecoder :: Decoder BatteryApp
batteryAppDecoder :: Decoder BatteryApp
batteryAppDecoder =
  Decoder Text
forall a. DecodeTOML a => Decoder a
tomlDecoder Decoder Text -> (Text -> Decoder BatteryApp) -> Decoder BatteryApp
forall a b. Decoder a -> (a -> Decoder b) -> Decoder b
forall (m :: Type -> Type) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Text
"acpi" -> BatteryApp -> Decoder BatteryApp
forall a. a -> Decoder a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure BatteryApp
BatteryAppAcpi
    Text
"sysfs" -> BatteryApp -> Decoder BatteryApp
forall a. a -> Decoder a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure BatteryApp
BatteryAppSysFs
    Text
"upower" -> BatteryApp -> Decoder BatteryApp
forall a. a -> Decoder a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure BatteryApp
BatteryAppUPower
    Text
bad ->
      String -> Decoder BatteryApp
forall a. String -> Decoder a
forall (m :: Type -> Type) a. MonadFail m => String -> m a
fail
        (String -> Decoder BatteryApp) -> String -> Decoder BatteryApp
forall a b. (a -> b) -> a -> b
$ Text -> String
unpackText
        (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat
          [ Text
"Unexpected battery app: ",
            Text
bad,
            Text
". Expected one of <acpi | sysfs | upower>."
          ]