serde-yaml icon indicating copy to clipboard operation
serde-yaml copied to clipboard

Is there a way to get `ErrorKind` ?

Open XyLyXyRR opened this issue 3 years ago • 0 comments

Hi, there.

use serde::Deserialize;
use std::{fs, io::ErrorKind};

fn main() {
    #[derive(Deserialize)]
    struct One {
        _one: Option<bool>,
        _two: Option<bool>,
    }

    if let Err(e) = fs::read_to_string("jqoifnjiosdj") {
        match e.kind() {
            ErrorKind::NotFound => println!("is ok"),
            _ => println!("no, is not ok"),
        }
    }

    if let Err(e) = serde_yaml::from_str::<One>("") {
        // match e.kind() {}
        println!("this is not ok: {}", e);
    }

    if serde_yaml::from_str::<One>("something:").is_ok() {
        println!("this is ok");
    }
}

Is there a way to get the error kind like std::io::ErrorKind? I want ignore empty input, use default in this case, but I can't get the error kind.

XyLyXyRR avatar Dec 04 '21 04:12 XyLyXyRR

As of new versions of serde_yaml I think your program just does what you want.

is ok
this is ok

dtolnay avatar Aug 14 '22 01:08 dtolnay