module Descriptive.Form
(
input
,validate
,Form (..)
)
where
import Descriptive
import Control.Monad.State.Strict
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as M
import Data.Text (Text)
data Form
= Input !Text
| Constraint !Text
deriving (Show,Eq)
input :: Text -> Consumer (Map Text Text) Form Text
input name =
consumer (return d)
(do s <- get
return (case M.lookup name s of
Nothing -> Continued d
Just a -> Succeeded a))
where d = Unit (Input name)
validate :: Text
-> (a -> Maybe b)
-> Consumer (Map Text Text) Form a
-> Consumer (Map Text Text) Form b
validate d' check =
wrap (liftM wrapper)
(\d p ->
do s <- get
r <- p
case r of
(Failed e) -> return (Failed e)
(Continued e) ->
return (Continued (wrapper e))
(Succeeded a) ->
case check a of
Nothing ->
do doc <- withStateT (const s) d
return (Continued (wrapper doc))
Just a' -> return (Succeeded a'))
where wrapper = Wrap (Constraint d')