module Descriptive.Formlet
(
indexed
,FormletState(..)
,Formlet(..))
where
import Descriptive
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as M
import Data.Text (Text)
data Formlet
= Index !Integer
| Constrained !Text
deriving (Show,Eq)
data FormletState =
FormletState {formletMap :: (Map Integer Text)
,formletIndex :: !Integer}
deriving (Show,Eq)
indexed :: Consumer FormletState Formlet Text
indexed =
consumer (\(nextIndex -> (i,s)) -> (d i,s))
(\(nextIndex -> (i,s)) ->
case M.lookup i (formletMap s) of
Nothing -> (Failed (d i),s)
Just a -> (Succeeded a,s))
where d = Unit . Index
nextIndex s =
(formletIndex s,s {formletIndex = formletIndex s + 1})