packages feed

hsrs (empty) → 0.1.0.0

raw patch · 5 files changed

+118/−0 lines, 5 filesdep +basedep +borshdep +bytestringsetup-changed

Dependencies added: base, borsh, bytestring, text

Files

+ CHANGELOG.md view
@@ -0,0 +1,6 @@+# Changelog++## 0.1.0.0++- Initial release+- `Hsrs.Runtime` module with `fromBorshBuffer`, `withBorshArg`, and re-exports from `Codec.Borsh`
+ LICENSE view
@@ -0,0 +1,21 @@+MIT License++Copyright (c) 2026 Marko Vejnovic++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hsrs.cabal view
@@ -0,0 +1,37 @@+cabal-version: 2.4+name:          hsrs+version:       0.1.0.0+synopsis:      Runtime support for hsrs-generated Haskell FFI bindings+description:+  Provides the Haskell-side runtime needed by code generated+  by hsrs-codegen, including Borsh serialization utilities for+  FFI data transfer. Users of hsrs-generated bindings only need+  to add @hsrs@ to their @build-depends@ — the @borsh@ package+  is pulled in transitively.++license:       MIT+license-file:  LICENSE+author:        Marko Vejnovic+maintainer:    marko@mesa.dev+homepage:      https://github.com/harmont-dev/hsrs+bug-reports:   https://github.com/harmont-dev/hsrs/issues+category:      FFI, Foreign+build-type:    Simple+tested-with:   GHC == 9.6.7, GHC == 9.8.4, GHC == 9.10.1+extra-doc-files: CHANGELOG.md++source-repository head+  type:     git+  location: https://github.com/harmont-dev/hsrs+  subdir:   hsrs-haskell++library+  exposed-modules: Hsrs.Runtime+  build-depends:+      base        >= 4.16 && < 5+    , borsh       >= 0.3  && < 0.4+    , bytestring  >= 0.11 && < 0.13+    , text        >= 2.0  && < 2.3+  hs-source-dirs: src+  default-language: Haskell2010+  ghc-options: -Wall
+ src/Hsrs/Runtime.hs view
@@ -0,0 +1,52 @@+module Hsrs.Runtime+  ( -- * Borsh buffer FFI+    BorshBufferRaw+  , fromBorshBuffer+    -- * Borsh argument marshalling+  , withBorshArg+    -- * Exceptions+  , HsrsDeserialiseError(..)+    -- * Re-exports from Codec.Borsh+  , BorshSize+  , ToBorsh+  , FromBorsh+  , AsStruct+  , serialiseBorsh+  , deserialiseBorsh+    -- * Re-exports for generated code+  , Text+  ) where++import Codec.Borsh (AsStruct, BorshSize, FromBorsh, ToBorsh, deserialiseBorsh, serialiseBorsh)+import Control.Exception (Exception, throwIO)+import Data.Text (Text)+import Data.ByteString (packCStringLen, useAsCStringLen)+import Data.Word (Word8, Word64)+import Foreign (FinalizerPtr, Ptr, castPtr, newForeignPtr, withForeignPtr)++data BorshBufferRaw++foreign import ccall "hsrs_borsh_len"  c_hsrsBorshLen  :: Ptr BorshBufferRaw -> IO Word64+foreign import ccall "hsrs_borsh_ptr"  c_hsrsBorshPtr  :: Ptr BorshBufferRaw -> IO (Ptr Word8)+foreign import ccall "&hsrs_borsh_free" c_hsrsBorshFree :: FinalizerPtr BorshBufferRaw++newtype HsrsDeserialiseError = HsrsDeserialiseError String+  deriving (Show)++instance Exception HsrsDeserialiseError++fromBorshBuffer :: FromBorsh a => Ptr BorshBufferRaw -> IO a+fromBorshBuffer bufPtr = do+  fp <- newForeignPtr c_hsrsBorshFree bufPtr+  withForeignPtr fp $ \p -> do+    len <- c_hsrsBorshLen p+    dataPtr <- c_hsrsBorshPtr p+    bs <- packCStringLen (castPtr dataPtr, fromIntegral len)+    case deserialiseBorsh bs of+      Left err -> throwIO (HsrsDeserialiseError (show err))+      Right val -> pure val++withBorshArg :: ToBorsh a => a -> (Ptr Word8 -> Word64 -> IO b) -> IO b+withBorshArg val f =+  useAsCStringLen (serialiseBorsh val) $ \(ptr, len) ->+    f (castPtr ptr) (fromIntegral len)