From c55cce3055d69113ce32032192ccb966f40d8304 Mon Sep 17 00:00:00 2001 From: "Dawid J. Kubis" Date: Thu, 7 Oct 2021 00:03:29 +0200 Subject: [PATCH] all thats left is to create the files and we have ourselves a working prototype --- src/main.rs | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 686857a..48f1ce9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,8 +24,23 @@ struct Opt { template: PathBuf, } -fn replace(input: String, rules: &[(&str, &str)]) -> String { - unimplemented!(); +/// replaces {{ value }} with actual value +fn replace(mut template: String, rules: &[(&str, &str)]) -> String { + for (x, y) in rules { + let l = format!("{{{{ {} }}}}", x); + template = template.replace(&l, y) + } + template +} + +/// collects table into a vector of String pairs +fn collect_table(table: &Value) -> Vec<(String, String)> { + table.as_table() + .unwrap() + .keys() + .map( + |x| (x.clone(), String::from(table[x].as_str().unwrap())) + ).collect() } fn main() { @@ -34,12 +49,12 @@ fn main() { //println!("{:?}", opt); // parse toml - let value = read_to_string(&opt.toml).unwrap().parse::().unwrap(); - println!("{:#?}", value); + let mut value = read_to_string(&opt.toml).unwrap().parse::().unwrap(); + //println!("{:#?}", value); // create output directory // if it doesn't exist - if (!opt.output.exists()) { + if !opt.output.exists() { create_dir(&opt.output).unwrap(); } @@ -50,8 +65,22 @@ fn main() { .collect::(), ); + // create rules - let page = value.get("page").unwrap().as_array().unwrap(); + // hope to god the toml is correctly formatted - println!("{:#?}", page); + // start with removing page rules + let page = value.as_table_mut().unwrap().remove("page").unwrap().clone(); + + // collect global rules into a vec + let global = collect_table(&value); + + // iterate over pages + for i in page.as_array().unwrap() { + println!("{:?}", i); + + // TODO create the files + } + + //println!("{:#?}", global); }