Skip to content

Commit

Permalink
Reduce duplication, by switching to locally defined slice function
Browse files Browse the repository at this point in the history
  • Loading branch information
lehins committed Aug 7, 2022
1 parent cbb5887 commit f5dacdc
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions binary/src/Cardano/Binary/Annotated.hs
Expand Up @@ -199,17 +199,14 @@ newtype Annotator a = Annotator { runAnnotator :: FullByteString -> a }
-- | were decoded. This function constructs and supplies the relevant piece.
annotatorSlice :: Decoder s (Annotator (LByteString -> a)) -> Decoder s (Annotator a)
annotatorSlice dec = do
(k,bytes) <- withSlice dec
(k, bytes) <- withSlice dec
pure $ k <*> bytes

-- | Pairs the decoder result with an annotator that can be used to construct the exact bytes used to decode the result.
withSlice :: Decoder s a -> Decoder s (a, Annotator LByteString)
withSlice dec = do
(r, start, end) <- decodeWithByteSpan dec
return (r, Annotator $ sliceOffsets start end)
where
sliceOffsets :: ByteOffset -> ByteOffset -> FullByteString -> LByteString
sliceOffsets start end (Full b) = (BSL.take (end - start) . BSL.drop start) b
Annotated r byteSpan <- annotatedDecoder dec
return (r, Annotator (\(Full bsl) -> slice bsl byteSpan))

-- | Supplies the bytestring argument to both the decoder and the produced annotator.
decodeAnnotator :: Text -> (forall s. Decoder s (Annotator a)) -> LByteString -> Either DecoderError a
Expand Down

0 comments on commit f5dacdc

Please sign in to comment.