packages feed

web-routes-boomerang (empty) → 0.25.0

raw patch · 4 files changed

+236/−0 lines, 4 filesdep +basedep +boomerangdep +mtlsetup-changed

Dependencies added: base, boomerang, mtl, web-routes

Files

+ LICENSE view
@@ -0,0 +1,32 @@+Copyright (c) 2010, Sjoerd Visscher & Martijn van Steenbergen+Copyright (c)2011, Jeremy Shaw+++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 Jeremy Shaw 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
+ Web/Routes/Boomerang.hs view
@@ -0,0 +1,177 @@+{-# LANGUAGE TypeOperators #-}+{- |+@web-routes-boomerang@ makes it easy to use write custom+pretty-printers and parsers for your URL types. Instead of writing a+parser and a separate pretty-printer you can specify both at once by+using the @boomerang@ library:++<http://hackage.haskell.org/package/boomerang>++This demo will show the basics of getting started.++First we need to enable some language extensions:++@{\-\# LANGUAGE TemplateHaskell, TypeOperators, OverloadedStrings \#-\} @++> {-# LANGUAGE TemplateHaskell, TypeOperators, OverloadedStrings #-}+> module Main where++Note in the imports that we hide @(id, (.))@ from the "Prelude" and+use the versions from "Control.Category" instead.++> import Prelude              hiding (id, (.))+> import Control.Category     (Category(id, (.)))+> import Control.Monad.Trans  (MonadIO(liftIO))+> import Text.Boomerang.TH    (derivePrinterParsers)+> import Web.Routes           (Site(..), RouteT(..), decodePathInfo, encodePathInfo, runSite, showURL)+> import Web.Routes.Boomerang (Router, (<>), (</>), int, parse1, toSiteRouteT, anyString, parseStrings)++Next we define a data type that represents our sitemap.++> -- | the routes+> data Sitemap+>    = Home+>    | UserOverview+>    | UserDetail Int+>    | Article Int String+>    deriving (Eq, Show)+++To use the 'Sitemap' type with @boomerang@ we need to call 'derivePrinterParsers':++> $(derivePrinterParsers ''Sitemap)++That will create new combinators corresponding to the constructors for+'Sitemap'. They will be named, @rHome@, @rUserOverview@, etc.++Now we can specify how the 'Sitemap' type is mapped to a url and back:++> sitemap :: Router Sitemap+> sitemap =+>     (  rHome+>     <> "users" . users+>     <> rArticle . ("article" </> int . "-" . anyString)+>     )+>   where+>     users  =  rUserOverview+>            <> rUserDetail </> int++The mapping looks like this:++@+ \/                       \<=\> Home+ \/users                  \<=\> UserOverview+ \/users\//<int>/            \<=\> UserDetail /<int>/+ \/article\//<int>/-/<string>/ \<=\> Article /<int>/ /<string>/+@++Next we have our function which maps a parsed route to the handler for+that route. (There is nothing @boomerang@ specific about this+function):++> handle :: Sitemap -> RouteT Sitemap IO ()+> handle url =+>     case url of+>       _ -> do liftIO $ print url+>               s <- showURL url+>               liftIO $ putStrLn s++Normally the @case@ statement would match on the different constructors and map them to different handlers. But in this case we use the same handler for all constructors. Also, instead of running in the IO monad, we would typically use a web framework monad like Happstack's 'ServerPartT'.++The handler does two things:++ 1. prints the parsed url++ 2. unparses the url and prints it++We now have two pieces:++ 1. 'sitemap' - which converts urls to the 'Sitemap' type and back++ 2. 'handle' - which maps 'Sitemap' to handlers++We tie these two pieces together use 'toSiteRouteT':++> site :: Site Sitemap (IO ())+> site = boomerangSiteRouteT handle sitemap++This gives as a standard 'Site' value that we can use with 'runSite'+or with framework specific wrappers like @implSite@.++If we were not using 'RouteT' then we could use @boomerangSite@ instead.++Now we can create a simple test function that takes the path info part+of a url and runs our site:++> test :: String -- ^ path info of incoming url+>      -> IO ()+> test path = +>     case runSite "" site path of+>       (Left e)   -> putStrLn e+>       (Right io) -> io++We can use it like this:++@+ghci> test "users/1"+UserDetail 1+users/1+@++Here is a simple wrapper to call test interactively:++> -- | interactively call 'test'+> main :: IO ()+> main = mapM_ test =<< fmap lines getContents+        +Here are two more helper functions you can use to experiment interactively:++> -- | a little function to test rendering a url+> showurl :: Sitemap -> String+> showurl url = +>     let (ps, params) = formatPathSegments site url+>     in (encodePathInfo ps params)++> -- | a little function to test parsing a url+> testParse :: String -> Either String Sitemap+> testParse pathInfo = +>     case parsePathSegments site $ decodePathInfo pathInfo of+>       (Left e)  -> Left (show e)+>       (Right a) -> Right a++-}+module Web.Routes.Boomerang +    ( module Text.Boomerang+    , module Text.Boomerang.Strings+    , Router+    , boomerangSite+    , boomerangSiteRouteT+    ) where++import Text.Boomerang          -- (PrinterParser(..), ParserError(..), (:-), condenseErrors, parse1, showParserError, unparse1)+import Text.Boomerang.Strings  -- (StringsPos(..), isComplete)+import Web.Routes           (RouteT(..), Site(..))++type Router url = PrinterParser StringsError [String] () (url :- ())++boomerangSite :: ((url -> [(String, String)] -> String) -> url -> a) -- ^ handler function+       -> Router url -- ^ the router+       -> Site url a+boomerangSite handler r@(PrinterParser pf sf) =+    Site { handleSite = handler+         , formatPathSegments =  \url ->+             case unparseStrings r url of+               Nothing -> error "formatPathSegments failed to produce a url"+               (Just ps) -> (ps, [])+         , parsePathSegments = \paths -> mapLeft (showErrors paths) (parseStrings r paths)+         }+    where+      mapLeft f       = either (Left . f) Right+      showErrors paths err = (showParserError showPos err) ++ " while parsing " ++ show paths+      showPos (MajorMinorPos s c) = "path segment " ++ show (s + 1) ++ ", character " ++ show c+++boomerangSiteRouteT :: (url -> RouteT url m a) -- ^ handler function+       -> Router url -- ^ the router+       -> Site url (m a)+boomerangSiteRouteT handler router = boomerangSite (flip $ unRouteT . handler) router
+ web-routes-boomerang.cabal view
@@ -0,0 +1,25 @@+Name:             web-routes-boomerang+Version:          0.25.0+License:          BSD3+License-File:     LICENSE+Author:           jeremy@seereason.com+Maintainer:       partners@seereason.com+Bug-Reports:      http://bugzilla.seereason.com/+Category:         Web, Language+Synopsis:         Library for maintaining correctness and composability of URLs within an application.+Description:      This module add support for creating url parsers/printers using a single unified grammar specification+Cabal-Version:    >= 1.6+Build-type:       Simple++Library+        Build-Depends:    base >= 4 && < 5,+                          boomerang,+                          mtl,+                          web-routes+        Exposed-Modules:  Web.Routes.Boomerang++        Extensions:       TypeOperators++source-repository head+    type:     darcs+    location: http://src.seereason.com/web-routes/