From 6c52cf8229011c186259663b545d1bf8eeeffad9 Mon Sep 17 00:00:00 2001 From: Mihai Galos Date: Sun, 20 Oct 2019 12:15:07 +0200 Subject: [PATCH] Fix indent in yaml not producing results --- src/dot_generator.rs | 23 +++++++++++++++-------- src/main.rs | 2 +- src/output_writer.rs | 11 +++++++---- src/test_unit.rs | 38 +++++++++++++++++++++++--------------- test/yaml/demo_file1.yaml | 14 +++++++------- test/yaml/demo_file2.yaml | 4 ++-- 6 files changed, 55 insertions(+), 37 deletions(-) diff --git a/src/dot_generator.rs b/src/dot_generator.rs index cd9700e..29c037a 100644 --- a/src/dot_generator.rs +++ b/src/dot_generator.rs @@ -4,9 +4,9 @@ use crate::yaml_parser::YamlParser; pub struct DotGenerator {} impl DotGenerator { - pub fn run(&self, args: Vec) -> String{ + pub fn run(&self, args: Vec) -> String { let yaml_parser = YamlParser {}; - let mut result : String; + let mut result: String; result = OutputWriter::prologue(); for file in &args[1..] { @@ -18,8 +18,8 @@ impl DotGenerator { result } - fn generate_dot(&self, jobs: yaml_rust::Yaml) -> String{ - let mut result : String = "".to_string(); + fn generate_dot(&self, jobs: yaml_rust::Yaml) -> String { + let mut result: String = "".to_string(); let hexified_rgb = RandomColorGenerator::get_random_color().to_owned(); let hex_color = "#".to_owned() + &hexified_rgb; @@ -27,9 +27,16 @@ impl DotGenerator { let mut job_name: String = "".to_string(); let mut job_parent: String = "".to_string(); - for job in jobs.as_vec().unwrap() { - for field in job.as_hash().unwrap() { - result += &self.generate_node_and_edge(field, &hex_color, &mut job_name, &mut job_parent); + for hashed_job in jobs.as_vec().unwrap() { + for job in hashed_job.as_hash().unwrap() { + for field in job.1.as_hash().unwrap() { + result += &self.generate_node_and_edge( + field, + &hex_color, + &mut job_name, + &mut job_parent, + ); + } } } result @@ -42,7 +49,7 @@ impl DotGenerator { job_name: &mut String, job_parent: &mut String, ) -> String { - let mut result : String = "".to_string(); + let mut result: String = "".to_string(); if field.0.as_str() == Some("name") { *job_name = field.1.as_str().unwrap().to_owned(); } else if field.0.as_str() == Some("parent") { diff --git a/src/main.rs b/src/main.rs index 2a02c4f..8315886 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ mod test_unit; fn main() { let args = get_program_arguments(); let dot_generator = DotGenerator {}; - println!("{}",dot_generator.run(args)); + println!("{}", dot_generator.run(args)); } fn get_program_arguments() -> Vec { diff --git a/src/output_writer.rs b/src/output_writer.rs index 39d0333..1356310 100644 --- a/src/output_writer.rs +++ b/src/output_writer.rs @@ -1,16 +1,19 @@ pub struct OutputWriter {} impl OutputWriter { - pub fn prologue() -> String{ + pub fn prologue() -> String { "digraph D {\n".to_string() } - pub fn epilogue() -> String{ + pub fn epilogue() -> String { "}\n".to_string() } pub fn write_output(job_name: &String, job_parent: &String, hex_color: &String) -> String { - let mut result = format!(" {} -> {}\n", job_name, job_parent); - result += &format!(" \"{}\" [style=filled, fillcolor=\"{}\"]\n", job_name, hex_color); + let mut result = format!(" \"{}\" -> \"{}\"\n", job_name, job_parent); + result += &format!( + " \"{}\" [style=filled, fillcolor=\"{}\"]\n", + job_name, hex_color + ); result } } diff --git a/src/test_unit.rs b/src/test_unit.rs index 3128382..f18755d 100644 --- a/src/test_unit.rs +++ b/src/test_unit.rs @@ -1,28 +1,36 @@ #[cfg(test)] mod tests { - use regex::Regex; use crate::dot_generator::DotGenerator; + use regex::Regex; #[test] fn test_when_typical() { let dot_generator = DotGenerator {}; - let args = ["zuulgraph","/home/mihai/git/zuulgraph/test/yaml/demo_file1.yaml","/home/mihai/git/zuulgraph/test/yaml/demo_file2.yaml"].iter().map(|&s| s.into()).collect(); - let expected = concat!("digraph D {\n", - " JobB_file1 -> A_file2\n", - " \"JobB_file1\" [style=filled, fillcolor=\"\"]\n", - " JobC_file1 -> JobB_file1\n", - " \"JobC_file1\" [style=filled, fillcolor=\"\"]\n", - " JobD_file1 -> JobB_file1\n", - " \"JobD_file1\" [style=filled, fillcolor=\"\"]\n", - " A_file2 -> JobA_file1\n", - " \"A_file2\" [style=filled, fillcolor=\"\"]\n", - "}\n"); + let args = [ + "zuulgraph", + "/home/mihai/git/zuulgraph/test/yaml/demo_file1.yaml", + "/home/mihai/git/zuulgraph/test/yaml/demo_file2.yaml", + ] + .iter() + .map(|&s| s.into()) + .collect(); + let expected = concat!( + "digraph D {\n", + " JobB_file1 -> A_file2\n", + " \"JobB_file1\" [style=filled, fillcolor=\"\"]\n", + " JobC_file1 -> JobB_file1\n", + " \"JobC_file1\" [style=filled, fillcolor=\"\"]\n", + " JobD_file1 -> JobB_file1\n", + " \"JobD_file1\" [style=filled, fillcolor=\"\"]\n", + " A_file2 -> JobA_file1\n", + " \"A_file2\" [style=filled, fillcolor=\"\"]\n", + "}\n" + ); let result = dot_generator.run(args); let re = Regex::new(r"#[A-Z0-9]+").unwrap(); let postprocessed_result = re.replace_all(result.as_str(), ""); - - assert_eq!(postprocessed_result, expected); + assert_eq!(postprocessed_result, expected); } -} \ No newline at end of file +} diff --git a/test/yaml/demo_file1.yaml b/test/yaml/demo_file1.yaml index bc7249b..6462de9 100644 --- a/test/yaml/demo_file1.yaml +++ b/test/yaml/demo_file1.yaml @@ -1,14 +1,14 @@ - job: - name: JobA_file1 + name: JobA_file1 - job: - name: JobB_file1 - parent: A_file2 + name: JobB_file1 + parent: A_file2 - job: - name: JobC_file1 - parent: JobB_file1 + name: JobC_file1 + parent: JobB_file1 - job: - name: JobD_file1 - parent: JobB_file1 + name: JobD_file1 + parent: JobB_file1 diff --git a/test/yaml/demo_file2.yaml b/test/yaml/demo_file2.yaml index 1237a24..76af421 100644 --- a/test/yaml/demo_file2.yaml +++ b/test/yaml/demo_file2.yaml @@ -1,3 +1,3 @@ - job: - name: A_file2 - parent: JobA_file1 + name: A_file2 + parent: JobA_file1