feat: Extracting word types

This commit is contained in:
vegowotenks 2025-07-11 17:50:29 +02:00
parent a577bddd7f
commit 5723a92308
4 changed files with 58 additions and 3 deletions

View file

@ -1,8 +1,9 @@
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE InstanceSigs #-}
module Language.Java.Classfile.Extract (Extract()) where
module Language.Java.Classfile.Extract (Extract(), bytes) where
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy as ByteString
data Extract a = Extract (Continuation a)
@ -28,6 +29,16 @@ instance Applicative Extract where
Fail -> Fail
-- | Get a specified count of bytes. Fail if there are not enough bytes available.
bytes :: Word -> Extract ByteString
bytes count = Extract $ \ input -> let
count' = fromIntegral count
(bs, rest) = ByteString.splitAt count' input
in if ByteString.length bs /= count'
then Fail
else Done rest bs
{- It seems I cannot define a lawful monad instance
instance Monad Extract where