Created
July 4, 2020 16:44
-
-
Save andremedeiros/a419d4c51f34e415bdc83a1c5802e86a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate yaml_rust; | |
use yaml_rust::{Yaml,YamlLoader}; | |
use std::collections::HashMap; | |
#[derive(Debug)] | |
struct Project { | |
name: String, | |
url: String, | |
deps: HashMap<String, String> | |
} | |
impl Default for Project { | |
fn default() -> Project { | |
Project{name: String::from(""), url: String::from(""), deps: HashMap::new()} | |
} | |
} | |
fn main() { | |
println!("Hello, world!"); | |
let s = | |
" | |
name: Some cool project | |
url: https://github.com/andremedeiros/loon | |
deps: | |
- ruby | |
- golang: 1.14.1 | |
"; | |
let docs = YamlLoader::load_from_str(s).unwrap(); | |
// Multi document support, doc is a yaml::Yaml | |
let doc = &docs[0]; | |
// Debug support | |
println!("{:?}", doc); | |
let mut p = Project::default(); | |
match &doc["name"] { | |
Yaml::String(name) => p.name = name.to_string(), | |
_ => { | |
println!("How will people find you without a URL? Come on!"); | |
return; | |
} | |
} | |
match &doc["url"] { | |
Yaml::String(url) => p.url = url.to_string(), | |
Yaml::Null => { | |
println!("How will people find you without a URL? Come on!"); | |
return; | |
} | |
_ => { | |
println!("URL wants to be a string. Sorry."); | |
return; | |
}, | |
} | |
match &doc["deps"] { | |
Yaml::Array(deps) => { | |
for dep in deps.to_vec() { | |
match dep { | |
Yaml::String(name) => { | |
p.deps.insert(name.to_string(), "latest".to_string()); | |
} | |
Yaml::Hash(map) => { | |
map.iter() | |
.for_each(|(n, v)| { | |
let name = n.as_str().unwrap(); | |
let ver = v.as_str().unwrap(); | |
p.deps.insert(name.to_string(), ver.to_string()); | |
}); | |
} | |
_ => { | |
println!("Hey, what are you trying to do?"); | |
return; | |
} | |
} | |
} | |
}, | |
Yaml::Null => (), // deps can be empty | |
_ => { | |
println!("Deps wants to be an array!"); | |
return; | |
} | |
}; | |
println!("{:#?}", p); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment