-- | 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
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
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. 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
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]
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
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 -> Builder
Quantity -> ByteString
Quantity -> Text
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 = forall a. ToJSON a => a -> Value
toJSON forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show forall b c a. (b -> c) -> (a -> b) -> a -> c
. Quantity -> Integer
unQuantity
  toEncoding :: Quantity -> Encoding
toEncoding = forall a. ToJSON a => a -> Encoding
toEncoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show forall b c a. (b -> c) -> (a -> b) -> a -> c
. Quantity -> Integer
unQuantity

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

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