{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module PyF.Class where
import qualified Data.ByteString
import qualified Data.ByteString.Lazy
import Data.Char (ord)
import Data.Int
import qualified Data.Ratio
import qualified Data.Text as SText
import qualified Data.Text.Encoding as E
import qualified Data.Text.Lazy as LText
import qualified Data.Time
import Data.Word
import Numeric.Natural
import PyF.Formatters
data PyFCategory
=
PyFIntegral
|
PyFFractional
|
PyFString
type family PyFClassify t :: PyFCategory
type instance PyFClassify Integer = 'PyFIntegral
type instance PyFClassify Int = 'PyFIntegral
type instance PyFClassify Int8 = 'PyFIntegral
type instance PyFClassify Int16 = 'PyFIntegral
type instance PyFClassify Int32 = 'PyFIntegral
type instance PyFClassify Int64 = 'PyFIntegral
type instance PyFClassify Natural = 'PyFIntegral
type instance PyFClassify Word = 'PyFIntegral
type instance PyFClassify Word8 = 'PyFIntegral
type instance PyFClassify Word16 = 'PyFIntegral
type instance PyFClassify Word32 = 'PyFIntegral
type instance PyFClassify Word64 = 'PyFIntegral
type instance PyFClassify Float = 'PyFFractional
type instance PyFClassify Double = 'PyFFractional
type instance PyFClassify Data.Time.DiffTime = 'PyFFractional
type instance PyFClassify Data.Time.NominalDiffTime = 'PyFFractional
type instance PyFClassify (Data.Ratio.Ratio i) = 'PyFFractional
type instance PyFClassify String = 'PyFString
type instance PyFClassify LText.Text = 'PyFString
type instance PyFClassify SText.Text = 'PyFString
type instance PyFClassify Data.ByteString.ByteString = 'PyFString
type instance PyFClassify Data.ByteString.Lazy.ByteString = 'PyFString
type instance PyFClassify Char = 'PyFString
class PyFToString t where
pyfToString :: t -> String
instance PyFToString String where pyfToString :: String -> String
pyfToString = String -> String
forall a. a -> a
id
instance PyFToString LText.Text where pyfToString :: Text -> String
pyfToString = Text -> String
LText.unpack
instance PyFToString SText.Text where pyfToString :: Text -> String
pyfToString = Text -> String
SText.unpack
instance PyFToString Data.ByteString.ByteString where pyfToString :: ByteString -> String
pyfToString = Text -> String
SText.unpack (Text -> String) -> (ByteString -> Text) -> ByteString -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
E.decodeUtf8
instance PyFToString Data.ByteString.Lazy.ByteString where pyfToString :: ByteString -> String
pyfToString = ByteString -> String
forall t. PyFToString t => t -> String
pyfToString (ByteString -> String)
-> (ByteString -> ByteString) -> ByteString -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
Data.ByteString.Lazy.toStrict
instance PyFToString Char where pyfToString :: Char -> String
pyfToString Char
c = [Char
c]
instance {-# OVERLAPPABLE #-} Show t => PyFToString t where pyfToString :: t -> String
pyfToString = t -> String
forall t. Show t => t -> String
show
class PyfFormatFractional a where
pyfFormatFractional ::
(Integral paddingWidth, Integral precision) =>
Format t t' 'Fractional ->
SignMode ->
Maybe (paddingWidth, AlignMode k, Char) ->
Maybe (Int, Char) ->
Maybe precision ->
a ->
String
instance {-# OVERLAPPABLE #-} Real t => PyfFormatFractional t where
pyfFormatFractional :: Format t t' 'Fractional
-> SignMode
-> Maybe (paddingWidth, AlignMode k, Char)
-> Maybe (Int, Char)
-> Maybe precision
-> t
-> String
pyfFormatFractional Format t t' 'Fractional
f SignMode
s Maybe (paddingWidth, AlignMode k, Char)
p Maybe (Int, Char)
g Maybe precision
prec t
v = Format t t' 'Fractional
-> SignMode
-> Maybe (paddingWidth, AlignMode k, Char)
-> Maybe (Int, Char)
-> Maybe precision
-> Double
-> String
forall f paddingWidth precision (t :: AltStatus)
(t' :: UpperStatus) (k :: AlignForString).
(RealFloat f, Integral paddingWidth, Integral precision) =>
Format t t' 'Fractional
-> SignMode
-> Maybe (paddingWidth, AlignMode k, Char)
-> Maybe (Int, Char)
-> Maybe precision
-> f
-> String
formatFractional Format t t' 'Fractional
f SignMode
s Maybe (paddingWidth, AlignMode k, Char)
p Maybe (Int, Char)
g Maybe precision
prec (t -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac @t @Double t
v)
instance PyfFormatFractional Double where pyfFormatFractional :: Format t t' 'Fractional
-> SignMode
-> Maybe (paddingWidth, AlignMode k, Char)
-> Maybe (Int, Char)
-> Maybe precision
-> Double
-> String
pyfFormatFractional = Format t t' 'Fractional
-> SignMode
-> Maybe (paddingWidth, AlignMode k, Char)
-> Maybe (Int, Char)
-> Maybe precision
-> Double
-> String
forall f paddingWidth precision (t :: AltStatus)
(t' :: UpperStatus) (k :: AlignForString).
(RealFloat f, Integral paddingWidth, Integral precision) =>
Format t t' 'Fractional
-> SignMode
-> Maybe (paddingWidth, AlignMode k, Char)
-> Maybe (Int, Char)
-> Maybe precision
-> f
-> String
formatFractional
instance PyfFormatFractional Float where pyfFormatFractional :: Format t t' 'Fractional
-> SignMode
-> Maybe (paddingWidth, AlignMode k, Char)
-> Maybe (Int, Char)
-> Maybe precision
-> Float
-> String
pyfFormatFractional = Format t t' 'Fractional
-> SignMode
-> Maybe (paddingWidth, AlignMode k, Char)
-> Maybe (Int, Char)
-> Maybe precision
-> Float
-> String
forall f paddingWidth precision (t :: AltStatus)
(t' :: UpperStatus) (k :: AlignForString).
(RealFloat f, Integral paddingWidth, Integral precision) =>
Format t t' 'Fractional
-> SignMode
-> Maybe (paddingWidth, AlignMode k, Char)
-> Maybe (Int, Char)
-> Maybe precision
-> f
-> String
formatFractional
class PyfFormatIntegral i where
pyfFormatIntegral ::
Integral paddingWidth =>
Format t t' 'Integral ->
SignMode ->
Maybe (paddingWidth, AlignMode k, Char) ->
Maybe (Int, Char) ->
i ->
String
instance {-# OVERLAPPABLE #-} Integral t => PyfFormatIntegral t where
pyfFormatIntegral :: Format t t' 'Integral
-> SignMode
-> Maybe (paddingWidth, AlignMode k, Char)
-> Maybe (Int, Char)
-> t
-> String
pyfFormatIntegral Format t t' 'Integral
f SignMode
s Maybe (paddingWidth, AlignMode k, Char)
p Maybe (Int, Char)
g t
v = Format t t' 'Integral
-> SignMode
-> Maybe (paddingWidth, AlignMode k, Char)
-> Maybe (Int, Char)
-> t
-> String
forall paddingWidth i (t :: AltStatus) (t' :: UpperStatus)
(k :: AlignForString).
(Integral paddingWidth, Integral i) =>
Format t t' 'Integral
-> SignMode
-> Maybe (paddingWidth, AlignMode k, Char)
-> Maybe (Int, Char)
-> i
-> String
formatIntegral Format t t' 'Integral
f SignMode
s Maybe (paddingWidth, AlignMode k, Char)
p Maybe (Int, Char)
g t
v
instance PyfFormatIntegral Char where
pyfFormatIntegral :: Format t t' 'Integral
-> SignMode
-> Maybe (paddingWidth, AlignMode k, Char)
-> Maybe (Int, Char)
-> Char
-> String
pyfFormatIntegral Format t t' 'Integral
f SignMode
s Maybe (paddingWidth, AlignMode k, Char)
p Maybe (Int, Char)
g Char
v = Format t t' 'Integral
-> SignMode
-> Maybe (paddingWidth, AlignMode k, Char)
-> Maybe (Int, Char)
-> Int
-> String
forall paddingWidth i (t :: AltStatus) (t' :: UpperStatus)
(k :: AlignForString).
(Integral paddingWidth, Integral i) =>
Format t t' 'Integral
-> SignMode
-> Maybe (paddingWidth, AlignMode k, Char)
-> Maybe (Int, Char)
-> i
-> String
formatIntegral Format t t' 'Integral
f SignMode
s Maybe (paddingWidth, AlignMode k, Char)
p Maybe (Int, Char)
g (Char -> Int
ord Char
v)