-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a QuasiQuoter for UUID literals
- Loading branch information
1 parent
d1cedd8
commit a0bce93
Showing
4 changed files
with
61 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
module Data.UUID.TH (uuid) where | ||
|
||
import Data.Maybe (fromMaybe) | ||
import Data.UUID (fromString, fromWords, toWords) | ||
import Language.Haskell.TH.Quote | ||
import Language.Haskell.TH.Syntax | ||
|
||
uuid :: QuasiQuoter | ||
uuid = QuasiQuoter | ||
{ quoteExp = uuidExp | ||
, quotePat = \_ -> fail "illegal UUID QuasiQuote (allowed as expression only, used as a pattern)" | ||
, quoteType = \_ -> fail "illegal UUID QuasiQuote (allowed as expression only, used as a type)" | ||
, quoteDec = \_ -> fail "illegal UUID QuasiQuote (allowed as expression only, used as a declaration)" | ||
} | ||
|
||
uuidExp :: String -> Q Exp | ||
uuidExp uuidStr = | ||
return $ AppE (AppE (AppE (AppE (VarE 'fromWords) w1e) w2e) w3e) w4e | ||
|
||
where | ||
(w1, w2, w3, w4) = toWords parsedUUID | ||
wordExp = LitE . IntegerL . fromIntegral | ||
w1e = wordExp w1 | ||
w2e = wordExp w2 | ||
w3e = wordExp w3 | ||
w4e = wordExp w4 | ||
parsedUUID = fromMaybe (error errmsg) $ fromString uuidStr | ||
errmsg = "'" ++ uuidStr ++ "' is not a valid UUID" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters