summaryrefslogtreecommitdiff
path: root/src/Conftrack/Source/Trivial.hs
blob: d4151c2acfa8ae74730723456515f448e980ac64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE ImpredicativeTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE OverloadedStrings #-}

module Conftrack.Source.Trivial where

import Conftrack.Value (Key, Value(..), ConfigError(..))
import Conftrack.Source (SomeSource(..), ConfigSource (..))

import Control.Monad.State (get, modify, MonadState (..))
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as M
import Data.Function ((&))
import qualified Data.Text as T


newtype Trivial = Trivial (Map Key Value)

mkTrivialSource :: [(Key, Value)] -> SomeSource
mkTrivialSource pairs = SomeSource (source, [])
  where source = Trivial (M.fromList pairs)

instance ConfigSource Trivial where
  type SourceState Trivial = [Key]
  fetchValue key (Trivial tree) = do
    case M.lookup key tree of
      Nothing -> pure $ Left (NotPresent key)
      Just val -> do
        modify (key :)
        pure $ Right (val, "Trivial source with keys "<> T.pack (show (M.keys tree)))

  leftovers (Trivial tree) = do
    used <- get

    M.keys tree
     & filter (`notElem` used)
     & Just
     & pure