{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskellQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE StandaloneDeriving #-}
module Language.JVM.TextSerializable where
import Language.Haskell.TH
import Data.String
import Data.Attoparsec.Text
import qualified Data.Text as Text
import qualified Data.Text.Lazy as Lazy
import Data.Text.Lazy.Builder as Builder
class TextSerializable a where
parseText :: Parser a
toBuilder :: a -> Builder
deserialize :: Text.Text -> Either String a
deserialize = deserializeWith parseText
serialize :: a -> Text.Text
serialize = serializeWith toBuilder
deserializeWith :: Parser a -> Text.Text -> Either String a
deserializeWith p = parseOnly (p <* endOfInput)
serializeWith :: (a -> Builder) -> a -> Text.Text
serializeWith serializer = Lazy.toStrict . Builder.toLazyText . serializer
showViaTextSerializable :: TextSerializable a => a -> String
showViaTextSerializable = show . serialize
{-# INLINE showViaTextSerializable #-}
fromStringViaTextSerializable :: TextSerializable a => String -> a
fromStringViaTextSerializable a =
case deserialize (Text.pack a) of
Right a' -> a'
Left msg -> error $
"While parsing a fromString instance we got this error message: " <> msg
<> "Maybe the string " <> show a <> " is wrongly formatted."
{-# INLINE fromStringViaTextSerializable #-}
deriveFromTextSerializable :: Name -> Q [Dec]
deriveFromTextSerializable name =
concat <$> sequence
[ [d|instance Show ($n) where show = showViaTextSerializable |]
, [d|instance IsString ($n) where fromString = fromStringViaTextSerializable |]
] where n = conT name