well-typed / cborg

Binary serialisation in the CBOR format
https://hackage.haskell.org/package/cborg
191 stars 86 forks source link

How to decode data based on tags #338

Open Forist2034 opened 2 weeks ago

Forist2034 commented 2 weeks ago

Maybe provide something like peekToken or Alternative instance of Decoder.

Like this:

decodeIpAddress =
    peekToken >>= \case
        TkTag 52 -> decodeIpv4 -- decoder for ipv4 address
        TkTag 54 -> decodeIpv6
        _ -> fail "invalid ip address"