-- SPDX-FileCopyrightText: 2018 obsidian.systems
-- SPDX-FileCopyrightText: 2020 Tocqueville Group
--
-- SPDX-License-Identifier: LicenseRef-MIT-obsidian-systems

-- | Module that defines helper types and functions that are related
-- to Micheline.
module Morley.Micheline.Json
  ( StringEncode (..)
  , TezosBigNum
  , TezosInt64
  , TezosMutez (..)
  ) where

import Data.Aeson (FromJSON, ToJSON, parseJSON, toEncoding, toJSON)
import qualified Data.Aeson.Encoding as AE
import qualified Data.Aeson.Types as Aeson
import Data.Bits (Bits)
import Data.Typeable (typeRep)
import Fmt (Buildable(..))
import qualified Text.Show as T

import Tezos.Core (Mutez, mkMutez', mutezToInt64)

printAsString :: Show a => a -> Aeson.Value
printAsString :: a -> Value
printAsString a :: a
a = Text -> Value
Aeson.String (Text -> Value) -> Text -> Value
forall a b. (a -> b) -> a -> b
$ a -> Text
forall b a. (Show a, IsString b) => a -> b
show a
a

parseAsString :: forall a. (Read a, Typeable a) => Aeson.Value -> Aeson.Parser a
parseAsString :: Value -> Parser a
parseAsString = String -> (Text -> Parser a) -> Value -> Parser a
forall a. String -> (Text -> Parser a) -> Value -> Parser a
Aeson.withText (TypeRep -> String
forall a. Show a => a -> String
T.show (TypeRep -> String) -> TypeRep -> String
forall a b. (a -> b) -> a -> b
$ Proxy a -> TypeRep
forall k (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (Proxy a
forall k (t :: k). Proxy t
Proxy :: Proxy a)) ((Text -> Parser a) -> Value -> Parser a)
-> (Text -> Parser a) -> Value -> Parser a
forall a b. (a -> b) -> a -> b
$ \txt :: Text
txt ->
  Parser a -> (a -> Parser a) -> Maybe a -> Parser a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail "Failed to parse string") a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe a -> Parser a) -> Maybe a -> Parser a
forall a b. (a -> b) -> a -> b
$ String -> Maybe a
forall a. Read a => String -> Maybe a
readMaybe (Text -> String
forall a. ToString a => a -> String
toString Text
txt)

