basilisp.contrib.bencode

protocolBEncodeable
fn (to-bencode-encodeable* this)

Return an object which can be bencoded.

fn (decode data {:as opts :keys [keywordize-keys key-fn]})

Decode the first value in the bencoded data bytes according to opts and return a [decoded* rest*] vector.

The decoded* item in the vector is the decoded value of the first item in data while rest* is the remaining unencoded values.

If data cannot be decoded (e.g. is incomplete or an error occurred), it returns a [nil data] vector.

opts is a map with the following optional supported keys.

keyword :keywordize-keys:

if the decoded value is a map, keywordize its keys.

keyword :key-fn:

a function which will be called for each key-in a map; cannot be specified if :keywordize-keys is also specified

keyword :string-fn:

a function which will be called for each byte string which is not a map key; default is basilisp.core/identity

fn (decode-all data)
fn (decode-all data opts)

Decode all values in the bencoded data bytes and return them as a [values* incomplete*] vector.

The values* item is a collection of the data decoded values, while incomplete* is the rest of the data bytes that could not be decoded or nil.

opts is a map supporting the same keys as decode.

fn (encode d)

Encode the object d into a byte string using bencode encoding.

encode supports encoding the following types:

  • bytes

  • int

  • str, which is first decided to UTF-8 bytes

  • keywords and symbols, which are first converted to strings (including namespace, separated by ‘/’) and then converted using the rules for str s

  • Python list

  • tuple

  • Basilisp lists and vectors

  • dict

  • maps

Mapping type keys must one of: keywords, symbols, or strings.

This function does not support float because the bencode specification does not support non-integer numerics.

Set types (including frozenset, set, or Basilisp’s set types) are not supported due to the requirement that lists retain their original element ordering.