#borsh #data #command-line-tool #utility #serialization #schema #manipulating

app borsh-cli

用于操作 Borsh 序列化数据的命令行工具

4 个版本

0.1.3 2022 年 12 月 20 日
0.1.2 2022 年 12 月 6 日
0.1.1 2022 年 12 月 6 日
0.1.0 2022 年 12 月 3 日

#1149编码

每月 23 次下载

GPL-3.0 许可证

47KB
1K SLoC

Borsh CLI

基本 Borsh 序列化数据操作的命令行工具。

安装

$ cargo install borsh-cli

用法

Command-line utility for manipulating Borsh-serialized data

Note: Does not play particularly nicely with `HashMap<_, _>` types.

Usage: borsh[EXE] <COMMAND>

Commands:
  pack
          Serialize the input as a simple binary blob with Borsh headers
  unpack
          Deserialize the input as a simple binary blob with Borsh headers
  encode
          Convert JSON to Borsh
  decode
          Decode Borsh input to JSON
  extract
          Extracts the Borsh schema header
  strip
          Removes the Borsh schema header
  help
          Print this message or the help of the given subcommand(s)

Options:
  -h, --help
          Print help information (use `-h` for a summary)

  -V, --version
          Print version information

通常,除非分别指定了 -/--input-/--output 标志,否则每个子命令都将从 STDIN 读取并输出到 STDOUT。

示例

打包

$ echo 'hello' | borsh pack | base64
BgAAAGhlbGxvCg==

编码

使用模式

适用于大多数用例,尤其是对于结构化数据。

$ cat schema.borshschema | base64
BQAAAEZpcnN0CAAAAAUAAABGaXJzdAQABAAAAAEAAABhDwAAAFR1cGxlPHUzMiwgdTY0PgEAAABi
BgAAAHN0cmluZwEAAABjBgAAAFNlY29uZAEAAABlCwAAAFZlYzxzdHJpbmc+BgAAAFNlY29uZAQA
BQAAAAEAAABhBQAAAFRoaXJkAQAAAGIFAAAAVGhpcmQBAAAAYwUAAABUaGlyZAEAAABkAwAAAHUz
MgEAAABlAwAAAHUzMgUAAABUaGlyZAMDAAAABQAAAEFscGhhCgAAAFRoaXJkQWxwaGEEAAAAQmV0
YQkAAABUaGlyZEJldGEFAAAAR2FtbWEKAAAAVGhpcmRHYW1tYQoAAABUaGlyZEFscGhhBAABAAAA
BQAAAGZpZWxkAwAAAHUzMgkAAABUaGlyZEJldGEEAQEAAAADAAAAdTMyCgAAAFRoaXJkR2FtbWEE
Ag8AAABUdXBsZTx1MzIsIHU2ND4CAgAAAAMAAAB1MzIDAAAAdTY0CwAAAFZlYzxzdHJpbmc+AQYA
AABzdHJpbmc=

$ cat data.json
{
  "a": [32, 64],
  "b": "String",
  "c": {
    "a": { "Alpha": { "field": 1 } },
    "b": { "Beta": 1 },
    "c": "Gamma",
    "d": 2,
    "e": 3
  },
  "e": ["a", "b", "c"]
}

$ borsh encode -i data.json -s schema.borshschema -o data.borsh
$ cat data.borsh | base64
BQAAAEZpcnN0CAAAAAUAAABGaXJzdAQABAAAAAEAAABhDwAAAFR1cGxlPHUzMiwgdTY0PgEAAABi
BgAAAHN0cmluZwEAAABjBgAAAFNlY29uZAEAAABlCwAAAFZlYzxzdHJpbmc+BgAAAFNlY29uZAQA
BQAAAAEAAABhBQAAAFRoaXJkAQAAAGIFAAAAVGhpcmQBAAAAYwUAAABUaGlyZAEAAABkAwAAAHUz
MgEAAABlAwAAAHUzMgUAAABUaGlyZAMDAAAABQAAAEFscGhhCgAAAFRoaXJkQWxwaGEEAAAAQmV0
YQkAAABUaGlyZEJldGEFAAAAR2FtbWEKAAAAVGhpcmRHYW1tYQoAAABUaGlyZEFscGhhBAABAAAA
BQAAAGZpZWxkAwAAAHUzMgkAAABUaGlyZEJldGEEAQEAAAADAAAAdTMyCgAAAFRoaXJkR2FtbWEE
Ag8AAABUdXBsZTx1MzIsIHU2ND4CAgAAAAMAAAB1MzIDAAAAdTY0CwAAAFZlYzxzdHJpbmc+AQYA
AABzdHJpbmcgAAAAQAAAAAAAAAAGAAAAU3RyaW5nAAEAAAABAQAAAAICAAAAAwAAAAMAAAABAAAA
YQEAAABiAQAAAGM=

$ borsh decode -i data.borsh
{"e":["a","b","c"],"b":"String","a":[32,64],"c":{"e":3,"b":{"Beta":[1]},"a":{"Alpha":{"field":1}},"c":{"Gamma":[]},"d":2}}

未使用模式

不推荐用于结构化数据。

$ cat data.json
{
  "definitely_pi": 2.718281828459045,
  "trustworthy": false
}

$ borsh encode -i data.json -o data.borsh
$ cat data.borsh | base64
aVcUiwq/BUAA

注意:字段按照它们出现的顺序进行编码。因此,{"a":1,"b":2} 的编码与 {"b":2,"a":1} 的编码不同。

解码

要求输入文件包含 Borsh 模式头。

剥离

从一些 Borsh 数据中删除模式头并返回剩余的数据。

提取

仅返回一些 Borsh 数据中的模式头。

常见问题解答 (FAQ)

如何为我的数据生成 Borsh 模式头?

包含用于自动为您的数据生成 Borsh 模式头的宏的 Rust crate borsh

use borsh::{BorshSchema, BorshSerialize};

#[derive(BorshSerialize, BorshSchema)]
struct MyStruct { /* ... */ }

fn serialize(data: MyStruct) {
  let serialized_with_schema: Vec<u8> = borsh::try_to_vec_with_schema(&data).unwrap();

  // ...
}

作者

依赖关系

~5.5MB
~101K SLoC