No description
  • Elixir 98.6%
  • Earthly 1.4%
Find a file
2026-05-27 18:01:01 +00:00
.woodpecker Update docker.io/elixir Docker tag to v1.19.5 2026-01-17 10:42:48 +00:00
benchmark Add a decoding benchmark 2020-10-15 22:51:05 -06:00
config New Mix project 2017-12-20 13:08:01 -06:00
lib Use Earthly for builds 2022-04-29 16:56:35 -06:00
test Remove "performance test" 2020-10-15 22:08:26 -06:00
.gitignore New Mix project 2017-12-20 13:08:01 -06:00
CODE_OF_CONDUCT.md Misc doc changes 2020-11-09 20:50:20 +08:00
CONTRIBUTING.md Add project docs, improve README 2020-11-08 18:05:17 -07:00
Earthfile Use Earthly for builds 2022-04-29 16:56:35 -06:00
LICENSE Add project docs, improve README 2020-11-08 18:05:17 -07:00
mix.exs Update dependency ex_doc to ~> 0.40 2026-01-21 22:08:32 +00:00
mix.lock Update dependency ex_doc to v0.40.2 2026-05-27 18:00:59 +00:00
README.md fix(docs): update CI badge [ci skip] 2026-05-27 11:19:49 -06:00
renovate.json Update renovate.json 2026-05-27 15:59:12 +00:00

ExBencode

status-badge Module Version Hex Docs Total Download License

An Elixir library for encoding and decoding BitTorrent's bencoding.

Installation

Add ex_bencode to your list of dependencies in mix.exs:

def deps do
  [
    {:ex_bencode, "~> 2.0.1"}
  ]
end

Usage

Just give ExBencode.decode/1 a binary. It returns a success or error tuple.

iex> ExBencode.decode("i10e")
{:ok, 10}

Give ExBencode.encode/1 any Elixir term. It returns a success or error tuple as well.

iex> ExBencode.encode("hi!")
{:ok, "3:hi!"}

The bang versions of these functions, encode!/1 and decode!/1, return the value without an error tuple, but will raise if there is an error.

To customize how your structs serialize, implement the Bencode protocol.

defimpl Bencode, for: MyStruct do
  def encode(term) do
    term |> Map.from_struct() |> ExBencode.encode()
  end
end

Benchmarks

A benchmark comparing this library's performance against Bento is given in the benchmarks/ directory. Run it using mix run

$ mix run benchmark/benchmark.exs

Results on my machine show that this library is just a teensy bit faster than Bento.

Maintainer

This project was developed by Rosa Richter. You can get in touch with her on Matrix.

Contributing

Questions and pull requests are more than welcome. I follow Elixir's tenet of bad documentation being a bug, so if anything is unclear, please file an issue! Ideally, my answer to your question will be in an update to the docs.

Please see CONTRIBUTING.md for all the details you could ever want about helping me with this project.

Note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

License

MIT License

Copyright 2020 Rosa Richter.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.