#serde-yaml #serialization #serde

serde_yaml_ok

为Serde提供的YAML数据格式。它没问题。

1个不稳定版本

0.9.36 2024年6月7日
0.9.35 2024年6月7日

编码中排名第289

Download history 344/week @ 2024-06-07 191/week @ 2024-06-14 151/week @ 2024-06-21 111/week @ 2024-06-28 228/week @ 2024-07-05 88/week @ 2024-07-12 83/week @ 2024-07-19 189/week @ 2024-07-26 237/week @ 2024-08-02 101/week @ 2024-08-09 119/week @ 2024-08-16

每月下载量664

MIT/Apache

345KB
9K SLoC

Serde YAML,OK

github crates.io docs.rs build status

Rust库,用于使用Serde序列化框架处理YAML文件格式的数据。

依赖

[dependencies]
serde = "1.0"
serde_yaml_ok = "0.9"

发布说明可在GitHub发布中找到。

使用Serde YAML

API文档以rustdoc形式提供,但基本思路是

use std::collections::BTreeMap;

fn main() -> Result<(), serde_yaml_ok::Error> {
    // You have some type.
    let mut map = BTreeMap::new();
    map.insert("x".to_string(), 1.0);
    map.insert("y".to_string(), 2.0);

    // Serialize it to a YAML string.
    let yaml = serde_yaml_ok::to_string(&map)?;
    assert_eq!(yaml, "x: 1.0\ny: 2.0\n");

    // Deserialize it back to a Rust type.
    let deserialized_map: BTreeMap<String, f64> = serde_yaml_ok::from_str(&yaml)?;
    assert_eq!(map, deserialized_map);
    Ok(())
}

它也可以与Serde的 derive 宏一起使用,以处理程序中定义的结构体和枚举。

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9"

结构体以明显的方式序列化

use serde::{Serialize, Deserialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Point {
    x: f64,
    y: f64,
}

fn main() -> Result<(), serde_yaml_ok::Error> {
    let point = Point { x: 1.0, y: 2.0 };

    let yaml = serde_yaml_ok::to_string(&point)?;
    assert_eq!(yaml, "x: 1.0\ny: 2.0\n");

    let deserialized_point: Point = serde_yaml_ok::from_str(&yaml)?;
    assert_eq!(point, deserialized_point);
    Ok(())
}

枚举使用YAML的!tag语法来标识变体名称。

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum Enum {
    Unit,
    Newtype(usize),
    Tuple(usize, usize, usize),
    Struct { x: f64, y: f64 },
}

fn main() -> Result<(), serde_yaml_ok::Error> {
    let yaml = "
        - !Newtype 1
        - !Tuple [0, 0, 0]
        - !Struct {x: 1.0, y: 2.0}
    ";
    let values: Vec<Enum> = serde_yaml_ok::from_str(yaml).unwrap();
    assert_eq!(values[0], Enum::Newtype(1));
    assert_eq!(values[1], Enum::Tuple(0, 0, 0));
    assert_eq!(values[2], Enum::Struct { x: 1.0, y: 2.0 });

    // The last two in YAML's block style instead:
    let yaml = "
        - !Tuple
          - 0
          - 0
          - 0
        - !Struct
          x: 1.0
          y: 2.0
    ";
    let values: Vec<Enum> = serde_yaml_ok::from_str(yaml).unwrap();
    assert_eq!(values[0], Enum::Tuple(0, 0, 0));
    assert_eq!(values[1], Enum::Struct { x: 1.0, y: 2.0 });

    // Variants with no data can be written using !Tag or just the string name.
    let yaml = "
        - Unit  # serialization produces this one
        - !Unit
    ";
    let values: Vec<Enum> = serde_yaml_ok::from_str(yaml).unwrap();
    assert_eq!(values[0], Enum::Unit);
    assert_eq!(values[1], Enum::Unit);

    Ok(())
}

许可协议

根据您的选择,许可协议为Apache License, Version 2.0MIT许可
除非您明确说明,否则您提交的任何有意包含在此crate中的贡献,根据Apache-2.0许可证的定义,应如上双许可,不附加任何额外条款或条件。

依赖项

~1.3–1.8MB
~34K SLoC