-- | 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
$c== :: Quantity -> Quantity -> Bool
== :: Quantity -> Quantity -> Bool
$c/= :: Quantity -> Quantity -> Bool
/= :: 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
$cshowsPrec :: Int -> Quantity -> ShowS
showsPrec :: Int -> Quantity -> ShowS
$cshow :: Quantity -> String
show :: Quantity -> String
$cshowList :: [Quantity] -> ShowS
showList :: [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
$cfrom :: forall x. Quantity -> Rep Quantity x
from :: forall x. Quantity -> Rep Quantity x
$cto :: forall x. Rep Quantity x -> Quantity
to :: forall x. Rep Quantity x -> Quantity
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
$c+ :: Quantity -> Quantity -> Quantity
+ :: Quantity -> Quantity -> Quantity
$c- :: Quantity -> Quantity -> Quantity
- :: Quantity -> Quantity -> Quantity
$c* :: Quantity -> Quantity -> Quantity
* :: Quantity -> Quantity -> Quantity
$cnegate :: Quantity -> Quantity
negate :: Quantity -> Quantity
$cabs :: Quantity -> Quantity
abs :: Quantity -> Quantity
$csignum :: Quantity -> Quantity
signum :: Quantity -> Quantity
$cfromInteger :: Integer -> Quantity
fromInteger :: Integer -> 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
$creadsPrec :: Int -> ReadS Quantity
readsPrec :: Int -> ReadS Quantity
$creadList :: ReadS [Quantity]
readList :: ReadS [Quantity]
$creadPrec :: ReadPrec Quantity
readPrec :: ReadPrec Quantity
$creadListPrec :: ReadPrec [Quantity]
readListPrec :: ReadPrec [Quantity]
Read, Text -> Either Text Quantity
ByteString -> 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
$cparseUrlPiece :: Text -> Either Text Quantity
parseUrlPiece :: Text -> Either Text Quantity
$cparseHeader :: ByteString -> Either Text Quantity
parseHeader :: ByteString -> Either Text Quantity
$cparseQueryParam :: Text -> Either Text Quantity
parseQueryParam :: Text -> Either Text Quantity
FromHttpApiData, Quantity -> Text
Quantity -> ByteString
Quantity -> Builder
(Quantity -> Text)
-> (Quantity -> Builder)
-> (Quantity -> ByteString)
-> (Quantity -> Text)
-> (Quantity -> Builder)
-> ToHttpApiData Quantity
forall a.
(a -> Text)
-> (a -> Builder)
-> (a -> ByteString)
-> (a -> Text)
-> (a -> Builder)
-> ToHttpApiData a
$ctoUrlPiece :: Quantity -> Text
toUrlPiece :: Quantity -> Text
$ctoEncodedUrlPiece :: Quantity -> Builder
toEncodedUrlPiece :: Quantity -> Builder
$ctoHeader :: Quantity -> ByteString
toHeader :: Quantity -> ByteString
$ctoQueryParam :: Quantity -> Text
toQueryParam :: Quantity -> Text
$ctoEncodedQueryParam :: Quantity -> Builder
toEncodedQueryParam :: Quantity -> Builder
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 a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Unable to read quantity as Integer"
      Just Quantity
quant -> Quantity -> Parser Quantity
forall a. a -> Parser a
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 a. a -> Proxy Quantity -> a
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]