digestive-functors-snap (empty) → 0.0.2.0
raw patch · 4 files changed
+109/−0 lines, 4 filesdep +basedep +bytestringdep +digestive-functorssetup-changed
Dependencies added: base, bytestring, digestive-functors, snap-core, utf8-string
Files
- LICENSE +31/−0
- Setup.hs +2/−0
- digestive-functors-snap.cabal +23/−0
- src/Text/Digestive/Forms/Snap.hs +53/−0
+ LICENSE view
@@ -0,0 +1,31 @@+Copyright (c) 2010, Jasper Van der Jeugt+ +All rights reserved.+ +Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are+met:+ + * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.+ + * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.+ + * Neither the name of Jasper Van der Jeugt nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.+ +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ digestive-functors-snap.cabal view
@@ -0,0 +1,23 @@+Name: digestive-functors-snap+Version: 0.0.2.0+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+++Library+ Hs-source-dirs: src+ Exposed-modules: Text.Digestive.Forms.Snap+ Build-depends: base >= 4 && < 5,+ digestive-functors == 0.0.2.*,+ snap-core >= 0.2 && < 0.3,+ bytestring >= 0.9,+ utf8-string >= 0.3
+ src/Text/Digestive/Forms/Snap.hs view
@@ -0,0 +1,53 @@+-- | Module providing a snap backend for the digestive-functors library+--+{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}+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++-- | Simplification of the `Form` type, instantiated to Snap+--+type SnapForm e v a = Form Snap SnapInput e v a++-- | Environment that will fetch input from the parameters parsed by Snap+--+snapEnvironment :: Environment Snap 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 :: SnapForm e v a -- ^ Form+ -> String -- ^ Form name+ -> Snap (Either v a) -- ^ Result+eitherSnapForm form name = do+ method' <- rqMethod <$> getRequest+ case method' of GET -> liftM Left $ viewForm form name+ _ -> eitherForm form name snapEnvironment