summaryrefslogtreecommitdiff
path: root/lib/Util.hs
diff options
context:
space:
mode:
authorstuebinm2021-09-18 00:27:22 +0200
committerstuebinm2021-09-18 00:27:22 +0200
commitb17396b2eeefdf113b862b254cb152557bebf68d (patch)
tree09ab8776b87a7c193d08144b3d40ecd4f249f11e /lib/Util.hs
parentbfe45dc4996537b72436f4041d0ca819aa3444e1 (diff)
tame the strings
Adds a PrettyPrint typeclass which operates on Text and should replace Show, since constantly converting strings from linked lists to arrays seems somewhat silly.
Diffstat (limited to 'lib/Util.hs')
-rw-r--r--lib/Util.hs38
1 files changed, 24 insertions, 14 deletions
diff --git a/lib/Util.hs b/lib/Util.hs
index be67143..3a0e1d4 100644
--- a/lib/Util.hs
+++ b/lib/Util.hs
@@ -1,27 +1,37 @@
+{-# 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.Text (Text)
-import Data.Text as T
-import Data.Aeson as Aeson
+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
--- | same as showText, but without the "String"-prefix for strings
--- TODO: serialise back into json for printing? People may get
--- confused by the type annotations if they only know json …
-showAeson :: Aeson.Value -> Text
-showAeson (Aeson.String s) = showText s
-showAeson v = showText v
-
+-- | 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"
--- | adds quotes (but does not escape, for now!)
-quote :: Text -> Text
-quote text = "\"" <> text <> "\""
+printPretty :: PrettyPrint a => a -> IO ()
+printPretty = putStr . T.unpack . prettyprint