module Text.Digestive.Forms.Snap
( SnapInput
, SnapForm
, snapEnvironment
, eitherSnapForm
) where
import Control.Applicative ((<$>))
import Control.Monad (liftM)
import Data.ByteString as SB
import Data.ByteString.UTF8 as SB (toString, fromString)
import Snap.Types
import Text.Digestive.Forms (FormInput (..))
import Text.Digestive.Types (Form (..), Environment (..), viewForm, eitherForm)
newtype SnapInput = SnapInput {unSnapInput :: SB.ByteString}
instance FormInput SnapInput () where
getInputString = Just . SB.toString . unSnapInput
getInputFile = const Nothing
type SnapForm e v a = Form Snap SnapInput e v a
snapEnvironment :: Environment Snap SnapInput
snapEnvironment = Environment $ \id' -> do
input' <- getParam (SB.fromString $ show id')
return $ SnapInput <$> input'
eitherSnapForm :: SnapForm e v a
-> String
-> Snap (Either v a)
eitherSnapForm form name = do
method' <- rqMethod <$> getRequest
case method' of GET -> liftM Left $ viewForm form name
_ -> eitherForm form name snapEnvironment