feat: generic deriving for extraction

This commit is contained in:
vegowotenks 2025-07-11 18:09:33 +02:00
parent 7a20aeeca2
commit 84510b41a5
3 changed files with 39 additions and 7 deletions

View file

@ -1,6 +1,6 @@
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE InstanceSigs #-}
module Language.Java.Classfile.Extract (Extract(), bytes) where
module Language.Java.Classfile.Extract (Extract(), bytes, runExtract) where
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy as ByteString
@ -37,7 +37,6 @@ instance Alternative Extract where
Fail -> right input
t -> t
-- | Get a specified count of bytes. Fail if there are not enough bytes available.
bytes :: Word -> Extract ByteString
@ -48,7 +47,6 @@ bytes count = Extract $ \ input -> let
then Fail
else Done rest bs
{- It seems I cannot define a lawful monad instance
instance Monad Extract where
(>>=) :: Extract a -> (a -> Extract b) -> Extract b
@ -57,3 +55,8 @@ instance Monad Extract where
Fail -> Fail
-}
runExtract :: ByteString -> Extract b -> Maybe (ByteString, b)
runExtract string (Extract computation) = case computation string of
Fail -> Nothing
Done rest x -> Just (rest, x)