Permalink
Browse files

Initial release

  • Loading branch information...
1 parent 374ae8d commit 4a40b56b34fc228933ec7cfdeac790270bbd2bb3 @mightybyte committed Aug 20, 2012
Showing with 130 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +28 −0 LICENSE
  3. +3 −0 Setup.hs
  4. +40 −0 readable.cabal
  5. +58 −0 src/Data/Readable.hs
View
@@ -4,3 +4,4 @@ cabal-dev
*.hi
*.chi
*.chs.h
+*.swp
View
28 LICENSE
@@ -0,0 +1,28 @@
+Copyright (c) 2009, Snap Framework authors (see CONTRIBUTORS)
+Copyright (c) 2010, Google, Inc.
+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 the Snap Framework authors nor the names of its
+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 HOLDER 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.
View
@@ -0,0 +1,3 @@
+import Distribution.Simple
+
+main = defaultMain
View
@@ -0,0 +1,40 @@
+name: readable
+version: 0.1
+synopsis: Read for the modern world
+
+description:
+ Provides a Readable type class for reading data types from ByteString and
+ Text.
+
+license: BSD3
+license-file: LICENSE
+author: Doug Beardsley
+maintainer: mightybyte@gmail.com
+build-type: Simple
+cabal-version: >= 1.6
+homepage: https://github.com/mightybyte/readable
+category: Text
+
+extra-source-files:
+ LICENSE,
+ README.md,
+ Setup.hs
+
+Library
+ hs-source-dirs: src
+
+ exposed-modules:
+ Data.Readable
+
+ build-depends:
+ base >= 4 && < 5,
+ bytestring >= 0.9 && < 0.10,
+ text >= 0.11 && < 0.12
+
+ ghc-prof-options: -prof -auto-all
+
+ ghc-options: -Wall -fwarn-tabs -O
+
+source-repository head
+ type: git
+ location: git://github.com/mightybyte/readable.git
View
@@ -0,0 +1,58 @@
+module Data.Readable
+ ( Readable(..)
+ ) where
+
+------------------------------------------------------------------------------
+import Data.ByteString.Char8 (ByteString)
+import Data.Int
+import Data.Text (Text)
+import qualified Data.Text as T
+import Data.Text.Encoding
+import Data.Text.Read
+import Data.Word
+
+
+------------------------------------------------------------------------------
+-- | Monadic analog to Read that uses ByteString instead of String.
+class Readable a where
+ fromText :: Monad m => Text -> m a
+ fromBS :: Monad m => ByteString -> m a
+ fromBS = fromText . decodeUtf8
+
+
+------------------------------------------------------------------------------
+-- | Fails if the input wasn't parsed completely.
+checkComplete :: Monad m => (t, Text) -> m t
+checkComplete (a,rest)
+ | T.null rest = return a
+ | otherwise = fail "Readable: could not parse completely"
+
+
+instance Readable ByteString where
+ fromText = return . encodeUtf8
+instance Readable Text where
+ fromText = return
+instance Readable Int where
+ fromText = either fail checkComplete . decimal
+instance Readable Integer where
+ fromText = either fail checkComplete . decimal
+instance Readable Double where
+ fromText = either fail checkComplete . double
+
+instance Readable Int8 where
+ fromText = either fail checkComplete . decimal
+instance Readable Int16 where
+ fromText = either fail checkComplete . decimal
+instance Readable Int32 where
+ fromText = either fail checkComplete . decimal
+instance Readable Int64 where
+ fromText = either fail checkComplete . decimal
+
+instance Readable Word8 where
+ fromText = either fail checkComplete . decimal
+instance Readable Word16 where
+ fromText = either fail checkComplete . decimal
+instance Readable Word32 where
+ fromText = either fail checkComplete . decimal
+instance Readable Word64 where
+ fromText = either fail checkComplete . decimal

0 comments on commit 4a40b56

Please sign in to comment.