#bloom #serde #serialization #version #field #struct

sane

SANE为Rust提供的序列化和反序列化工具

4个版本 (2个重大更新)

0.2.1 2019年3月28日
0.2.0 2019年3月28日
0.1.0 2019年3月28日
0.0.1 2018年12月22日

#969 in 编码

每月32次 下载
2 crates 中使用

Apache-2.0

220KB
5.5K SLoC

SANE-rs

SANE为Rust提供的序列化和反序列化工具


Build Status sane-rs crate sane-rs documentation

规范: https://opensource.bloom.sh/sane

兼容SANE版本: v1.0.0

  1. 使用方法
  2. 贡献
  3. 许可

使用方法

//! An example showing off the usage of `Deserialize` to automatically decode
//! SANE into a Rust `struct`

#![deny(warnings)]

use sane;
use serde::{Deserialize};

/// This is what we're going to decode into. Each field is optional, meaning
/// that it doesn't have to be present in SANE.
#[derive(Debug, Deserialize)]
struct Config {
    global_string: Option<String>,
    global_integer: Option<u64>,
    server: Option<ServerConfig>,
    peers: Option<Vec<PeerConfig>>,
}

/// Sub-structs are decoded from tables, so this will decode from the `[server]`
/// table.
///
/// Again, each field is optional, meaning they don't have to be present.
#[derive(Debug, Deserialize)]
struct ServerConfig {
    ip: Option<String>,
    port: Option<u64>,
}

#[derive(Debug, Deserialize)]
struct PeerConfig {
    ip: Option<String>,
    port: Option<u64>,
}

fn main() {
    let sane_str = r#"
        global_string = "test"
        global_integer = 5
        server = { ip = "127.0.0.1", port = 80 }
        peers = [
            {
                ip = "127.0.0.1",
                port = 8080,
            },
            { ip = "127.0.0.1" },
        ]
"#;

    let decoded: Config = sane::from_str(sane_str).unwrap();
    println!("{:#?}", decoded);
}

贡献

感谢您对贡献的兴趣!请参阅 https://opensource.bloom.sh/contributing 以获取指导。

许可

查看 LICENSE.txthttps://opensource.bloom.sh/licensing

alexcrichton 的原始作品改编: toml-rs - 提交 d729bf9c53fcfe8f1e5506a938b88d81526b55a4

依赖项

~110–345KB