Skip to content

Instantly share code, notes, and snippets.

@rorymckinley
Created November 18, 2019 17:37
Show Gist options
  • Save rorymckinley/f392b9e1eb9ac4ae0265ee92e974adfa to your computer and use it in GitHub Desktop.
Save rorymckinley/f392b9e1eb9ac4ae0265ee92e974adfa to your computer and use it in GitHub Desktop.
Parsing yaml file and using an array as input to args() for command
---
marking:
command:
arguments:
- 8
// Not all of the below are needed for the code snippet
// What I would like to do is parse the yaml and convert it to a Vec<&str>
use std::env;
use std::process::Command;
use git2::Repository;
use std::fs;
use std::io::prelude::*;
use std::str;
use yaml_rust::YamlLoader;
let answer_config_contents = fs::read_to_string(format!("{}/answers/{}/config.yml", repo_path, exercise)).unwrap();
let answer_config = YamlLoader::load_from_str(&answer_config_contents).unwrap();
let command_args = match answer_config[0]["marking"]["command"]["arguments"].as_vec() {
Some(vec) => vec,
_ => panic!("No array found"),
};
let command_strings: Vec<&str> = command_args.iter().map( |i| {
println!("{:?}", i.as_i64());
match i.as_i64() {
Some(val) => &val,
None => panic!("Got None")
}
}).collect();
// Compiler Error: a collection of type `std::vec::Vec<&str>` cannot be built from an iterator over elements of type `&i64`
let answer = Command::new(format!("./{}", source_file))
.args(command_strings)
.output()
.expect("Could not execute source file")
.stdout;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment