data-msgpack 0.0.2 → 0.0.3
raw patch · 2 files changed
+73/−3 lines, 2 filesdep +groomnew-component:exe:msgpack-parser
Dependencies added: groom
Files
- data-msgpack.cabal +22/−3
- tools/msgpack-parser.hs +51/−0
data-msgpack.cabal view
@@ -1,7 +1,6 @@ name: data-msgpack-version: 0.0.2+version: 0.0.3 synopsis: A Haskell implementation of MessagePack-description: A Haskell implementation of MessagePack <http://msgpack.org/> homepage: http://msgpack.org/ license: BSD3 license-file: LICENSE@@ -12,10 +11,16 @@ stability: Experimental cabal-version: >= 1.10 build-type: Simple+description:+ A Haskell implementation of MessagePack <http://msgpack.org/>+ .+ This is a fork of msgpack-haskell <https://github.com/msgpack/msgpack-haskell>,+ since the original author is unreachable. This fork incorporates a number of+ bugfixes and is actively being developed. source-repository head type: git- location: https://github.com/TokTok/msgpack-haskell.git+ location: https://github.com/TokTok/hs-msgpack.git library default-language: Haskell2010@@ -41,6 +46,20 @@ , hashable , text , unordered-containers++executable msgpack-parser+ default-language: Haskell2010+ hs-source-dirs:+ tools+ ghc-options:+ -Wall+ -fno-warn-unused-imports+ build-depends:+ base < 5+ , bytestring+ , groom+ , data-msgpack+ main-is: msgpack-parser.hs test-suite testsuite type: exitcode-stdio-1.0
+ tools/msgpack-parser.hs view
@@ -0,0 +1,51 @@+{-# LANGUAGE Trustworthy #-}+-- | A MessagePack parser.+--+-- Example usage:+-- $ echo -ne "\x94\x01\xa1\x32\xa1\x33\xa4\x50\x6f\x6f\x66" | ./msgpack-parser+-- or+-- $ echo 'ObjectArray [ObjectInt 97, ObjectStr "test", ObjectBool True]' | ./msgpack-parser+--+-- This tool performs two symmetrical functions:+-- 1. It can decode binary data representing a+-- Data.MessagePack.Object into a human-readable string.+-- 2. It can do the reverse: encode a human-readable string into+-- a binary representation of Data.MessagePack.Object.+--+-- No flags are required as it automatically detects which of these+-- two functions it should perform. This is done by first assuming+-- the input is human readable. If it fails to parse it, it then+-- considers it as binary data.+--+-- Therefore, given a valid input, the tool has the following property+-- $ ./msgpack-parser < input.bin | ./msgpack-parser+-- will output back the contents of input.bin.+--+-- In case the input is impossible to parse, nothing is output.+--+-- Known bugs:+-- - If no input is given, the tool exits with+-- "Data.Binary.Get.runGet at position 0: not enough bytes"+-- - The tool does not check that all the input is parsed.+-- Therefore, "abc" is interpreted as just "ObjectInt 97".+--+module Main where++import Control.Applicative ((<$>), (<|>))+import qualified Data.ByteString.Lazy as L+import qualified Data.ByteString.Lazy.Char8 as L8+import Data.Maybe (fromMaybe)+import Data.MessagePack (Object, pack, unpack)+import Text.Groom (groom)+import Text.Read (readMaybe)+++parse :: L.ByteString -> L.ByteString+parse str = fromMaybe L.empty $+ pack <$> (readMaybe $ L8.unpack str :: Maybe Object)+ <|>+ L8.pack . flip (++) "\n" . groom <$> (unpack str :: Maybe Object)+++main :: IO ()+main = parse <$> L.getContents >>= L.putStr