{-# LANGUAGE TemplateHaskell, DataKinds #-}
{-# OPTIONS_GHC -fplugin=Polysemy.Plugin #-}

module System.XDG.Env where

import           Polysemy
import           System.Environment             ( lookupEnv )


data Env m a where
  GetEnv ::String -> Env m (Maybe String)

makeSem ''Env


type EnvList = [(String, String)]

runEnvList :: EnvList -> InterpreterFor Env r
runEnvList :: forall (r :: EffectRow). EnvList -> InterpreterFor Env r
runEnvList EnvList
envs = forall (e :: Effect) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall (rInitial :: EffectRow) x. e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret (\(GetEnv String
name) -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
name EnvList
envs)


runEnvIO :: Member (Embed IO) r => InterpreterFor Env r
runEnvIO :: forall (r :: EffectRow).
Member (Embed IO) r =>
InterpreterFor Env r
runEnvIO = forall (e :: Effect) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall (rInitial :: EffectRow) x. e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret (\(GetEnv String
name) -> forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
embed forall a b. (a -> b) -> a -> b
$ String -> IO (Maybe String)
lookupEnv String
name)