Skip to content

Commit

Permalink
Add HappstackForm type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspervdj committed Dec 9, 2010
1 parent 43de04f commit 616b86f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
27 changes: 15 additions & 12 deletions Text/Digestive/Forms/Happstack.hs
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
-- | Module providing a happstack backend for the digestive-functors library
--
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
module Text.Digestive.Forms.Happstack
( happstackEnvironment
( HappstackForm
, happstackEnvironment
, eitherHappstackForm
) where

import Control.Monad (liftM)
import Data.Maybe (fromMaybe)

import Data.ByteString.Lazy as LB
import Data.ByteString.Lazy.UTF8 as LB (toString)
import Happstack.Server ( Input (..), ServerPartT, getDataFn, lookInput
, Method (..), withRequest, runServerPartT, rqMethod
)

import Text.Digestive.Forms (FormInput (..), FormFileInput (..))
import Text.Digestive.Forms (FormInput (..))
import Text.Digestive.Types (Form (..), Environment (..), viewForm, eitherForm)

instance FormInput Input where
instance FormInput Input (Maybe String, LB.ByteString) where
getInputString = LB.toString . inputValue
getInputFile inp = FormFileInput
{ formFileInputName = fromMaybe "" $ inputFilename inp
, formFileInputContents = inputValue inp
}
getInputFile inp = (inputFilename inp, inputValue inp)

-- | Simplification of the `Form` type, instantiated to Happstack
--
type HappstackForm m e v a = Form (ServerPartT m) Input e v a

-- | Environment that will fetch input from the parameters parsed by Happstack
--
happstackEnvironment :: Monad m => Environment (ServerPartT m) Input
happstackEnvironment :: (Monad m) => Environment (ServerPartT m) Input
happstackEnvironment = Environment $ getDataFn . lookInput . show

-- | Run a happstack form
Expand All @@ -38,9 +41,9 @@ happstackEnvironment = Environment $ getDataFn . lookInput . show
-- you will get the actual result
--
eitherHappstackForm :: (Monad m, Functor m)
=> Form (ServerPartT m) Input e v a -- ^ Form
-> String -- ^ Form name
-> ServerPartT m (Either v a) -- ^ Result
=> HappstackForm m e v a -- ^ Form
-> String -- ^ Form name
-> ServerPartT m (Either v a) -- ^ Result
eitherHappstackForm form name = withRequest $ \rq -> flip runServerPartT rq $
case rqMethod rq of GET -> liftM Left $ viewForm form name
_ -> eitherForm form name happstackEnvironment
3 changes: 2 additions & 1 deletion digestive-functors-happstack.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ Library
Build-depends: base >= 4 && < 5,
digestive-functors == 0.0.1,
happstack-server >= 0.5 && < 0.6,
utf8-string >= 0.3
utf8-string >= 0.3,
bytestring >= 0.9

0 comments on commit 616b86f

Please sign in to comment.