https://github.com/minosiants/benc
Bencoding library for scala
https://github.com/minosiants/benc
bencoding bittorrent cats fp scala scodec shapeless
Last synced: 29 days ago
JSON representation
Bencoding library for scala
- Host: GitHub
- URL: https://github.com/minosiants/benc
- Owner: minosiants
- License: apache-2.0
- Created: 2020-04-17T11:42:16.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-14T21:49:42.000Z (almost 6 years ago)
- Last Synced: 2024-04-24T08:11:58.717Z (almost 2 years ago)
- Topics: bencoding, bittorrent, cats, fp, scala, scodec, shapeless
- Language: Scala
- Size: 72.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
## Benc
 [](https://javadoc.io/doc/com.minosiants/benc_2.13)
Bencoding library for scala
### Overview
#### Bencoding
[Specification](https://wiki.theory.org/index.php/BitTorrentSpecification#Bencoding)
`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
### Benc functionality
1. Encode case classes to bencoding
2. Decode bencoding to case classes
### Usage
```scala
libraryDependencies += "com.minosiants" %% "benc" %
```
### Example
```scala
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)
```