module OpenTelemetry.Environment (
  lookupBooleanEnv,
) where

import qualified Data.Char as C
import System.Environment (lookupEnv)


{- | Does the given value of an environment variable correspond to "true" according
to [the OpenTelemetry specification](https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#boolean-value)?
-}
isTrue :: String -> Bool
isTrue :: [Char] -> Bool
isTrue = ([Char]
"true" [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
==) ([Char] -> Bool) -> ([Char] -> [Char]) -> [Char] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Char) -> [Char] -> [Char]
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
C.toLower


lookupBooleanEnv :: String -> IO Bool
lookupBooleanEnv :: [Char] -> IO Bool
lookupBooleanEnv = (Maybe [Char] -> Bool) -> IO (Maybe [Char]) -> IO Bool
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Bool -> ([Char] -> Bool) -> Maybe [Char] -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False [Char] -> Bool
isTrue) (IO (Maybe [Char]) -> IO Bool)
-> ([Char] -> IO (Maybe [Char])) -> [Char] -> IO Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> IO (Maybe [Char])
lookupEnv