<commit_before>{-# LANGUAGE FlexibleContexts  #-}
{-# LANGUAGE OverloadedStrings #-}

module Hecate.Context
  ( AppContext(..)
  , AppConfig
  , appConfigDataDirectory
  , appConfigKeyId
  , KeyId
  , unKeyId
  , getEnvOrDefault
  , configure
  ) where

import Control.Monad.Except
import Data.Maybe (fromMaybe)
import System.Directory (createDirectory, doesDirectoryExist)
import System.Posix.Env (getEnv)
import Text.Toml
import Text.Toml.Types
import qualified Data.HashMap.Lazy as HM
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import qualified Database.SQLite.Simple as SQLite

import Hecate.Error


-- | 'AppContext' represents the shared environment for computations which occur
-- within our application.  Values of this type are created by 'createContext'.
data AppContext = AppContext
  { appContextKeyId      :: KeyId
  , appContextConnection :: SQLite.Connection
  }

-- | An 'AppConfig' represents values read from a configuration file
data AppConfig = AppConfig
  { appConfigDataDirectory :: FilePath
  , appConfigKeyId         :: KeyId
  } deriving (Show, Eq)

-- | A 'KeyId' represents a GPG Key Id
newtype KeyId = KeyId T.Text
  deriving Eq

instance Show KeyId where
  show (KeyId a) = show a

unKeyId :: KeyId -> T.Text
unKeyId (KeyId keyid) = keyid

-- | Look up values in a given HashMap
lup :: HM.HashMap T.Text v -> T.Text -> Either AppError v
lup hm key = maybe err pure (HM.lookup key hm)<commit_msg>Hecate.Context: use record syntax for KeyId<commit_after>{-# LANGUAGE FlexibleContexts  #-}
{-# LANGUAGE OverloadedStrings #-}

module Hecate.Context
  ( AppContext(..)
  , AppConfig
  , appConfigDataDirectory
  , appConfigKeyId
  , KeyId
  , unKeyId
  , getEnvOrDefault
  , configure
  ) where

import Control.Monad.Except
import Data.Maybe (fromMaybe)
import System.Directory (createDirectory, doesDirectoryExist)
import System.Posix.Env (getEnv)
import Text.Toml
import Text.Toml.Types
import qualified Data.HashMap.Lazy as HM
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import qualified Database.SQLite.Simple as SQLite

import Hecate.Error


-- | 'AppContext' represents the shared environment for computations which occur
-- within our application.  Values of this type are created by 'createContext'.
data AppContext = AppContext
  { appContextKeyId      :: KeyId
  , appContextConnection :: SQLite.Connection
  }

-- | An 'AppConfig' represents values read from a configuration file
data AppConfig = AppConfig
  { appConfigDataDirectory :: FilePath
  , appConfigKeyId         :: KeyId
  } deriving (Show, Eq)

-- | A 'KeyId' represents a GPG Key Id
newtype KeyId = KeyId { unKeyId :: T.Text }
  deriving Eq

instance Show KeyId where
  show (KeyId a) = show a

-- | Look up values in a given HashMap
lup :: HM.HashMap T.Text v -> T.Text -> Either AppError v
lup hm key = maybe err pure (HM.lookup key hm)
{'fraction_non_alphanumeric': 0.08451679944579148, 'fraction_numerical': 0.0, 'mean_word_length': 3.8456375838926173, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2933381', 'n_tokens_mistral': 903, 'n_tokens_neox': 887, 'n_words': 304}