Bencode
Bencode decoder / encoder using Elixir data structures.
Encode strings (with thrown exceptions)
Bencode.encode!("hello world")
"11:hello world"
Encode integers
Bencode.encode!(42)
"i42e"
Encode lists
Bencode.encode!([1,2,3])
"li1ei2ei3ee"
Encode maps
Bencode.encode!(%{"a" => 1, 2 => "b"})
#"di2e1:b1:ai1ee"
Encode Dicts
Bencode.encode!(HashDict.new |> Dict.put :hello, :world)
"d5:hello5:worlde"
Encode bitstrings
Bencode.encode!(<<1,2,3,4>>)
<<52, 58, 1, 2, 3, 4>>
Encode without exceptions
Bencode.encode(2.0)
{:error, :invalid_format}
Decode strings (with thrown expcetions)
Bencode.decode!("5:hello")
"hello"
Decode integers
Bencode.decode!("i42e")
42
Decode lists
Bencode.decode!("li1ei2ei3ee")
[1, 2, 3]
Decode maps
Bencode.decode!("d5:hello5:worlde")
%{"hello" => "world"}
Decode without exceptions
Bencode.decode("4:spam")
{:ok, "spam"}
Summary↑
decode!(bitstring) | Decode a Bencode bitstring to an Elixir data structure |
decode(bitstring) | Decode a Bencode bitstring to an Elixir data structure |
encode!(data) | Encode Elixir data structures to a Bencode bitstring |
encode(data) | Encode Elixir data structures to a Bencode bitstring |
Types ↑
error_trailing_data :: {:error, :trailing_data}
error_invalid_format :: {:error, :invalid_format}
Functions
Specs:
- decode(binary) :: {:ok, encodable} | bencode_error
Decode a Bencode bitstring to an Elixir data structure.
The function returns either {:ok, data_structure}
on success, or
{:error, :trailing_data}
on unspecified trailing data or
{:error, :invalid_format}
on format errors.
Args:
bitstring
- Bencoded bitstring to decode.
Specs:
- decode!(binary) :: encodable
Decode a Bencode bitstring to an Elixir data structure.
An exception will be thrown if an invalid Bencoded string is provided.
Args:
bitstring
- Bencoded bitstring to decode.
Specs:
- encode(encodable) :: {:ok, binary} | error_invalid_format
Encode Elixir data structures to a Bencode bitstring.
The function will return {:ok, bitstring}
on success or
{:error, :invalid_format}
on failure.
Args:
data
- Data structure to encode.
Specs:
- encode!(encodable) :: binary
Encode Elixir data structures to a Bencode bitstring.
An exception will be thrown if an unsupported data structure is provided.
Args:
data
- Data structure to encode.