Specification
Bencoding
is a way to specify and organize data in a terse format. It supports the following types: byte strings, integers, lists, and dictionaries.
Bencoding
is used in .torrent files
- Encode case classes to bencoding
- Decode bencoding to case classes
libraryDependencies += "com.minosiants" %% "benc" % <version>
final case class Id(id: String)
final case class Author(name: String, age: Option[Int])
final case class Book(id: Id, author: Author, content: BitVector, pages: Long)
val book = Book(...)
//there are several ways to do conversion
val bits:Either[Error, BitVector] = Benc.toBenc[Book](book)
val backToBook:Either[Error, Book] = bits.flatMap(b => Benc.fromBenc[Book](b))
//Using decoder adn encode directly
BEncoder[Book].encode(book).flatMap(bt => BDecoder[Book].decode(bt))
//Using codec
val codec = BCodec[Book]
codec.encode(book).flatMap(bt => codec.decode(bt))
//Setting different name
case class Pen(@BencKey("name") brand:String)