{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_HADDOCK prune #-}
module Core.Encoding.External (
Externalize (formatExternal, parseExternal),
) where
import Core.Data.Clock
import Core.Text.Rope
import Data.ByteString.Builder qualified as Builder
import Data.Int (Int32, Int64)
import Data.Scientific (FPFormat (Exponent), Scientific, formatScientific)
import Data.UUID qualified as Uuid (UUID, fromText, toText)
import Text.Read (readMaybe)
class Externalize ξ where
formatExternal :: ξ -> Rope
parseExternal :: Rope -> Maybe ξ
instance {-# OVERLAPPABLE #-} (Read a, Show a) => Externalize a where
formatExternal :: a -> Rope
formatExternal = String -> Rope
forall α. Textual α => α -> Rope
intoRope (String -> Rope) -> (a -> String) -> a -> Rope
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. Show a => a -> String
show
parseExternal :: Rope -> Maybe a
parseExternal = String -> Maybe a
forall a. Read a => String -> Maybe a
readMaybe (String -> Maybe a) -> (Rope -> String) -> Rope -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rope -> String
forall α. Textual α => Rope -> α
fromRope
instance Externalize Rope where
formatExternal :: Rope -> Rope
formatExternal = Rope -> Rope
forall a. a -> a
id
parseExternal :: Rope -> Maybe Rope
parseExternal = Rope -> Maybe Rope
forall a. a -> Maybe a
Just
instance Externalize String where
formatExternal :: String -> Rope
formatExternal = String -> Rope
packRope
parseExternal :: Rope -> Maybe String
parseExternal = String -> Maybe String
forall a. a -> Maybe a
Just (String -> Maybe String)
-> (Rope -> String) -> Rope -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rope -> String
forall α. Textual α => Rope -> α
fromRope
instance Externalize Int where
formatExternal :: Int -> Rope
formatExternal = ByteString -> Rope
forall α. Textual α => α -> Rope
intoRope (ByteString -> Rope) -> (Int -> ByteString) -> Int -> Rope
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Builder.toLazyByteString (Builder -> ByteString) -> (Int -> Builder) -> Int -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Builder
Builder.intDec
parseExternal :: Rope -> Maybe Int
parseExternal = String -> Maybe Int
forall a. Read a => String -> Maybe a
readMaybe (String -> Maybe Int) -> (Rope -> String) -> Rope -> Maybe Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rope -> String
forall α. Textual α => Rope -> α
fromRope
instance Externalize Int32 where
formatExternal :: Int32 -> Rope
formatExternal = ByteString -> Rope
forall α. Textual α => α -> Rope
intoRope (ByteString -> Rope) -> (Int32 -> ByteString) -> Int32 -> Rope
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Builder.toLazyByteString (Builder -> ByteString)
-> (Int32 -> Builder) -> Int32 -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Builder
Builder.int32Dec
parseExternal :: Rope -> Maybe Int32
parseExternal = String -> Maybe Int32
forall a. Read a => String -> Maybe a
readMaybe (String -> Maybe Int32) -> (Rope -> String) -> Rope -> Maybe Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rope -> String
forall α. Textual α => Rope -> α
fromRope
instance Externalize Int64 where
formatExternal :: Int64 -> Rope
formatExternal = ByteString -> Rope
forall α. Textual α => α -> Rope
intoRope (ByteString -> Rope) -> (Int64 -> ByteString) -> Int64 -> Rope
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Builder.toLazyByteString (Builder -> ByteString)
-> (Int64 -> Builder) -> Int64 -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Builder
Builder.int64Dec
parseExternal :: Rope -> Maybe Int64
parseExternal = String -> Maybe Int64
forall a. Read a => String -> Maybe a
readMaybe (String -> Maybe Int64) -> (Rope -> String) -> Rope -> Maybe Int64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rope -> String
forall α. Textual α => Rope -> α
fromRope
instance Externalize Float where
formatExternal :: Float -> Rope
formatExternal = ByteString -> Rope
forall α. Textual α => α -> Rope
intoRope (ByteString -> Rope) -> (Float -> ByteString) -> Float -> Rope
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Builder.toLazyByteString (Builder -> ByteString)
-> (Float -> Builder) -> Float -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Builder
Builder.floatDec
parseExternal :: Rope -> Maybe Float
parseExternal = String -> Maybe Float
forall a. Read a => String -> Maybe a
readMaybe (String -> Maybe Float) -> (Rope -> String) -> Rope -> Maybe Float
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rope -> String
forall α. Textual α => Rope -> α
fromRope
instance Externalize Double where
formatExternal :: Double -> Rope
formatExternal = ByteString -> Rope
forall α. Textual α => α -> Rope
intoRope (ByteString -> Rope) -> (Double -> ByteString) -> Double -> Rope
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Builder.toLazyByteString (Builder -> ByteString)
-> (Double -> Builder) -> Double -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Builder
Builder.doubleDec
parseExternal :: Rope -> Maybe Double
parseExternal = String -> Maybe Double
forall a. Read a => String -> Maybe a
readMaybe (String -> Maybe Double)
-> (Rope -> String) -> Rope -> Maybe Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rope -> String
forall α. Textual α => Rope -> α
fromRope
instance Externalize Uuid.UUID where
formatExternal :: UUID -> Rope
formatExternal = Text -> Rope
forall α. Textual α => α -> Rope
intoRope (Text -> Rope) -> (UUID -> Text) -> UUID -> Rope
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UUID -> Text
Uuid.toText
parseExternal :: Rope -> Maybe UUID
parseExternal = Text -> Maybe UUID
Uuid.fromText (Text -> Maybe UUID) -> (Rope -> Text) -> Rope -> Maybe UUID
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rope -> Text
forall α. Textual α => Rope -> α
fromRope
instance Externalize Time where
formatExternal :: Time -> Rope
formatExternal = String -> Rope
forall α. Textual α => α -> Rope
intoRope (String -> Rope) -> (Time -> String) -> Time -> Rope
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Time -> String
forall a. Show a => a -> String
show
parseExternal :: Rope -> Maybe Time
parseExternal = String -> Maybe Time
forall a. Read a => String -> Maybe a
readMaybe (String -> Maybe Time) -> (Rope -> String) -> Rope -> Maybe Time
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rope -> String
forall α. Textual α => Rope -> α
fromRope
instance Externalize Scientific where
formatExternal :: Scientific -> Rope
formatExternal = String -> Rope
forall α. Textual α => α -> Rope
intoRope (String -> Rope) -> (Scientific -> String) -> Scientific -> Rope
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FPFormat -> Maybe Int -> Scientific -> String
formatScientific FPFormat
Exponent Maybe Int
forall a. Maybe a
Nothing
parseExternal :: Rope -> Maybe Scientific
parseExternal = String -> Maybe Scientific
forall a. Read a => String -> Maybe a
readMaybe (String -> Maybe Scientific)
-> (Rope -> String) -> Rope -> Maybe Scientific
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rope -> String
forall α. Textual α => Rope -> α
fromRope