packages feed

special-keys 0.1.0.2 → 0.1.0.3

raw patch · 3 files changed

+36/−4 lines, 3 filesPVP: minor bump suggested

API additions: PVP suggests at least a minor version bump

API changes (from Hackage documentation)

+ Keys.UUID: instance FromJSON UUID
+ Keys.UUID: instance ToJSON UUID
+ Keys.UUID: txtUUID :: UUID -> Text

Files

Keys/Random.hs view
@@ -19,7 +19,7 @@  ------------------------------------------------------------------------------ charRangeEnd :: Word8-charRangeEnd = c2w8 'z'+charRangeEnd = c2w8 'Z'  ------------------------------------------------------------------------------ genString :: Gen (PrimState IO) -> IO B.ByteString
Keys/UUID.hs view
@@ -2,8 +2,10 @@ module Keys.UUID where  import Prelude+import Data.Bits+import Data.Char import Data.UUID (fromString, toString, toWords, fromWords)-import qualified Data.UUID as U (UUID, nil)+import qualified Data.UUID as U (UUID(..), nil) import Data.UUID.V4(nextRandom) import Control.Applicative ((<$>)) -- import Data.Aeson.Types (FromJSON(..), ToJSON(..), typeMismatch)@@ -12,13 +14,18 @@ -- import Control.Monad(mzero) import Data.Data import Data.Hashable-import Data.Text (unpack, pack)+import Control.Monad+import Data.Text (unpack, pack, Text)+import qualified Data.Text as T import Keys.Random import Data.SafeCopy import Data.Serialize import Web.PathPieces import Foreign.Storable import Text.Blaze+import Data.Word+import Data.Aeson+import qualified Data.Aeson as Aeson  newtype UUID = UUID U.UUID     deriving(Ord, Eq, Data, Typeable, Storable)@@ -34,6 +41,23 @@ strUUID :: UUID -> String strUUID (UUID u) = toString u +txtUUID :: UUID -> Text+txtUUID (UUID u) = uuidToText (toWords u) +    where+        uuidToText :: (Word32, Word32, Word32, Word32) -> Text+        uuidToText (w0, w1, w2, w3) = hexw w0 $ hexw' w1 $ hexw' w2 $ hexw w3 T.empty+        hexw :: Word32 -> Text -> Text+        hexw  w s = hexn w 28 `T.cons` hexn w 24 `T.cons` hexn w 20 `T.cons` hexn w 16+                  `T.cons` hexn w 12 `T.cons` hexn w  8 `T.cons` hexn w  4 `T.cons` hexn w  0 `T.cons` s++        hexw' :: Word32 -> Text -> Text +        hexw' w s = '-' `T.cons` hexn w 28 `T.cons` hexn w 24 `T.cons` hexn w 20 `T.cons` hexn w 16+                    `T.cons` '-' `T.cons` hexn w 12 `T.cons` hexn w  8 `T.cons` hexn w  4 `T.cons` hexn w  0 `T.cons` s++        hexn :: Word32 -> Int -> Char+        hexn w r = intToDigit $ fromIntegral ((w `shiftR` r) .&. 0xf)+        + nil :: UUID nil = UUID U.nil @@ -46,6 +70,14 @@  instance ToMarkup UUID where     toMarkup u = toMarkup $ pack $ strUUID u++instance ToJSON UUID where+    toJSON u = Aeson.String (txtUUID u)+instance FromJSON UUID where+    parseJSON (Aeson.String s) = case fromString (T.unpack s) of+                                    Just u -> return $ UUID u+                                    Nothing -> mzero+    parseJSON _ = mzero  {- these can be useful in postgresql-simple: instance FromField UUID where
special-keys.cabal view
@@ -1,5 +1,5 @@ name:                special-keys-version:             0.1.0.2+version:             0.1.0.3 synopsis:            Simple data types that help me here and there.  description:         Datatypes that I use as keys to index other data types. Here you will find a wrap of UUID and also constraints on Text so that it never exceeds a given number of characters. license:             BSD3