{-# LANGUAGE RecordWildCards            #-}

module Data.KeyStore.KS.Configuration where

import           Data.KeyStore.Types
import qualified Data.Map               as Map
import           Data.Maybe
import           Text.KSRegex


configurationSettings :: Configuration -> Settings
configurationSettings :: Configuration -> Settings
configurationSettings = Configuration -> Settings
_cfg_settings

trigger :: Name -> Configuration -> Settings -> E Settings
trigger :: Name -> Configuration -> Settings -> E Settings
trigger Name
nm Configuration
cfg Settings
stgs0 =
    case Settings -> [SettingID]
checkSettingsCollisions Settings
stgs of
      []   -> forall a b. b -> Either a b
Right Settings
stgs
      [SettingID]
sids -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ String -> Reason
strMsg forall a b. (a -> b) -> a -> b
$ String
"settings collided in triggers: "
                                forall a. [a] -> [a] -> [a]
++ String
nm_s forall a. [a] -> [a] -> [a]
++ String
": " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show(forall a b. (a -> b) -> [a] -> [b]
map SettingID -> Text
_SettingID [SettingID]
sids)
                                forall a. [a] -> [a] -> [a]
++ String
"\n\n" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (Settings
stgs0 forall a. a -> [a] -> [a]
: [Settings]
t_stgss)
  where
    stgs :: Settings
stgs     = forall a. Monoid a => [a] -> a
mconcat forall a b. (a -> b) -> a -> b
$ Settings
stgs0 forall a. a -> [a] -> [a]
: [Settings]
t_stgss

    t_stgss :: [Settings]
t_stgss  = [ Settings
_trg_settings | Trigger{Settings
Pattern
TriggerID
_trg_settings :: Trigger -> Settings
_trg_pattern :: Trigger -> Pattern
_trg_id :: Trigger -> TriggerID
_trg_pattern :: Pattern
_trg_id :: TriggerID
_trg_settings :: Settings
..}<-forall k a. Map k a -> [a]
Map.elems forall a b. (a -> b) -> a -> b
$ Configuration -> TriggerMap
_cfg_triggers Configuration
cfg,
                                                            Pattern -> Bool
mtch Pattern
_trg_pattern ]

    mtch :: Pattern -> Bool
mtch Pattern
pat = forall a. Maybe a -> Bool
isJust forall a b. (a -> b) -> a -> b
$ Regex -> String -> Maybe [String]
matchRegex (Pattern -> Regex
_pat_regex Pattern
pat) String
nm_s

    nm_s :: String
nm_s     = Name -> String
_name Name
nm