conjure-0.1: src/BEncode/BEncodePP.hs
{-
Copyright (c) 2005 Jesper Louis Andersen <jlouis@mongers.org>
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-}
-----------------------------------------------------------------------------
-- |
-- Module : BEncode.BEncodePP
-- Copyright : (c) Jesper Louis Andersen, 2005
-- License : BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer : jlouis@mongers.org
-- Stability : believed to be stable
-- Portability : portable
--
-- This module provides Pretty-printing of BEncoded strings
-----------------------------------------------------------------------------
module BEncode.BEncodePP
(
pp
)
where
import BEncode.BEncode
import qualified Data.Map
import Text.PrettyPrint
-- | Convert a BEncoded AST into a Document of the type we can
-- pretty-print
ppBEncode :: BEncode -> Doc
ppBEncode benc =
case benc of
BInt i -> parens $ (text "INT") <> (brackets $ text $ show i)
BString str -> parens $ (text "STRING") <> (brackets $ text $ show str)
BList blst -> brackets . hsep $ map ppBEncode blst
BDict mp ->
braces $ vcat $
map (\ (k, v) -> parens $ text k <> comma <+> ppBEncode v)
(Data.Map.toList mp)
-- | Render a BEncoded AST into a (humanly readable) String.
pp :: BEncode -> String
pp = render . ppBEncode