Go to file
David Tolnay 77236b0d50
Ignore dead code lint in tests
New in nightly-2024-03-24 from https://github.com/rust-lang/rust/pull/119552.

    warning: field `b` is never read
      --> tests/test_error.rs:53:13
       |
    52 |     pub struct A {
       |                - field in this struct
    53 |         pub b: Vec<B>,
       |             ^
       |
       = note: `A` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
       = note: `#[warn(dead_code)]` on by default

    warning: field `0` is never read
      --> tests/test_error.rs:57:11
       |
    57 |         C(C),
       |         - ^
       |         |
       |         field in this variant
       |
    help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field
       |
    57 |         C(()),
       |           ~~

    warning: field `d` is never read
      --> tests/test_error.rs:61:13
       |
    60 |     pub struct C {
       |                - field in this struct
    61 |         pub d: bool,
       |             ^
       |
       = note: `C` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

    warning: fields `v` and `w` are never read
      --> tests/test_error.rs:82:13
       |
    81 |     pub struct Basic {
       |                ----- fields in this struct
    82 |         pub v: bool,
       |             ^
    83 |         pub w: bool,
       |             ^
       |
       = note: `Basic` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

    warning: field `c` is never read
       --> tests/test_error.rs:107:13
        |
    106 |     pub struct Wrapper {
        |                ------- field in this struct
    107 |         pub c: (),
        |             ^
        |
        = note: `Wrapper` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

    warning: field `0` is never read
       --> tests/test_error.rs:160:11
        |
    160 |         V(usize),
        |         - ^^^^^
        |         |
        |         field in this variant
        |
    help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field
        |
    160 |         V(()),
        |           ~~

    warning: field `0` is never read
       --> tests/test_error.rs:212:15
        |
    212 |         Inner(Inner),
        |         ----- ^^^^^
        |         |
        |         field in this variant
        |
    help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field
        |
    212 |         Inner(()),
        |               ~~

    warning: field `0` is never read
       --> tests/test_error.rs:216:17
        |
    216 |         Variant(Vec<usize>),
        |         ------- ^^^^^^^^^^
        |         |
        |         field in this variant
        |
    help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field
        |
    216 |         Variant(()),
        |                 ~~

    warning: field `0` is never read
       --> tests/test_error.rs:245:11
        |
    245 |         V(usize),
        |         - ^^^^^
        |         |
        |         field in this variant
        |
    help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field
        |
    245 |         V(()),
        |           ~~

    warning: fields `x` and `y` are never read
       --> tests/test_error.rs:260:13
        |
    259 |     pub struct Struct {
        |                ------ fields in this struct
    260 |         pub x: usize,
        |             ^
    261 |         pub y: usize,
        |             ^
        |
        = note: `Struct` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

    warning: field `x` is never read
       --> tests/test_error.rs:334:13
        |
    333 |     pub struct S {
        |                - field in this struct
    334 |         pub x: [i32; 1],
        |             ^
        |
        = note: `S` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

    warning: field `x` is never read
       --> tests/test_error.rs:347:13
        |
    346 |     pub struct S {
        |                - field in this struct
    347 |         pub x: Option<Box<S>>,
        |             ^
        |
        = note: `S` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

    warning: fields `0` and `1` are never read
       --> tests/test_error.rs:359:18
        |
    359 |     pub struct S(pub usize, pub Option<Box<S>>);
        |                - ^^^^^^^^^  ^^^^^^^^^^^^^^^^^^
        |                |
        |                fields in this struct
        |
        = note: `S` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
    help: consider changing the fields to be of unit type to suppress this warning while preserving the field numbering, or remove the fields
        |
    359 |     pub struct S((), ());
        |                  ~~  ~~

    warning: field `0` is never read
       --> tests/test_error.rs:370:18
        |
    370 |     pub struct S(pub Option<Box<S>>);
        |                - ^^^^^^^^^^^^^^^^^^
        |                |
        |                field in this struct
        |
        = note: `S` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
    help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field
        |
    370 |     pub struct S(());
        |                  ~~

    warning: field `x` is never read
       --> tests/test_error.rs:382:13
        |
    381 |     pub struct S {
        |                - field in this struct
    382 |         pub x: Option<Box<S>>,
        |             ^
        |
        = note: `S` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

    warning: fields `0` and `1` are never read
       --> tests/test_error.rs:394:18
        |
    394 |     pub struct S(pub usize, pub Option<Box<S>>);
        |                - ^^^^^^^^^  ^^^^^^^^^^^^^^^^^^
        |                |
        |                fields in this struct
        |
        = note: `S` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
    help: consider changing the fields to be of unit type to suppress this warning while preserving the field numbering, or remove the fields
        |
    394 |     pub struct S((), ());
        |                  ~~  ~~
2024-03-23 20:04:05 -07:00
.github Test docs.rs documentation build in CI 2023-09-24 10:53:43 -07:00
fuzz Update fuzz crate gitignore to ignore coverage dir 2023-03-25 21:35:20 -07:00
src Release 0.9.33 2024-03-16 22:56:45 -07:00
tests Ignore dead code lint in tests 2024-03-23 20:04:05 -07:00
.gitignore Add redirect from top-level index.html 2016-02-24 23:55:19 -08:00
Cargo.toml Release 0.9.33 2024-03-16 22:56:45 -07:00
LICENSE-APACHE Sync license text with rust-lang repos 2022-12-30 12:00:51 -08:00
LICENSE-MIT Remove pseudo copyright lines and boilerplate 2019-04-30 02:13:04 -07:00
README.md Update build status badge 2022-12-15 17:52:43 -08:00

README.md

Serde YAML

github crates.io docs.rs build status

This crate is a Rust library for using the Serde serialization framework with data in YAML file format.

Dependency

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

Release notes are available under GitHub releases.

Using Serde YAML

API documentation is available in rustdoc form but the general idea is:

use std::collections::BTreeMap;

fn main() -> Result<(), serde_yaml::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::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::from_str(&yaml)?;
    assert_eq!(map, deserialized_map);
    Ok(())
}

It can also be used with Serde's derive macros to handle structs and enums defined in your program.

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

Structs serialize in the obvious way:

use serde::{Serialize, Deserialize};

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

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

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

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

Enums serialize using YAML's !tag syntax to identify the variant name.

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::Error> {
    let yaml = "
        - !Newtype 1
        - !Tuple [0, 0, 0]
        - !Struct {x: 1.0, y: 2.0}
    ";
    let values: Vec<Enum> = serde_yaml::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::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::from_str(yaml).unwrap();
    assert_eq!(values[0], Enum::Unit);
    assert_eq!(values[1], Enum::Unit);

    Ok(())
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.