# Rust

> Use the bytepeel-core crate to decode, select and edit data from Rust, with no dependencies by default.

[`bytepeel-core`](https://crates.io/crates/bytepeel-core) is the library
behind the CLI. It has no dependencies by default and no `unsafe` code.

```sh
cargo add bytepeel-core                          # no compression codecs
cargo add bytepeel-core --features deflate,zstd  # gzip, zlib and zstd too
```

```rust
use bytepeel_core::{FieldValue, Op, Options, Pick, edit, get, parse_value, peel};

let opts = Options::default();

// base64 -> protobuf -> a string field that is base64 again -> text.
let node = peel(b"ChRTR1ZzYkc4c0lIZHZjbXhrSVNFPRAq", &opts).unwrap();
println!("{}", bytepeel_core::to_json(&node)); // the whole tree as JSON

// Select one item by path (paths can cross formats, e.g. into a
// certificate held in a protobuf field).
match get(&node, "2").unwrap() {
    Pick::Field(f) => assert!(matches!(f.value, FieldValue::Varint { value: 42, .. })),
    _ => unreachable!(),
}

// Edit and re-encode every layer in its original style (here: hex).
let out = edit(b"1a 03 08 96 01", &opts, &Op::Set("3.1".into(), parse_value("300000").unwrap())).unwrap();
assert_eq!(out, b"1a 04 08 e0 a7 12");
```

- `peel(input, &opts)` returns the node tree; `to_json` and `to_tree` render
  it as the CLI does.
- `get(&node, path)` returns a `Pick`; `Pick::payload()` gives the bytes that
  `--format raw` prints, and `innermost(&node, input)` the innermost layer's
  bytes.
- `edit(input, &opts, &Op::{Set, Add, Del})` applies one edit and verifies
  it; `set(..)` is `edit` with `Op::Set`.

## Features

| Feature | Adds | Dependency |
|---|---|---|
| `deflate` | gzip and zlib layers | `miniz_oxide` |
| `zstd` | Zstandard layers | `ruzstd` |

Both are off by default and pure Rust. Without a feature, a stream it would
decode is a `bytes` leaf with an `UNSUPPORTED_FORMAT` warning naming the
missing feature. The full API is on [docs.rs](https://docs.rs/bytepeel-core).
