TSFoster/elm-bytes-extra/1.0.2

NOTE: the latest version is 1.3.0

Bytes.Decode.Extra

Helpers for working with Bytes.Decoders.

list : Int -> Decoder a -> Decoder (List a)

Run a decoder a set amount of times, and collect the result in a list.

import Bytes.Decode exposing (Decoder)
import Bytes.Encode
import Bytes.Encode.Extra


myDecoder : Decoder (List String)
myDecoder =
    list 3 (Bytes.Decode.string 5)


[ "HELLO", "WORLD", "third" ]
    |> Bytes.Encode.Extra.list Bytes.Encode.string
    |> Bytes.Encode.encode
    |> Bytes.Decode.decode myDecoder
--> Just [ "HELLO", "WORLD", "third" ]
byteValues : Int -> Decoder (List Int)

Before the release of [elm/bytes], many packages would use List Int to represent bytes. byteValues aids interaciton between those packages and Bytes.

import Hex
import Bytes exposing (Bytes)
import Bytes.Decode exposing (Decoder)
import Bytes.Extra exposing (fromByteValues)


input : Bytes
input = fromByteValues [ 0x32, 0xff, 0x53 ]


decoder : Decoder (List String)
decoder =
    byteValues (Bytes.width input)
      |> Bytes.Decode.map (List.map Hex.toString)


Bytes.Decode.decode decoder input
--> Just [ "32", "ff", "53" ]

Pipeline-style

Json.Decode.Pipeline provides a neat, scalable way for decoding JSON. This module provides a few helpers for adopting a similar style when decoding Bytes.

It is important to note that decoding bytes poses more restrictions than decoding JSON, as bytes are consumed in order, and a maximum of one time. This makes an equivalent to Json.Decode.Pipeline.optional difficult to impossible.

import Bytes exposing (Endianness(..))
import Bytes.Decode exposing (Decoder, succeed, andThen, unsignedInt32, string, float64, decode)
import Bytes.Extra exposing (fromByteValues)


type Status = Downloaded | LocallyGenerated


type alias MyData =
  { id: Int
  , status : Status
  , name: String
  , value: Float
  }


myDataDecoder : Decoder MyData
myDataDecoder =
    succeed MyData
        |> andMap (unsignedInt32 BE)
        |> hardcoded Downloaded
        |> andMap (unsignedInt32 BE |> andThen string)
        |> andMap (float64 BE)




decode myDataDecoder (fromByteValues [0,0,0,252,0,0,0,8,116,104,101,95,110,97,109,101,63,224,0,0,0,0,0,0])
--> Just
-->     { id = 252
-->     , status = Downloaded
-->     , name = "the_name"
-->     , value = 0.5
-->     }
andMap : Decoder a -> Decoder (a -> b) -> Decoder b

andMap behaves in a similar way to Json.Decode.Pipeline.required. It is named andMap to match the naming of similar functions in other packages, and because this package does not provide an equivalent to Json.Decode.Pipeline.optional.

modelDecoder : Decoder Model
modelDecoder =
    succeed Model
        |> andMap (unsignedInt32 BE)
        |> andMap (unsignedInt32 BE |> andThen string)
        |> andMap myCustomTypeDecoder
        |> andMap myCustomBooleanDecoder
hardcoded : a -> Decoder (a -> b) -> Decoder b

A neat way to fill in predetermined information that's not part of the data being decoded, similar to Json.Decode.Pipeline.hardcoded.

modelDecoder : Decoder Model
modelDecoder =
    succeed Model
        |> andMap (unsignedInt32 BE)
        |> andMap (unsignedInt32 BE |> andThen string)
        |> hardcoded []
        |> andMap myCustomBooleanDecoder
        |> hardcoded SubModel.default

Extra maps

(a -> b -> c -> d -> e -> f -> result)
-> Decoder a
-> Decoder b
-> Decoder c
-> Decoder d
-> Decoder e
-> Decoder f
-> Decoder result
(a -> b -> c -> d -> e -> f -> g -> result)
-> Decoder a
-> Decoder b
-> Decoder c
-> Decoder d
-> Decoder e
-> Decoder f
-> Decoder g
-> Decoder result
(a -> b -> c -> d -> e -> f -> g -> h -> result)
-> Decoder a
-> Decoder b
-> Decoder c
-> Decoder d
-> Decoder e
-> Decoder f
-> Decoder g
-> Decoder h
-> Decoder result