parseStringEncodedIntegral :: (Read a, Typeable a) => Aeson.Value -> Aeson.Parser (StringEncode a)
parseStringEncodedIntegral :: Value -> Parser (StringEncode a)
parseStringEncodedIntegral x :: Value
x = a -> StringEncode a
forall a. a -> StringEncode a
StringEncode (a -> StringEncode a) -> Parser a -> Parser (StringEncode a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser a
forall a. (Read a, Typeable a) => Value -> Parser a
parseAsString Value
x

newtype StringEncode a = StringEncode { StringEncode a -> a
unStringEncode :: a }
  deriving stock ((forall x. StringEncode a -> Rep (StringEncode a) x)
-> (forall x. Rep (StringEncode a) x -> StringEncode a)
-> Generic (StringEncode a)
forall x. Rep (StringEncode a) x -> StringEncode a
forall x. StringEncode a -> Rep (StringEncode a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (StringEncode a) x -> StringEncode a
forall a x. StringEncode a -> Rep (StringEncode a) x
$cto :: forall a x. Rep (StringEncode a) x -> StringEncode a
$cfrom :: forall a x. StringEncode a -> Rep (StringEncode a) x
Generic, StringEncode a -> StringEncode a -> Bool
(StringEncode a -> StringEncode a -> Bool)
-> (StringEncode a -> StringEncode a -> Bool)
-> Eq (StringEncode a)
forall a. Eq a => StringEncode a -> StringEncode a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StringEncode a -> StringEncode a -> Bool
$c/= :: forall a. Eq a => StringEncode a -> StringEncode a -> Bool
== :: StringEncode a -> StringEncode a -> Bool
$c== :: forall a. Eq a => StringEncode a -> StringEncode a -> Bool
Eq, Eq (StringEncode a)
Eq (StringEncode a) =>
(StringEncode a -> StringEncode a -> Ordering)
-> (StringEncode a -> StringEncode a -> Bool)
-> (StringEncode a -> StringEncode a -> Bool)
-> (StringEncode a -> StringEncode a -> Bool)
-> (StringEncode a -> StringEncode a -> Bool)
-> (StringEncode a -> StringEncode a -> StringEncode a)
-> (StringEncode a -> StringEncode a -> StringEncode a)
-> Ord (StringEncode a)
StringEncode a -> StringEncode a -> Bool
StringEncode a -> StringEncode a -> Ordering
StringEncode a -> StringEncode a -> StringEncode a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (StringEncode a)
forall a. Ord a => StringEncode a -> StringEncode a -> Bool
forall a. Ord a => StringEncode a -> StringEncode a -> Ordering
forall a.
Ord a =>
StringEncode a -> StringEncode a -> StringEncode a
min :: StringEncode a -> StringEncode a -> StringEncode a
$cmin :: forall a.
Ord a =>
StringEncode a -> StringEncode a -> StringEncode a
max :: StringEncode a -> StringEncode a -> StringEncode a
$cmax :: forall a.
Ord a =>
StringEncode a -> StringEncode a -> StringEncode a
>= :: StringEncode a -> StringEncode a -> Bool
$c>= :: forall a. Ord a => StringEncode a -> StringEncode a -> Bool
> :: StringEncode a -> StringEncode a -> Bool
$c> :: forall a. Ord a => StringEncode a -> StringEncode a -> Bool
<= :: StringEncode a -> StringEncode a -> Bool
$c<= :: forall a. Ord a => StringEncode a -> StringEncode a -> Bool
< :: StringEncode a -> StringEncode a -> Bool
$c< :: forall a. Ord a => StringEncode a -> StringEncode a -> Bool
compare :: StringEncode a -> StringEncode a -> Ordering
$ccompare :: forall a. Ord a => StringEncode a -> StringEncode a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (StringEncode a)
Ord, StringEncode a
StringEncode a -> StringEncode a -> Bounded (StringEncode a)
forall a. a -> a -> Bounded a
forall a. Bounded a => StringEncode a
maxBound :: StringEncode a
$cmaxBound :: forall a. Bounded a => StringEncode a
minBound :: StringEncode a
$cminBound :: forall a. Bounded a => StringEncode a
Bounded, ReadPrec [StringEncode a]
ReadPrec (StringEncode a)
Int -> ReadS (StringEncode a)
ReadS [StringEncode a]
(Int -> ReadS (StringEncode a))
-> ReadS [StringEncode a]
-> ReadPrec (StringEncode a)
-> ReadPrec [StringEncode a]
-> Read (StringEncode a)
forall a. Read a => ReadPrec [StringEncode a]
forall a. Read a => ReadPrec (StringEncode a)
forall a. Read a => Int -> ReadS (StringEncode a)
forall a. Read a => ReadS [StringEncode a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [StringEncode a]
$creadListPrec :: forall a. Read a => ReadPrec [StringEncode a]
readPrec :: ReadPrec (StringEncode a)
$creadPrec :: forall a. Read a => ReadPrec (StringEncode a)
readList :: ReadS [StringEncode a]
$creadList :: forall a. Read a => ReadS [StringEncode a]
readsPrec :: Int -> ReadS (StringEncode a)
$creadsPrec :: forall a. Read a => Int -> ReadS (StringEncode a)
Read, Int -> StringEncode a -> ShowS
[StringEncode a] -> ShowS
StringEncode a -> String
(Int -> StringEncode a -> ShowS)
-> (StringEncode a -> String)
-> ([StringEncode a] -> ShowS)
-> Show (StringEncode a)
forall a. Show a => Int -> StringEncode a -> ShowS
forall a. Show a => [StringEncode a] -> ShowS
forall a. Show a => StringEncode a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StringEncode a] -> ShowS
$cshowList :: forall a. Show a => [StringEncode a] -> ShowS
show :: StringEncode a -> String
$cshow :: forall a. Show a => StringEncode a -> String
showsPrec :: Int -> StringEncode a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> StringEncode a -> ShowS
Show)
  deriving newtype (Int -> StringEncode a
StringEncode a -> Int
StringEncode a -> [StringEncode a]
StringEncode a -> StringEncode a
StringEncode a -> StringEncode a -> [StringEncode a]
StringEncode a
-> StringEncode a -> StringEncode a -> [StringEncode a]
(StringEncode a -> StringEncode a)
-> (StringEncode a -> StringEncode a)
-> (Int -> StringEncode a)
-> (StringEncode a -> Int)
-> (StringEncode a -> [StringEncode a])
-> (StringEncode a -> StringEncode a -> [StringEncode a])
-> (StringEncode a -> StringEncode a -> [StringEncode a])
-> (StringEncode a
    -> StringEncode a -> StringEncode a -> [StringEncode a])
-> Enum (StringEncode a)
forall a. Enum a => Int -> StringEncode a
forall a. Enum a => StringEncode a -> Int
forall a. Enum a => StringEncode a -> [StringEncode a]
forall a. Enum a => StringEncode a -> StringEncode a
forall a.
Enum a =>
StringEncode a -> StringEncode a -> [StringEncode a]
forall a.
Enum a =>
StringEncode a
-> StringEncode a -> StringEncode a -> [StringEncode a]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: StringEncode a
-> StringEncode a -> StringEncode a -> [StringEncode a]
$cenumFromThenTo :: forall a.
Enum a =>
StringEncode a
-> StringEncode a -> StringEncode a -> [StringEncode a]
enumFromTo :: StringEncode a -> StringEncode a -> [StringEncode a]
$cenumFromTo :: forall a.
Enum a =>
StringEncode a -> StringEncode a -> [StringEncode a]
enumFromThen :: StringEncode a -> StringEncode a -> [StringEncode a]
$cenumFromThen :: forall a.
Enum a =>
StringEncode a -> StringEncode a -> [StringEncode a]
enumFrom :: StringEncode a -> [StringEncode a]
$cenumFrom :: forall a. Enum a => StringEncode a -> [StringEncode a]
fromEnum :: StringEncode a -> Int
$cfromEnum :: forall a. Enum a => StringEncode a -> Int
toEnum :: Int -> StringEncode a
$ctoEnum :: forall a. Enum a => Int -> StringEncode a
pred :: StringEncode a -> StringEncode a
$cpred :: forall a. Enum a => StringEncode a -> StringEncode a
succ :: StringEncode a -> StringEncode a
$csucc :: forall a. Enum a => StringEncode a -> StringEncode a
Enum, Integer -> StringEncode a
StringEncode a -> StringEncode a
StringEncode a -> StringEncode a -> StringEncode a
(StringEncode a -> StringEncode a -> StringEncode a)
-> (StringEncode a -> StringEncode a -> StringEncode a)
-> (StringEncode a -> StringEncode a -> StringEncode a)
-> (StringEncode a -> StringEncode a)
-> (StringEncode a -> StringEncode a)
-> (StringEncode a -> StringEncode a)
-> (Integer -> StringEncode a)
-> Num (StringEncode a)
forall a. Num a => Integer -> StringEncode a
forall a. Num a => StringEncode a -> StringEncode a
forall a.
Num a =>
StringEncode a -> StringEncode a -> StringEncode a
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> StringEncode a
$cfromInteger :: forall a. Num a => Integer -> StringEncode a
signum :: StringEncode a -> StringEncode a
$csignum :: forall a. Num a => StringEncode a -> StringEncode a
abs :: StringEncode a -> StringEncode a
$cabs :: forall a. Num a => StringEncode a -> StringEncode a
negate :: StringEncode a -> StringEncode a
$cnegate :: forall a. Num a => StringEncode a -> StringEncode a
* :: StringEncode a -> StringEncode a -> StringEncode a
$c* :: forall a.
Num a =>
StringEncode a -> StringEncode a -> StringEncode a
- :: StringEncode a -> StringEncode a -> StringEncode a
$c- :: forall a.
Num a =>
StringEncode a -> StringEncode a -> StringEncode a
+ :: StringEncode a -> StringEncode a -> StringEncode a
$c+ :: forall a.
Num a =>
StringEncode a -> StringEncode a -> StringEncode a
Num, Enum (StringEncode a)
Real (StringEncode a)
(Real (StringEncode a), Enum (StringEncode a)) =>
(StringEncode a -> StringEncode a -> StringEncode a)
-> (StringEncode a -> StringEncode a -> StringEncode a)
-> (StringEncode a -> StringEncode a -> StringEncode a)
-> (StringEncode a -> StringEncode a -> StringEncode a)
-> (StringEncode a
    -> StringEncode a -> (StringEncode a, StringEncode a))
-> (StringEncode a
    -> StringEncode a -> (StringEncode a, StringEncode a))
-> (StringEncode a -> Integer)
-> Integral (StringEncode a)
StringEncode a -> Integer
StringEncode a
-> StringEncode a -> (StringEncode a, StringEncode a)
StringEncode a -> StringEncode a -> StringEncode a
forall a. Integral a => Enum (StringEncode a)
forall a. Integral a => Real (StringEncode a)
forall a. Integral a => StringEncode a -> Integer
forall a.
Integral a =>
StringEncode a
-> StringEncode a -> (StringEncode a, StringEncode a)
forall a.
Integral a =>
StringEncode a -> StringEncode a -> StringEncode a
forall a.
(Real a, Enum a) =>
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
toInteger :: StringEncode a -> Integer
$ctoInteger :: forall a. Integral a => StringEncode a -> Integer
divMod :: StringEncode a
-> StringEncode a -> (StringEncode a, StringEncode a)
$cdivMod :: forall a.
Integral a =>
StringEncode a
-> StringEncode a -> (StringEncode a, StringEncode a)
quotRem :: StringEncode a
-> StringEncode a -> (StringEncode a, StringEncode a)
$cquotRem :: forall a.
Integral a =>
StringEncode a
-> StringEncode a -> (StringEncode a, StringEncode a)
mod :: StringEncode a -> StringEncode a -> StringEncode a
$cmod :: forall a.
Integral a =>
StringEncode a -> StringEncode a -> StringEncode a
div :: StringEncode a -> StringEncode a -> StringEncode a
$cdiv :: forall a.
Integral a =>
StringEncode a -> StringEncode a -> StringEncode a
rem :: StringEncode a -> StringEncode a -> StringEncode a
$crem :: forall a.
Integral a =>
StringEncode a -> StringEncode a -> StringEncode a
quot :: StringEncode a -> StringEncode a -> StringEncode a
$cquot :: forall a.
Integral a =>
StringEncode a -> StringEncode a -> StringEncode a
$cp2Integral :: forall a. Integral a => Enum (StringEncode a)
$cp1Integral :: forall a. Integral a => Real (StringEncode a)
Integral, Eq (StringEncode a)
StringEncode a
Eq (StringEncode a) =>
(StringEncode a -> StringEncode a -> StringEncode a)
-> (StringEncode a -> StringEncode a -> StringEncode a)
-> (StringEncode a -> StringEncode a -> StringEncode a)
-> (StringEncode a -> StringEncode a)
-> (StringEncode a -> Int -> StringEncode a)
-> (StringEncode a -> Int -> StringEncode a)
-> StringEncode a
-> (Int -> StringEncode a)
-> (StringEncode a -> Int -> StringEncode a)
-> (StringEncode a -> Int -> StringEncode a)
-> (StringEncode a -> Int -> StringEncode a)
-> (StringEncode a -> Int -> Bool)
-> (StringEncode a -> Maybe Int)
-> (StringEncode a -> Int)
-> (StringEncode a -> Bool)
-> (StringEncode a -> Int -> StringEncode a)
-> (StringEncode a -> Int -> StringEncode a)
-> (StringEncode a -> Int -> StringEncode a)
-> (StringEncode a -> Int -> StringEncode a)
-> (StringEncode a -> Int -> StringEncode a)
-> (StringEncode a -> Int -> StringEncode a)
-> (StringEncode a -> Int)
-> Bits (StringEncode a)
Int -> StringEncode a
StringEncode a -> Bool
StringEncode a -> Int
StringEncode a -> Maybe Int
StringEncode a -> StringEncode a
StringEncode a -> Int -> Bool
StringEncode a -> Int -> StringEncode a
StringEncode a -> StringEncode a -> StringEncode a
forall a.
Eq a =>
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> a
-> (Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> Bool)
-> (a -> Maybe Int)
-> (a -> Int)
-> (a -> Bool)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int -> a)
-> (a -> Int)
-> Bits a
forall a. Bits a => Eq (StringEncode a)
forall a. Bits a => StringEncode a
forall a. Bits a => Int -> StringEncode a
forall a. Bits a => StringEncode a -> Bool
forall a. Bits a => StringEncode a -> Int
forall a. Bits a => StringEncode a -> Maybe Int
forall a. Bits a => StringEncode a -> StringEncode a
forall a. Bits a => StringEncode a -> Int -> Bool
forall a. Bits a => StringEncode a -> Int -> StringEncode a
forall a.
Bits a =>
StringEncode a -> StringEncode a -> StringEncode a
popCount :: StringEncode a -> Int
$cpopCount :: forall a. Bits a => StringEncode a -> Int
rotateR :: StringEncode a -> Int -> StringEncode a
$crotateR :: forall a. Bits a => StringEncode a -> Int -> StringEncode a
rotateL :: StringEncode a -> Int -> StringEncode a
$crotateL :: forall a. Bits a => StringEncode a -> Int -> StringEncode a
unsafeShiftR :: StringEncode a -> Int -> StringEncode a
$cunsafeShiftR :: forall a. Bits a => StringEncode a -> Int -> StringEncode a
shiftR :: StringEncode a -> Int -> StringEncode a
$cshiftR :: forall a. Bits a => StringEncode a -> Int -> StringEncode a
unsafeShiftL :: StringEncode a -> Int -> StringEncode a
$cunsafeShiftL :: forall a. Bits a => StringEncode a -> Int -> StringEncode a
shiftL :: StringEncode a -> Int -> StringEncode a
$cshiftL :: forall a. Bits a => StringEncode a -> Int -> StringEncode a
isSigned :: StringEncode a -> Bool
$cisSigned :: forall a. Bits a => StringEncode a -> Bool
bitSize :: StringEncode a -> Int
$cbitSize :: forall a. Bits a => StringEncode a -> Int
bitSizeMaybe :: StringEncode a -> Maybe Int
$cbitSizeMaybe :: forall a. Bits a => StringEncode a -> Maybe Int
testBit :: StringEncode a -> Int -> Bool
$ctestBit :: forall a. Bits a => StringEncode a -> Int -> Bool
complementBit :: StringEncode a -> Int -> StringEncode a
$ccomplementBit :: forall a. Bits a => StringEncode a -> Int -> StringEncode a
clearBit :: StringEncode a -> Int -> StringEncode a
$cclearBit :: forall a. Bits a => StringEncode a -> Int -> StringEncode a
setBit :: StringEncode a -> Int -> StringEncode a
$csetBit :: forall a. Bits a => StringEncode a -> Int -> StringEncode a
bit :: Int -> StringEncode a
$cbit :: forall a. Bits a => Int -> StringEncode a
zeroBits :: StringEncode a
$czeroBits :: forall a. Bits a => StringEncode a
rotate :: StringEncode a -> Int -> StringEncode a
$crotate :: forall a. Bits a => StringEncode a -> Int -> StringEncode a
shift :: StringEncode a -> Int -> StringEncode a
$cshift :: forall a. Bits a => StringEncode a -> Int -> StringEncode a
complement :: StringEncode a -> StringEncode a
$ccomplement :: forall a. Bits a => StringEncode a -> StringEncode a
xor :: StringEncode a -> StringEncode a -> StringEncode a
$cxor :: forall a.
Bits a =>
StringEncode a -> StringEncode a -> StringEncode a
.|. :: StringEncode a -> StringEncode a -> StringEncode a
$c.|. :: forall a.
Bits a =>
StringEncode a -> StringEncode a -> StringEncode a
.&. :: StringEncode a -> StringEncode a -> StringEncode a
$c.&. :: forall a.
Bits a =>
StringEncode a -> StringEncode a -> StringEncode a
$cp1Bits :: forall a. Bits a => Eq (StringEncode a)
Bits, Num (StringEncode a)
Ord (StringEncode a)
(Num (StringEncode a), Ord (StringEncode a)) =>
(StringEncode a -> Rational) -> Real (StringEncode a)
StringEncode a -> Rational
forall a. (Num a, Ord a) => (a -> Rational) -> Real a
forall a. Real a => Num (StringEncode a)
forall a. Real a => Ord (StringEncode a)
forall a. Real a => StringEncode a -> Rational
toRational :: StringEncode a -> Rational
$ctoRational :: forall a. Real a => StringEncode a -> Rational
$cp2Real :: forall a. Real a => Ord (StringEncode a)
$cp1Real :: forall a. Real a => Num (StringEncode a)
Real, StringEncode a -> ()
(StringEncode a -> ()) -> NFData (StringEncode a)
forall a. NFData a => StringEncode a -> ()
forall a. (a -> ()) -> NFData a
rnf :: StringEncode a -> ()
$crnf :: forall a. NFData a => StringEncode a -> ()
NFData, Int -> StringEncode a -> Int
StringEncode a -> Int
(Int -> StringEncode a -> Int)
-> (StringEncode a -> Int) -> Hashable (StringEncode a)
forall a. Hashable a => Int -> StringEncode a -> Int
forall a. Hashable a => StringEncode a -> Int
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: StringEncode a -> Int
$chash :: forall a. Hashable a => StringEncode a -> Int
hashWithSalt :: Int -> StringEncode a -> Int
$chashWithSalt :: forall a. Hashable a => Int -> StringEncode a -> Int
Hashable)

type TezosBigNum = StringEncode Integer

instance FromJSON TezosBigNum where
  parseJSON :: Value -> Parser TezosBigNum
parseJSON = Value -> Parser TezosBigNum
forall a. (Read a, Typeable a) => Value -> Parser (StringEncode a)
parseStringEncodedIntegral

instance ToJSON TezosBigNum where
  toJSON :: TezosBigNum -> Value
toJSON (StringEncode x :: Integer
x) = Text -> Value
Aeson.String (Text -> Value) -> Text -> Value
forall a b. (a -> b) -> a -> b
$ Integer -> Text
forall b a. (Show a, IsString b) => a -> b
show Integer
x
  toEncoding :: TezosBigNum -> Encoding
toEncoding (StringEncode x :: Integer
x) = Integer -> Encoding
forall a. Integer -> Encoding' a
AE.integerText Integer
x

type TezosInt64 = StringEncode Int64

instance FromJSON TezosInt64 where
  parseJSON :: Value -> Parser TezosInt64
parseJSON = Value -> Parser TezosInt64
forall a. (Read a, Typeable a) => Value -> Parser (StringEncode a)
parseStringEncodedIntegral

instance Buildable TezosInt64 where
  build :: TezosInt64 -> Builder
build = TezosInt64 -> Builder
forall b a. (Show a, IsString b) => a -> b
show

instance ToJSON TezosInt64 where
  toJSON :: TezosInt64 -> Value
toJSON (StringEncode x :: Int64
x) = Text -> Value
Aeson.String (Text -> Value) -> Text -> Value
forall a b. (a -> b) -> a -> b
$ Int64 -> Text
forall b a. (Show a, IsString b) => a -> b
show Int64
x
  toEncoding :: TezosInt64 -> Encoding
toEncoding (StringEncode x :: Int64
x) = Int64 -> Encoding
forall a. Int64 -> Encoding' a
AE.int64Text Int64
x

newtype TezosMutez = TezosMutez { TezosMutez -> Mutez
unTezosMutez :: Mutez }
  deriving stock (Int -> TezosMutez -> ShowS
[TezosMutez] -> ShowS
TezosMutez -> String
(Int -> TezosMutez -> ShowS)
-> (TezosMutez -> String)
-> ([TezosMutez] -> ShowS)
-> Show TezosMutez
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TezosMutez] -> ShowS
$cshowList :: [TezosMutez] -> ShowS
show :: TezosMutez -> String
$cshow :: TezosMutez -> String
showsPrec :: Int -> TezosMutez -> ShowS
$cshowsPrec :: Int -> TezosMutez -> ShowS
Show, TezosMutez -> TezosMutez -> Bool
(TezosMutez -> TezosMutez -> Bool)
-> (TezosMutez -> TezosMutez -> Bool) -> Eq TezosMutez
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TezosMutez -> TezosMutez -> Bool
$c/= :: TezosMutez -> TezosMutez -> Bool
== :: TezosMutez -> TezosMutez -> Bool
$c== :: TezosMutez -> TezosMutez -> Bool
Eq, Eq TezosMutez
Eq TezosMutez =>
(TezosMutez -> TezosMutez -> Ordering)
-> (TezosMutez -> TezosMutez -> Bool)
-> (TezosMutez -> TezosMutez -> Bool)
-> (TezosMutez -> TezosMutez -> Bool)
-> (TezosMutez -> TezosMutez -> Bool)
-> (TezosMutez -> TezosMutez -> TezosMutez)
-> (TezosMutez -> TezosMutez -> TezosMutez)
-> Ord TezosMutez
TezosMutez -> TezosMutez -> Bool
TezosMutez -> TezosMutez -> Ordering
TezosMutez -> TezosMutez -> TezosMutez
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: TezosMutez -> TezosMutez -> TezosMutez
$cmin :: TezosMutez -> TezosMutez -> TezosMutez
max :: TezosMutez -> TezosMutez -> TezosMutez
$cmax :: TezosMutez -> TezosMutez -> TezosMutez
>= :: TezosMutez -> TezosMutez -> Bool
$c>= :: TezosMutez -> TezosMutez -> Bool
> :: TezosMutez -> TezosMutez -> Bool
$c> :: TezosMutez -> TezosMutez -> Bool
<= :: TezosMutez -> TezosMutez -> Bool
$c<= :: TezosMutez -> TezosMutez -> Bool
< :: TezosMutez -> TezosMutez -> Bool
$c< :: TezosMutez -> TezosMutez -> Bool
compare :: TezosMutez -> TezosMutez -> Ordering
$ccompare :: TezosMutez -> TezosMutez -> Ordering
$cp1Ord :: Eq TezosMutez
Ord)

