Crate serde_bencoded

source ·
Expand description

Yet another encoding/decoding library for bencode.

I believe deserializer don’t allocate memory. Serializer allocates only when sort_dictionary feature enabled. I think this crate is fast enough.

§Examples

For .torrent parsing example see examples directory

§Caveats

serde treats [u8; N], Vec<u8>, &[u8] like any other sequence, that is it will be encoded as list of bytes(not a byte string).

Solution - use serde_bytes.

§Serializing map with Option values

You will need to write a custom ser/de helper. For inspiration see https://github.com/serde-rs/serde/issues/550#issuecomment-246746639

§Serializing Option

Use #[serde(skip_serializing_if = "Option::is_none")] annotation or see serde_with::skip_serializing_none

§Behaviour

This crate does sort dictionary by keys if sort_dictionary feature is enabled (enabled by default). Otherwise order of elements can vary (depends on Serialize trait implementation).

§Mapping of rust types to bencode

§Alternatives

Arbitrary order. Search more on crates.io or lib.rs.

§Bencoding

§Supported data types

§Byte Strings

<base ten ASCII>:<string bytes>

Examples:

  • 4:abcd
  • 0:
§Integers

i<base ten ASCII, optional minus sign>e

The maximum number is not specified. This crate handles integers as u64 or i64.

Examples:

  • i123456e
  • i-5e

Malformed:

  • i03e, anything that starts with 0, except i0e
  • i-0e
§Lists

l<bencode type values>e

Examples:

  • li0ei1ei2ee == [1,2,3]
  • le == []
§Dictionaries

d<bencoded string><bencoded element>e

Keys must be sorted as raw strings. string’s should be compared using a binary comparison.

Examples:

  • de == {}
  • d4:rustl2:is7:awesomeee == {"rust" => ["is", "awesome"]}

§Crate features

§sort_dictionary

Enables sort by keys when serializing to bencode dictionary.

Structs§

Enums§

Functions§

  • Deserializes bencoded bytes to rust’s value.
  • The same as from_bytes but deserialize_any will deserialize byte string as str if input bytes are valid UTF-8, otherwise as bytes.
  • Deserializes bencoded &str to rust’s value.
  • The same as from_str but deserialize_any will deserialize byte string as str if input bytes are valid UTF-8, otherwise as bytes
  • Serializes rust’s type to bencode string
  • Convenient function to get encoded value as bytes
  • Serializes rust’s type to bencode using Write trait

Type Aliases§