{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_HADDOCK hide #-}
module Cometa.Internal.Inspection (inspect, runUnchecked) where
import Cometa.Error (Error (..))
import qualified Cometa.Internal.FFI.Inspection as FFI
import qualified Cometa.Internal.Runtime as Runtime
import Data.Aeson (Value, eitherDecodeStrict')
import Data.ByteString (ByteString)
import Data.Text (Text)
import Foreign.C.Types (CUInt)
inspect :: Text -> CUInt -> ByteString -> IO (Either Error Value)
inspect :: Text -> CUInt -> ByteString -> IO (Either Error Value)
inspect Text
operation CUInt
kind ByteString
bytes = do
Either Error ByteString
result <- CUInt -> ByteString -> IO (Either Error ByteString)
runUnchecked CUInt
kind ByteString
bytes
Either Error Value -> IO (Either Error Value)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error Value -> IO (Either Error Value))
-> Either Error Value -> IO (Either Error Value)
forall a b. (a -> b) -> a -> b
$ do
ByteString
json <- Either Error ByteString
result
(String -> Either Error Value)
-> (Value -> Either Error Value)
-> Either String Value
-> Either Error Value
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either Error Value -> String -> Either Error Value
forall a b. a -> b -> a
const (Error -> Either Error Value
forall a b. a -> Either a b
Left (Text -> Error
NativeContractViolation Text
operation))) Value -> Either Error Value
forall a b. b -> Either a b
Right (ByteString -> Either String Value
forall a. FromJSON a => ByteString -> Either String a
eitherDecodeStrict' ByteString
json)
runUnchecked :: CUInt -> ByteString -> IO (Either Error ByteString)
runUnchecked :: CUInt -> ByteString -> IO (Either Error ByteString)
runUnchecked CUInt
kind ByteString
bytes = ByteString
-> (Ptr Word8 -> CSize -> IO (Either Error ByteString))
-> IO (Either Error ByteString)
forall a. ByteString -> (Ptr Word8 -> CSize -> IO a) -> IO a
Runtime.withBytes ByteString
bytes ((Ptr Word8 -> CSize -> IO (Either Error ByteString))
-> IO (Either Error ByteString))
-> (Ptr Word8 -> CSize -> IO (Either Error ByteString))
-> IO (Either Error ByteString)
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
input CSize
size ->
Text
-> (Ptr (Ptr Buffer) -> IO CUInt) -> IO (Either Error ByteString)
Runtime.withOwnedBuffer Text
"inspection.compute" (CUInt -> Ptr Word8 -> CSize -> Ptr (Ptr Buffer) -> IO CUInt
FFI.cCompute CUInt
kind Ptr Word8
input CSize
size)