happstack-monad-peel (empty) → 0.1
raw patch · 4 files changed
+152/−0 lines, 4 filesdep +basedep +happstack-serverdep +monad-peelsetup-changed
Dependencies added: base, happstack-server, monad-peel, mtl, transformers
Files
- LICENSE +30/−0
- Setup.hs +6/−0
- happstack-monad-peel.cabal +33/−0
- src/Happstack/Server/MonadPeel.hs +83/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2012, Nils Schweinsberg <mail@nils.cc> + +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 Nils Schweinsberg 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,6 @@+#!/usr/bin/env runhaskell + +import Distribution.Simple + +main :: IO () +main = defaultMainWithHooks simpleUserHooks
+ happstack-monad-peel.cabal view
@@ -0,0 +1,33 @@+Name: happstack-monad-peel +Version: 0.1 + +Synopsis: monad-peel instances for Happstack types +Description: monad-peel instances for Happstack types + +License: BSD3 +License-file: LICENSE +Author: Nils Schweinsberg <mail@nils.cc> +Maintainer: mail@nils.cc +-- Copyright: + +Category: Web, Happstack + +Build-type: Simple +Cabal-version: >= 1.6 + +-- Extra-source-files: + +Library + + HS-source-dirs: src + GHC-options: -Wall -O3 + + Build-depends: + base == 4.*, mtl, transformers, + monad-peel, + happstack-server + + Exposed-modules: + Happstack.Server.MonadPeel + + Other-modules:
+ src/Happstack/Server/MonadPeel.hs view
@@ -0,0 +1,83 @@+{-# LANGUAGE FlexibleInstances #-} +{-# OPTIONS -fno-warn-orphans #-} + +{- | + +Module : Happstack.Server.MonadPeel +Copyright : (c) Nils Schweinsberg, 2012 +License : BSD-style + +Maintainer : Nils Schweinsberg <mail@nils.cc> +Stability : experimental +Portability : non-portable (extended exceptions) + +This module defines instances of 'MonadTransPeel' and 'MonadPeelIO' for +Happstacks data types 'ServerPartT', 'FilterT' and 'WebT'. + +To use these instances, add + +> import Happstack.Server.MonadPeel () + +to the import list of your Haskell module. + +-} + +module Happstack.Server.MonadPeel + ( + ) where + +import Control.Monad.Error +import Control.Monad.Writer +import Control.Monad.Reader +import Control.Monad.Trans.Peel +import Control.Monad.Trans.Maybe +import Control.Monad.IO.Peel +import Happstack.Server.Internal.Monads +import Happstack.Server.Internal.Types + + +-------------------------------------------------------------------------------- +-- TransPeel instances + +instance MonadTransPeel (FilterT (FilterFun a)) where + peel = return $ \m -> do + (x,w) <- runWriterT $ unFilterT m + return $ FilterT $ do + tell w + return x + +instance MonadTransPeel WebT where + peel = return $ \m -> do + mxew <- runMaybeT $ runWriterT $ unFilterT $ runErrorT $ unWebT m + return $ WebT $ + case mxew of + Just (x,_) -> + case x of + Right a -> return a + Left b -> throwError b + Nothing -> mzero + +runWebT :: WebT m a -> m (Maybe (Either Response a, FilterFun Response)) +runWebT m = + runMaybeT $ runWriterT $ unFilterT $ runErrorT $ unWebT m + +instance MonadTransPeel ServerPartT where + peel = ServerPartT $ asks $ \r m -> do + x <- runWebT $ runReaderT (unServerPartT m) r + return $ + case x of + Just (Right a,_) -> return a + _ -> mzero + + +-------------------------------------------------------------------------------- +-- PeelIO instances + +instance MonadPeelIO m => MonadPeelIO (FilterT (FilterFun a) m) where + peelIO = liftPeel peelIO + +instance MonadPeelIO m => MonadPeelIO (WebT m) where + peelIO = liftPeel peelIO + +instance MonadPeelIO m => MonadPeelIO (ServerPartT m) where + peelIO = liftPeel peelIO