chyh1990 / yaml-rust

A pure rust YAML implementation.
Apache License 2.0
601 stars 138 forks source link

Doesn't seem possible to iteratate over results #170

Closed mabushey closed 3 years ago

mabushey commented 3 years ago

I'm pretty new to Rust. I've googled a few pages on this and either they're all wrong or it's changed. I'm using Rust 1.47.0 on Ubuntu. I have a config file in yaml.

println!("Config junk_file: {:?}", &doc["junk_file"]); ->

Config junk_file: Array([Hash({String("junk.txt"): String("24b5235d525a954ab98798cdbf5b3663")}), Hash({String("more_junk.exe"): String("d5a5d8421b2bd35ea6ab81f9d77efb3f")})])

I would like to do this (non-working code):

for (key, val) in doc["junk_file"].iter() {                                                                                                                                                                                                                 
    println!("{} / {}", key, val);                                                                                                                                                                                                                               
}

A lot of examples use iter_mut() or something like that. I want to read the values, not change them.

mabushey commented 3 years ago

Is there a better way to read in a yaml file as a hashmap so I could use .iter(), contains_key(), get()? The println() shows have a Hash, which I believe should be a Hashmap, but it seems gimped. Would using serde::yaml give me a hashmap with these traits?

mabushey commented 3 years ago

This works great:

   let mut junk = std::collections::HashMap::new();                                                                                                                                                                                                                              

   junk.insert("junk.txt", "24b5235d525a954ab98798cdbf5b3663");                                                                                                                                                                                                                  
   junk.insert("more_junk.exe", "d5a5d8421b2bd35ea6ab81f9d77efb3f");                                                                                                                                                                                                             

   println!("junk hashmap: {:?}", junk);                                                                                                                                                                                                                                         
   if junk.contains_key("junk.txt") {                                                                                                                                                                                                                                            
     println!("Match, hash is: {}", junk.get("junk.txt").unwrap());                                                                                                                                                                                                              
   }  
junk hashmap: {"more_junk.exe": "d5a5d8421b2bd35ea6ab81f9d77efb3f", "junk.txt": "24b5235d525a954ab98798cdbf5b3663"}
Match, hash is: 24b5235d525a954ab98798cdbf5b3663

I'm not sure what's wrong with this crate but it seems like a much better solution is to just read the file in and use a regular expression to grab the values and insert into a hashmap. I'm not using any anchors or aliases in my yaml files so I think this this will be a much cleaner, robust, and working solution.

mabushey commented 3 years ago

I was able use the serde-yaml crate (which uses this). I guess there's a reason for the serde middle-ware.