instance ToJSON TezosMutez where
  toJSON :: TezosMutez -> Value
toJSON = Int64 -> Value
forall a. Show a => a -> Value
printAsString (Int64 -> Value) -> (TezosMutez -> Int64) -> TezosMutez -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Mutez -> Int64
mutezToInt64 (Mutez -> Int64) -> (TezosMutez -> Mutez) -> TezosMutez -> Int64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TezosMutez -> Mutez
unTezosMutez

instance FromJSON TezosMutez where
  parseJSON :: Value -> Parser TezosMutez
parseJSON v :: Value
v = do
    Int64
i <- Value -> Parser Int64
forall a. (Read a, Typeable a) => Value -> Parser a
parseAsString @Int64 Value
v
    (Text -> Parser TezosMutez)
-> (Mutez -> Parser TezosMutez)
-> Either Text Mutez
-> Parser TezosMutez
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Parser TezosMutez
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser TezosMutez)
-> (Text -> String) -> Text -> Parser TezosMutez
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
forall a. ToString a => a -> String
toString) (TezosMutez -> Parser TezosMutez
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TezosMutez -> Parser TezosMutez)
-> (Mutez -> TezosMutez) -> Mutez -> Parser TezosMutez
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Mutez -> TezosMutez
TezosMutez) (Either Text Mutez -> Parser TezosMutez)
-> Either Text Mutez -> Parser TezosMutez
forall a b. (a -> b) -> a -> b
$ Int64 -> Either Text Mutez
forall i. Integral i => i -> Either Text Mutez
mkMutez' Int64
i