# bencodex-rs
[](https://github.com/bencodex/bencodex-rs/actions/workflows/build.yaml) [](https://codecov.io/gh/moreal/bencodex-rs) [](https://docs.rs/bencodex-rs/latest/)
The [Rust] implementation of [Bencodex].
- **Correctness** - Implement Bencodex spec and passed tests with its testsuites.
- **[Bencodex JSON]** - Support encoding Bencodex to JSON and decoding JSON to Bencodex.
- **Feature flags** - Support `json`, `json-cli` feature flags to minimize binary size in use.
[Rust]: https://rust-lang.org/
[Bencodex]: https://bencodex.org/
## Bencodex JSON feature
bencodex-rs implements [Bencodex JSON] feature, encoding and decoding both.
To use Bencodex JSON feature, you should enable `json` feature.
```toml
bencodex-rs = { version = "<VERSION>", features = ["json"] }
```
[Bencodex JSON]: https://github.com/planetarium/bencodex/blob/main/JSON.md
### Encoding to JSON
To encode from Bencodex to JSON, you can use `to_json` function.
```rust
use bencodex::{ BencodexValue, json::to_json };
let json = to_json(&BencodexValue::Null);
println!("{}", json);
```
There are two ways to encode `BencodexValue::Binary` type, `Hex` and `Base64`. You can choose one way with `bencodex::json::BinaryEncoding`. And you can pass it with `bencodex::json::JsonEncodeOptions` to `bencodex::json::to_json_with_options`.
```rust
use bencodex::BencodexValue;
use bencodex::json::{ BinaryEncoding, JsonEncodeOptions, to_json_with_options };
let json = to_json_with_options(&BencodexValue::Null, JsonEncodeOptions {
binary_encoding: BinaryEncoding::Base64,
});
println!("{}", json);
```
### Decoding from JSON
To decode from JSON to Bencodex, you can use `from_json_string` and `from_json` function.
```rust
// from_json_string
use bencodex::{ BencodexValue, json::from_json_string };
let result = from_json_string("null");
assert!(result.is_ok());
assert_eq!(result.unwrap(), BencodexValue::Null);
```
```rust
// from_json
use serde_json::from_str;
use bencodex::{ BencodexValue, json::from_json };
let json = from_str("null").unwrap();
let result = from_json(&json);
assert!(result.is_ok());
assert_eq!(result.unwrap(), BencodexValue::Null);
```
### CLI Tool
Also, it provides a CLI tool to encode from Bencodex to JSON and to decode from JSON to Bencodex. You can install it with `json-cli` feature like the below line:
```bash
cargo install bencodex-rs --features json-cli
```
You can use like the below:
```bash
# encode
$ echo -n 'i123e' | bencodex
"123"
$ echo -n '1:\x12' | bencodex
"0x12"
$ echo -n '1:\x12' | bencodex --base64
"b64:Eg=="
# decode
$ echo -n 'null' | bencodex -d
n
```