{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE OverloadedStrings #-} -- | has (perhaps inevitably) morphed into a module that mostly -- concerns itself with wrangling haskell's string types module Util where import Data.Aeson as Aeson import Data.Text (Text) import Data.Text as T -- | haskell's many string types are FUN … showText :: Show a => a -> Text showText = T.pack . show -- | a class to address all the string conversions necessary -- when using Show to much that just uses Text instead class PrettyPrint a where prettyprint :: a -> Text -- | let's see if this is a good idea or makes type inference bite us instance PrettyPrint Text where prettyprint text = "\"" <> text <> "\"" -- | same as show json, but without the "String" prefix for json strings instance PrettyPrint Aeson.Value where prettyprint = \case Aeson.String s -> prettyprint s v -> (T.pack . show) v -- | here since Unit is sometimes used as dummy type instance PrettyPrint () where prettyprint _ = error "shouldn't pretty-print Unit" printPretty :: PrettyPrint a => a -> IO () printPretty = putStr . T.unpack . prettyprint