packages feed

digestive-functors-snap 0.1.3.1 → 0.3.0.0

raw patch · 3 files changed

+57/−87 lines, 3 filesdep +containersdep +textdep −bytestringdep −utf8-stringdep ~digestive-functorsdep ~snap-core

Dependencies added: containers, text

Dependencies removed: bytestring, utf8-string

Dependency ranges changed: digestive-functors, snap-core

Files

digestive-functors-snap.cabal view
@@ -1,24 +1,24 @@-Name:                digestive-functors-snap-Version:             0.1.3.1-Synopsis:            Snap backend for the digestive-functors library-Description:         This is a snap backend for the digestive-functors library.--Homepage:            http://github.com/jaspervdj/digestive-functors-License:             BSD3-License-file:        LICENSE-Author:              Jasper Van der Jeugt-Maintainer:          jaspervdj@gmail.com-Category:            Web-Build-type:          Simple-Cabal-version:       >=1.6-+Name:          digestive-functors-snap+Version:       0.3.0.0+Synopsis:      Snap backend for the digestive-functors library+Description:   Snap backend for the digestive-functors library+Homepage:      http://github.com/jaspervdj/digestive-functors+License:       BSD3+License-file:  LICENSE+Author:        Jasper Van der Jeugt <m@jaspervdj.be>+Maintainer:    Jasper Van der Jeugt <m@jaspervdj.be>+Category:      Web+Build-type:    Simple+Cabal-version: >= 1.6  Library-  Hs-source-dirs:    src-  Exposed-modules:   Text.Digestive.Forms.Snap+  Hs-source-dirs:  src+  GHC-options:     -Wall -fwarn-tabs+  Exposed-modules: Text.Digestive.Snap+   Build-depends:-    base               >= 4     && < 5,-    bytestring         >= 0.9   && < 0.10,-    digestive-functors >= 0.2.1 && < 0.3,-    snap-core          >= 0.5   && < 0.9,-    utf8-string        >= 0.3   && < 0.4+    base               >= 4    && < 5,+    containers         >= 0.3  && < 0.5,+    digestive-functors >= 0.3  && < 0.4,+    snap-core          >= 0.7  && < 0.9,+    text               >= 0.11 && < 0.12
− src/Text/Digestive/Forms/Snap.hs
@@ -1,66 +0,0 @@--- | Module providing a snap backend for the digestive-functors library----{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}-module Text.Digestive.Forms.Snap-    ( SnapInput-    , SnapForm-    , snapEnvironment-    , eitherSnapForm-    , runViewSnapForm-    ) where----------------------------------------------------------------------------------import Control.Applicative  ((<$>))-import Control.Monad        (liftM)-import Data.ByteString      as SB-import Data.ByteString.UTF8 as SB (toString, fromString)-import Snap.Core---------------------------------------------------------------------------------import Text.Digestive.Forms (FormInput (..))-import Text.Digestive.Types (Form (..), Environment (..), viewForm, runViewForm, eitherForm)----------------------------------------------------------------------------------newtype SnapInput = SnapInput {unSnapInput :: SB.ByteString}--instance FormInput SnapInput () where-    getInputStrings = return . SB.toString . unSnapInput-    getInputFile = const Nothing---- | Simplification of the `Form` type, instantiated to Snap----type SnapForm m = Form m SnapInput---- | Environment that will fetch input from the parameters parsed by Snap----snapEnvironment :: (MonadSnap m) => Environment m SnapInput-snapEnvironment = Environment $ \id' -> do-    input' <- getParam (SB.fromString $ show id')-    return $ SnapInput <$> input'---- | Run a snap form------ * When we are responding to a GET request, you will simply receive the form---   as a view------ * When we are responding to another request method, the form data will be---   used. When errors occur, you will receive the form as a view, otherwise,---   you will get the actual result----eitherSnapForm :: (MonadSnap m)-               => SnapForm m e v a  -- ^ Form-               -> String            -- ^ Form name-               -> m (Either v a)    -- ^ Result-eitherSnapForm form name = do-    method' <- rqMethod <$> getRequest-    case method' of GET -> liftM Left $ viewForm form name-                    _   -> eitherForm form name snapEnvironment------------------------------------------------------------------------------------- | Render a snap form using the current Params-runViewSnapForm :: (MonadSnap m)-                => SnapForm m e v a-                -> String -                -> m (v, Maybe a)-runViewSnapForm form name = runViewForm form name snapEnvironment-    
+ src/Text/Digestive/Snap.hs view
@@ -0,0 +1,36 @@+-- | Module providing a Snap backend for the digestive-functors library+module Text.Digestive.Snap+    ( runForm+    ) where++import Control.Applicative ((<$>))+import Data.Maybe (fromMaybe)+import qualified Data.Map as M++import Data.Text (Text)+import qualified Data.Text.Encoding as T+import qualified Snap.Core as Snap++import Text.Digestive.Form+import Text.Digestive.Types+import Text.Digestive.View++snapEnv :: Snap.MonadSnap m => Env m+snapEnv path =+    map (TextInput . T.decodeUtf8) . findParams <$> Snap.getPostParams+  where+    findParams = fromMaybe [] . M.lookup name +    name       = T.encodeUtf8 $ fromPath path++-- | Runs a form with the HTTP input provided by Happstack.+--+-- Automatically picks between 'getForm' and 'postForm' based on the request+-- method.+runForm :: Snap.MonadSnap m+        => Text                 -- ^ Name for the form+        -> Form v m a           -- ^ Form to run+        -> m (View v, Maybe a)  -- ^ Result+runForm name form = Snap.getRequest >>= \rq ->+    case Snap.rqMethod rq of+        Snap.GET -> return (getForm name form, Nothing)+        _        -> postForm name form snapEnv