GithubHelp home page GithubHelp logo

elm-bytes-decoder's Introduction

elm-bytes-decoder

Enable branching (oneOf) for elm/bytes decoders.

This package builds on top of elm/bytes#Bytes.Decode and adds functionality to branch decoders with oneOf.

It is heavily inspired by zwilias/elm-bytes-parser and even starts as a fork of it. The key changes are two-fold.

First, on the visible side, this package has removed all context and error handling in order to stay as close as possible to elm/bytes decoders.

And second, the decoding is handled slightly differently. In zwilias/elm-bytes-parser, for every decoder, the code does a double decoding consisting in an bytes offset followed by the actual decoder:

-- extract from zwilias/elm-bytes-parser
fromDecoder : Decoder v -> Int -> Parser context error v
fromDecoder dec byteLength =
    Parser <|
        \state ->
            let
                combined =
                    Decode.map2 (always identity) (Decode.bytes state.offset) dec
            in
            case Decode.decode combined state.input of
                Just res ->
                    Good res { state | offset = state.offset + byteLength }

                Nothing ->
                    Bad (OutOfBounds { at = state.offset, bytes = byteLength })

In contrast, in this elm-bytes-decoder package, decoders are left unchanged.

-- extract from mpizenberg/elm-bytes-decoder
fromDecoder : D.Decoder v -> Int -> Decoder v
fromDecoder decoder byteLength =
    Decoder <|
        \state ->
            D.map
                (\v -> ( { input = state.input, offset = state.offset + byteLength }, v ))
                decoder

This is enabled by the state storing a decoder instead of a fully resolved decoding result.

type Decoder value
    = Decoder (State -> D.Decoder ( State, value ))

type alias State =
    { input : Bytes, offset : Int }

Thanks to this change, we can compose decoders as usual, and only when calling oneOf, backtracking is taken care of, instead of for each decoder. In theory, this should result in more performant decoding. In practice, I didn't notice any significant change.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.