Read multiple yamls from the same file

I try to build a yaml checker. It just opens files, tries to parse it and returns an error if, for example, there are keys defined multiple times.
I did it with serde_yaml, but if I have more than one yaml defined in a file, I get "deserializing from YAML containing more than one document is not supported".

Is there a way to solve that?

here's my code so far.

use walkdir::WalkDir;
use serde_yaml;

fn main() {
    for entry in WalkDir::new(".").into_iter().filter_map(|e| e.ok()) {
        if entry.path().display().to_string().ends_with(".yml") || entry.path().display().to_string().ends_with(".yaml") {
            println!("{}", entry.path().display());
            let f = match std::fs::File::open(entry.path()) {
                Err(e) => {
                    println!("{}", e);
                    continue;
                }
                Ok(f) => f,
            };

            let _ :serde_yaml::Value = match serde_yaml::from_reader(f) {
                Err(e) => {
                    println!("{}", e);
                    continue;
                }
                Ok(s) => s,
            };
        }
    }
}

You should probably follow the example in the documentation and use the Iterator impl of the Deserializer.

1 Like

thanks, I didn't see that. Now it works perfectly fine.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.