{-# 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 (Int16, Int32, Int64, Int8)
import Data.Scientific (FPFormat (Exponent), Scientific, formatScientific)
import Data.Time.Calendar qualified as Base (Day)
import Data.Time.Format.ISO8601 qualified as Base (formatParseM, formatShow, iso8601Format)
import Data.UUID qualified as Uuid (UUID, fromText, toText)
import Data.Word (Word16, Word32, Word64, Word8)
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 = forall α. Textual α => α -> Rope
intoRope forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show
parseExternal :: Rope -> Maybe a
parseExternal = forall a. Read a => String -> Maybe a
readMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall α. Textual α => Rope -> α
fromRope
instance Externalize Rope where
formatExternal :: Rope -> Rope
formatExternal = forall a. a -> a
id
parseExternal :: Rope -> Maybe Rope
parseExternal = forall a. a -> Maybe a
Just
instance Externalize String where
formatExternal :: String -> Rope
formatExternal = String -> Rope
packRope
parseExternal :: Rope -> Maybe String
parseExternal = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall α. Textual α => Rope -> α
fromRope
instance Externalize Int where
formatExternal :: Int -> Rope
formatExternal = forall α. Textual α => α -> Rope
intoRope forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Builder.toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Builder
Builder.intDec
parseExternal :: Rope -> Maybe Int
parseExternal = forall a. Read a => String -> Maybe a
readMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall α. Textual α => Rope -> α
fromRope
instance Externalize Int8 where
formatExternal :: Int8 -> Rope
formatExternal = forall α. Textual α => α -> Rope
intoRope forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Builder.toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int8 -> Builder
Builder.int8Dec
parseExternal :: Rope -> Maybe Int8
parseExternal = forall a. Read a => String -> Maybe a
readMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall α. Textual α => Rope -> α
fromRope
instance Externalize Int16 where
formatExternal :: Int16 -> Rope
formatExternal = forall α. Textual α => α -> Rope
intoRope forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Builder.toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int16 -> Builder
Builder.int16Dec
parseExternal :: Rope -> Maybe Int16
parseExternal = forall a. Read a => String -> Maybe a
readMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall α. Textual α => Rope -> α
fromRope
instance Externalize Int32 where
formatExternal :: Int32 -> Rope
formatExternal = forall α. Textual α => α -> Rope
intoRope forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Builder.toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Builder
Builder.int32Dec
parseExternal :: Rope -> Maybe Int32
parseExternal = forall a. Read a => String -> Maybe a
readMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall α. Textual α => Rope -> α
fromRope
instance Externalize Int64 where
formatExternal :: Int64 -> Rope
formatExternal = forall α. Textual α => α -> Rope
intoRope forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Builder.toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Builder
Builder.int64Dec
parseExternal :: Rope -> Maybe Int64
parseExternal = forall a. Read a => String -> Maybe a
readMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall α. Textual α => Rope -> α
fromRope
instance Externalize Word8 where
formatExternal :: Word8 -> Rope
formatExternal = forall α. Textual α => α -> Rope
intoRope forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Builder.toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Builder
Builder.word8Dec
parseExternal :: Rope -> Maybe Word8
parseExternal = forall a. Read a => String -> Maybe a
readMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall α. Textual α => Rope -> α
fromRope
instance Externalize Word16 where
formatExternal :: Word16 -> Rope
formatExternal = forall α. Textual α => α -> Rope
intoRope forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Builder.toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> Builder
Builder.word16Dec
parseExternal :: Rope -> Maybe Word16
parseExternal = forall a. Read a => String -> Maybe a
readMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall α. Textual α => Rope -> α
fromRope
instance Externalize Word32 where
formatExternal :: Word32 -> Rope
formatExternal = forall α. Textual α => α -> Rope
intoRope forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Builder.toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> Builder
Builder.word32Dec
parseExternal :: Rope -> Maybe Word32
parseExternal = forall a. Read a => String -> Maybe a
readMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall α. Textual α => Rope -> α
fromRope
instance Externalize Word64 where
formatExternal :: Word64 -> Rope
formatExternal = forall α. Textual α => α -> Rope
intoRope forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Builder.toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Builder
Builder.word64Dec
parseExternal :: Rope -> Maybe Word64
parseExternal = forall a. Read a => String -> Maybe a
readMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall α. Textual α => Rope -> α
fromRope
instance Externalize Float where
formatExternal :: Float -> Rope
formatExternal = forall α. Textual α => α -> Rope
intoRope forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Builder.toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Builder
Builder.floatDec
parseExternal :: Rope -> Maybe Float
parseExternal = forall a. Read a => String -> Maybe a
readMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall α. Textual α => Rope -> α
fromRope
instance Externalize Double where
formatExternal :: Double -> Rope
formatExternal = forall α. Textual α => α -> Rope
intoRope forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
Builder.toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Builder
Builder.doubleDec
parseExternal :: Rope -> Maybe Double
parseExternal = forall a. Read a => String -> Maybe a
readMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall α. Textual α => Rope -> α
fromRope
instance Externalize Uuid.UUID where
formatExternal :: UUID -> Rope
formatExternal = forall α. Textual α => α -> Rope
intoRope forall b c a. (b -> c) -> (a -> b) -> a -> c
. UUID -> Text
Uuid.toText
parseExternal :: Rope -> Maybe UUID
parseExternal = Text -> Maybe UUID
Uuid.fromText forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall α. Textual α => Rope -> α
fromRope
instance Externalize Time where
formatExternal :: Time -> Rope
formatExternal = forall α. Textual α => α -> Rope
intoRope forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show
parseExternal :: Rope -> Maybe Time
parseExternal = forall a. Read a => String -> Maybe a
readMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall α. Textual α => Rope -> α
fromRope
instance Externalize Base.Day where
formatExternal :: Day -> Rope
formatExternal = forall α. Textual α => α -> Rope
intoRope forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. Format t -> t -> String
Base.formatShow forall t. ISO8601 t => Format t
Base.iso8601Format
parseExternal :: Rope -> Maybe Day
parseExternal = forall (m :: * -> *) t. MonadFail m => Format t -> String -> m t
Base.formatParseM forall t. ISO8601 t => Format t
Base.iso8601Format forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall α. Textual α => Rope -> α
fromRope
instance Externalize Scientific where
formatExternal :: Scientific -> Rope
formatExternal = forall α. Textual α => α -> Rope
intoRope forall b c a. (b -> c) -> (a -> b) -> a -> c
. FPFormat -> Maybe Int -> Scientific -> String
formatScientific FPFormat
Exponent forall a. Maybe a
Nothing
parseExternal :: Rope -> Maybe Scientific
parseExternal = forall a. Read a => String -> Maybe a
readMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall α. Textual α => Rope -> α
fromRope