-- | Quantity wrapper

module Blockfrost.Types.Shared.Quantity
  where

import Data.Aeson (FromJSON (..), ToJSON (..), withText)
import qualified Data.Text
import GHC.Generics
import Servant.API (FromHttpApiData (..), ToHttpApiData (..))
import Servant.Docs (ToSample (..), samples)
import qualified Text.Read

newtype Quantity = Quantity Integer
  deriving stock (Quantity -> Quantity -> Bool
(Quantity -> Quantity -> Bool)
-> (Quantity -> Quantity -> Bool) -> Eq Quantity
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Quantity -> Quantity -> Bool
$c/= :: Quantity -> Quantity -> Bool
== :: Quantity -> Quantity -> Bool
$c== :: Quantity -> Quantity -> Bool
Eq, Int -> Quantity -> ShowS
[Quantity] -> ShowS
Quantity -> String
(Int -> Quantity -> ShowS)
-> (Quantity -> String) -> ([Quantity] -> ShowS) -> Show Quantity
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Quantity] -> ShowS
$cshowList :: [Quantity] -> ShowS
show :: Quantity -> String
$cshow :: Quantity -> String
showsPrec :: Int -> Quantity -> ShowS
$cshowsPrec :: Int -> Quantity -> ShowS
Show, (forall x. Quantity -> Rep Quantity x)
-> (forall x. Rep Quantity x -> Quantity) -> Generic Quantity
forall x. Rep Quantity x -> Quantity
forall x. Quantity -> Rep Quantity x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Quantity x -> Quantity
$cfrom :: forall x. Quantity -> Rep Quantity x
Generic)
  deriving newtype (Integer -> Quantity
Quantity -> Quantity
Quantity -> Quantity -> Quantity
(Quantity -> Quantity -> Quantity)
-> (Quantity -> Quantity -> Quantity)
-> (Quantity -> Quantity -> Quantity)
-> (Quantity -> Quantity)
-> (Quantity -> Quantity)
-> (Quantity -> Quantity)
-> (Integer -> Quantity)
-> Num Quantity
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> Quantity
$cfromInteger :: Integer -> Quantity
signum :: Quantity -> Quantity
$csignum :: Quantity -> Quantity
abs :: Quantity -> Quantity
$cabs :: Quantity -> Quantity
negate :: Quantity -> Quantity
$cnegate :: Quantity -> Quantity
* :: Quantity -> Quantity -> Quantity
$c* :: Quantity -> Quantity -> Quantity
- :: Quantity -> Quantity -> Quantity
$c- :: Quantity -> Quantity -> Quantity
+ :: Quantity -> Quantity -> Quantity
$c+ :: Quantity -> Quantity -> Quantity
Num, ReadPrec [Quantity]
ReadPrec Quantity
Int -> ReadS Quantity
ReadS [Quantity]
(Int -> ReadS Quantity)
-> ReadS [Quantity]
-> ReadPrec Quantity
-> ReadPrec [Quantity]
-> Read Quantity
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Quantity]
$creadListPrec :: ReadPrec [Quantity]
readPrec :: ReadPrec Quantity
$creadPrec :: ReadPrec Quantity
readList :: ReadS [Quantity]
$creadList :: ReadS [Quantity]
readsPrec :: Int -> ReadS Quantity
$creadsPrec :: Int -> ReadS Quantity
Read, ByteString -> Either Text Quantity
Text -> Either Text Quantity
(Text -> Either Text Quantity)
-> (ByteString -> Either Text Quantity)
-> (Text -> Either Text Quantity)
-> FromHttpApiData Quantity
forall a.
(Text -> Either Text a)
-> (ByteString -> Either Text a)
-> (Text -> Either Text a)
-> FromHttpApiData a
parseQueryParam :: Text -> Either Text Quantity
$cparseQueryParam :: Text -> Either Text Quantity
parseHeader :: ByteString -> Either Text Quantity
$cparseHeader :: ByteString -> Either Text Quantity
parseUrlPiece :: Text -> Either Text Quantity
$cparseUrlPiece :: Text -> Either Text Quantity
FromHttpApiData, Quantity -> ByteString
Quantity -> Builder
Quantity -> Text
(Quantity -> Text)
-> (Quantity -> Builder)
-> (Quantity -> ByteString)
-> (Quantity -> Text)
-> ToHttpApiData Quantity
forall a.
(a -> Text)
-> (a -> Builder)
-> (a -> ByteString)
-> (a -> Text)
-> ToHttpApiData a
toQueryParam :: Quantity -> Text
$ctoQueryParam :: Quantity -> Text
toHeader :: Quantity -> ByteString
$ctoHeader :: Quantity -> ByteString
toEncodedUrlPiece :: Quantity -> Builder
$ctoEncodedUrlPiece :: Quantity -> Builder
toUrlPiece :: Quantity -> Text
$ctoUrlPiece :: Quantity -> Text
ToHttpApiData)

unQuantity :: Quantity -> Integer
unQuantity :: Quantity -> Integer
unQuantity (Quantity Integer
i) = Integer
i

instance ToJSON Quantity where
  toJSON :: Quantity -> Value
toJSON = String -> Value
forall a. ToJSON a => a -> Value
toJSON (String -> Value) -> (Quantity -> String) -> Quantity -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> String
forall a. Show a => a -> String
show (Integer -> String) -> (Quantity -> Integer) -> Quantity -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Quantity -> Integer
unQuantity
  toEncoding :: Quantity -> Encoding
toEncoding = String -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding (String -> Encoding)
-> (Quantity -> String) -> Quantity -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> String
forall a. Show a => a -> String
show (Integer -> String) -> (Quantity -> Integer) -> Quantity -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Quantity -> Integer
unQuantity

instance FromJSON Quantity where
  parseJSON :: Value -> Parser Quantity
parseJSON = String -> (Text -> Parser Quantity) -> Value -> Parser Quantity
forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText String
"quantity" ((Text -> Parser Quantity) -> Value -> Parser Quantity)
-> (Text -> Parser Quantity) -> Value -> Parser Quantity
forall a b. (a -> b) -> a -> b
$ \Text
q -> do
    case String -> Maybe Quantity
forall a. Read a => String -> Maybe a
Text.Read.readMaybe (Text -> String
Data.Text.unpack Text
q) of
      Maybe Quantity
Nothing    -> String -> Parser Quantity
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Unable to read quantity as Integer"
      Just Quantity
quant -> Quantity -> Parser Quantity
forall (f :: * -> *) a. Applicative f => a -> f a
pure Quantity
quant

instance ToSample Quantity where
    toSamples :: Proxy Quantity -> [(Text, Quantity)]
toSamples = [(Text, Quantity)] -> Proxy Quantity -> [(Text, Quantity)]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([(Text, Quantity)] -> Proxy Quantity -> [(Text, Quantity)])
-> [(Text, Quantity)] -> Proxy Quantity -> [(Text, Quantity)]
forall a b. (a -> b) -> a -> b
$ [Quantity] -> [(Text, Quantity)]
forall a. [a] -> [(Text, a)]
samples [Quantity
37040682, Quantity
412162133]