diff --git a/src/Foreign/Rust/External/JSON.hs b/src/Foreign/Rust/External/JSON.hs index e8ad823..3318f4c 100644 --- a/src/Foreign/Rust/External/JSON.hs +++ b/src/Foreign/Rust/External/JSON.hs @@ -16,6 +16,8 @@ module Foreign.Rust.External.JSON ( ) where import Codec.Borsh +import Foreign.Rust.Failure +import GHC.Stack import qualified Data.Aeson as Aeson import qualified Data.Aeson.Encoding as Aeson (unsafeToEncoding) @@ -32,13 +34,13 @@ newtype JSON = JSON Lazy.ByteString deriving stock (Eq) deriving newtype (BorshSize, ToBorsh, FromBorsh) --- | Types with an external JSON renderer (typically, in Rust) +-- | Types with a Rust-side JSON renderer class ToJSON a where toJSON :: a -> JSON --- | Types with an external JSON parser (typically, in Rust) +-- | Types with a Rust-side JSON parser class FromJSON a where - fromJSON :: JSON -> Either String a + fromJSON :: HasCallStack => JSON -> Either Failure a {------------------------------------------------------------------------------- Deriving-via: derive Aeson instances using external (de)serialiser @@ -67,6 +69,6 @@ instance ToJSON a => Aeson.ToJSON (UseExternalJSON a) where instance FromJSON a => Aeson.FromJSON (UseExternalJSON a) where parseJSON val = case fromJSON (JSON (Aeson.encode val)) of - Left failure -> Aeson.parseFail failure + Left failure -> Aeson.parseFail (show failure) Right tx -> return $ UseExternalJSON tx diff --git a/src/Foreign/Rust/Marshall/Variable.hs b/src/Foreign/Rust/Marshall/Variable.hs index 573e0fc..84a69e7 100644 --- a/src/Foreign/Rust/Marshall/Variable.hs +++ b/src/Foreign/Rust/Marshall/Variable.hs @@ -11,26 +11,20 @@ module Foreign.Rust.Marshall.Variable ( , Buffer -- opaque , getVarBuffer , withBorshVarBuffer - , withBorshFailure , withBorshBufferOfInitSize -- ** Pure variants , withPureBorshVarBuffer - , withPureBorshFailure ) where import Codec.Borsh -import Data.Bifunctor -import Data.Text (Text) import Data.Typeable import Foreign import Foreign.C.Types -import GHC.Stack import System.IO.Unsafe (unsafePerformIO) import qualified Data.ByteString as Strict import Foreign.Rust.Marshall.Util -import Foreign.Rust.Failure {------------------------------------------------------------------------------- Haskell to Rust @@ -72,16 +66,6 @@ withBorshVarBuffer :: forall a. => (Buffer a -> IO ()) -> IO a withBorshVarBuffer = withBorshBufferOfInitSize 1024 --- | Wrapper around 'withBorshVarBuffer' with explicit support for failures -withBorshFailure :: forall a. - ( FromBorsh a - , StaticBorshSize a ~ 'HasVariableSize - , Typeable a - , HasCallStack - ) - => (Buffer (Either Text a) -> IO ()) -> IO (Either Failure a) -withBorshFailure = fmap (first mkFailure) . withBorshVarBuffer - {------------------------------------------------------------------------------- Pure variants -------------------------------------------------------------------------------} @@ -94,15 +78,6 @@ withPureBorshVarBuffer :: forall a. => (Buffer a -> IO ()) -> a withPureBorshVarBuffer = unsafePerformIO . withBorshVarBuffer -withPureBorshFailure :: forall a. - ( FromBorsh a - , StaticBorshSize a ~ 'HasVariableSize - , Typeable a - , HasCallStack - ) - => (Buffer (Either Text a) -> IO ()) -> Either Failure a -withPureBorshFailure = unsafePerformIO . withBorshFailure - {------------------------------------------------------------------------------- Generalization -------------------------------------------------------------------------------}