View Source Bento (bento v1.0.0)
An incredibly fast, correct, pure-Elixir Bencoding library.
This module contains high-level methods to encode and decode Bencoded data.
Link to this section Summary
Functions
Decode bencoded data to a value.
Decode bencoded data to a value, raising an exception on error.
Bencode a value.
Bencode a value, raising an exception on error.
Bencode a value as iodata.
Bencode a value as iodata, raises an exception on error.
Like decode
, but ensures the data is a valid torrent metainfo file.
Like decode!
, but ensures the data is a valid torrent metainfo file.
Link to this section Functions
@spec decode(iodata(), Bento.Decoder.opts()) :: {:ok, Bento.Decoder.t()} | failure when failure: {:error, Bento.Decoder.decode_err()}
Decode bencoded data to a value.
iex> Bento.decode("li1e3:twoli3eee")
{:ok, [1, "two", [3]]}
Use :as
as option to transform the parsed value into a struct.
defmodule User do
defstruct name: "John", age: 27
end
Bento.decode("d4:name3:Bobe", as: %User{})
# {:ok, %User{name: "Bob", age: 27}}
@spec decode!(iodata(), Bento.Decoder.opts()) :: Bento.Decoder.t() | no_return()
Decode bencoded data to a value, raising an exception on error.
iex> Bento.decode!("li1e3:twoli3eee")
[1, "two", [3]]
Use :as
as option to transform the parsed value into a struct.
defmodule User do
defstruct name: "John", age: 27
end
Bento.decode!("d4:name3:Bobe", as: %User{})
# %User{name: "Bob", age: 27}
@spec encode(Bento.Encoder.bencodable(), Keyword.t()) :: success | failure when success: {:ok, Bento.Encoder.t() | String.t()}, failure: {:error, Bento.Encoder.encode_err()}
Bencode a value.
iex> Bento.encode([1, "two", [3]])
{:ok, "li1e3:twoli3eee"}
@spec encode!(Bento.Encoder.bencodable(), Keyword.t()) :: success | no_return() when success: Bento.Encoder.t() | String.t()
Bencode a value, raising an exception on error.
iex> Bento.encode!([1, "two", [3]])
"li1e3:twoli3eee"
@spec encode_to_iodata(Bento.Encoder.bencodable(), Keyword.t()) :: success | failure when success: {:ok, iodata()}, failure: {:error, Bento.Encoder.encode_err()}
Bencode a value as iodata.
iex> Bento.encode_to_iodata([1, "two", [3]])
{:ok, [108, [[105, "1", 101], ["3", 58, "two"], [108, [[105, "3", 101]], 101]], 101]}
@spec encode_to_iodata!(Bento.Encoder.bencodable(), Keyword.t()) :: iodata() | no_return()
Bencode a value as iodata, raises an exception on error.
iex> Bento.encode_to_iodata!([1, "two", [3]])
[108, [[105, "1", 101], ["3", 58, "two"], [108, [[105, "3", 101]], 101]], 101]
@spec torrent(iodata()) :: {:ok, Bento.Metainfo.Torrent.t()} | failure when failure: {:error, Bento.Decoder.decode_err() | String.t()}
Like decode
, but ensures the data is a valid torrent metainfo file.
@spec torrent!(iodata()) :: Bento.Metainfo.Torrent.t() | no_return()
Like decode!
, but ensures the data is a valid torrent metainfo file.