Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lpeterse committed Apr 27, 2011
0 parents commit 77dcb07
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
Empty file added CHANGES
Empty file.
2 changes: 2 additions & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Antoine Latter
Lars Petersen
52 changes: 52 additions & 0 deletions Data/UUID/Quasi.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-- |
-- Module : Data.UUID.Quasi
-- Copyright : (c) 2011 Lars Petersen
--
-- License : BSD-style
--
-- Maintainer : info@lars-petersen.net
-- Stability : experimental
-- Portability : portable
--
--
-- This library supplies quasiquotation of UUIDs. You should use this in
-- case you want to hardcode UUIDs in your sourcecode with compile-time
-- checking for syntax errors.

module Data.UUID.Quasi (uuid) where

import Data.UUID

import Language.Haskell.TH.Quote
import Language.Haskell.TH.Syntax

import Data.Char
import Data.List hiding (null)
import Data.List.Split

quoteExpr :: (Name -> [Lit] -> a) -> String -> Q a
quoteExpr con x | length x == 32
&& all isHexDigit x
= t [a,b,c,d]
| length x == 36
&& elemIndices '-' x == [8,13,18,23]
&& all isHexDigit (filter (/='-') x)
= t [e, f++g, h++(take 4 i), drop 4 i]
| otherwise
= fail "a UUID must consist of 32 hexdigits optionally interspersed by 4 '-'"
where
[a,b,c,d] = splitPlaces [8 :: Int,8,8,8] x
[e,_,f,_,g,_,h,_,i] = splitPlaces [8 :: Int,1,4,1,4,1,4,1,12] x
z = IntegerL . read . ('0':) . ('x':)
t = return . con (mkName "UUID") . map z

-- | The quasiquoter for expressions and patterns of 'UUID'. Make sure to enable '-XQuasiQuotes'.
-- > > let a = [uuid|550e8400-e29b-41d4-a716-446655440000|]
-- > > case a of { [uuid|550e8400-e29b-41d4-a716-446655440000|] -> True; _ -> False; }
-- > True
uuid :: QuasiQuoter
uuid = QuasiQuoter
(quoteExpr $ \x-> foldl AppE (ConE x) . map LitE)
(quoteExpr $ \x-> ConP x . map LitP)
(const $ fail "undefined QuasiQuoter for Type")
(const $ fail "undefined QuasiQuoter for Declaration")
28 changes: 28 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Copyright (c) 2008, Antoine Latter

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.
* The names of the authors may not 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.
4 changes: 4 additions & 0 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Distribution.Simple
main :: IO ()
main = defaultMain

28 changes: 28 additions & 0 deletions uuid-quasi.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Name: uuid-quasi
Version: 0.1
Copyright: (c) 2011 Lars Petersen
Author: Lars Petersen
Maintainer: info@lars-petersen.net
License: BSD3
License-file: LICENSE

Category: Data
Build-Type: Simple
Cabal-Version: >= 1.6

Description:
This library is allows quasiquotation (expressions and patterns) of UUIDs.

Homepage: http://github.com/lpeterse/uuid-quasi
Bug-Reports: mailto:info@lars-petersen.net

Extra-Source-Files:
CHANGES
CONTRIBUTORS

Library
Build-Depends: uuid, template-haskell, split, base >=3, base < 5
Exposed-Modules: Data.UUID.Quasi

Ghc-Prof-Options: -auto-all -caf-all
Ghc-Options: -Wall

0 comments on commit 77dcb07

Please sign in to comment.