From 2c94e1e0524ec3d77a3387ab840d81bd5c19cdf3 Mon Sep 17 00:00:00 2001 From: Justine Date: Thu, 12 Jan 2023 10:25:17 +0100 Subject: [PATCH] Improved analyze_file --- src/lib.rs | 54 +++++++++++++++++++++++++++-------------------------- src/main.rs | 2 +- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d32e68d..cbf466b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,13 @@ -///! A simple templating library -///! -///! Takes template files : any text file ending in .ft -///! and substitutes special words. Special words can be of 2 types -///! and are written between four %: -///! %%cmd: my_bash_command%% : this will run the command my_bash_command -///! and subtitute the output. -///! %%myvar%% : this will be replaced by a given String. -///! -///! The only public function is analyze_file. +//! A simple templating library +//! +//! Takes template files : any text file ending in .ft +//! and substitutes special words. Special words can be of 2 types +//! and are written between four %: +//! %%cmd: my_bash_command%% : this will run the command my_bash_command +//! and subtitute the output. +//! %%myvar%% : this will be replaced by a given String. +//! +//! The only public function is analyze_file. pub mod flair { use std::{ fs, @@ -70,26 +70,28 @@ pub mod flair { ///vars must be : HashMap where key is var name ///and value is the value of our var. /// - ///# Panics - /// ///The file must be a flair template file, ending in .ft ///The file must exist, as well. - pub fn analyze_file(filepath: &str, vars: HashMap) -> String { + pub fn analyze_file(filepath: &str, vars: HashMap) -> Result { if !filepath.ends_with(".ft") { - panic!("Only flair template files will be accepted. \n\ - Please rename your file to be a .ft file."); - } - let file = fs::File::open(filepath).expect(&format!("File not {} found", &filepath)); - let reader = BufReader::new(file).lines(); - let mut analyzed_lines: Vec = Vec::new(); - - for line in reader { - let mut myline = line.unwrap(); - myline = analyze_line(&mut myline, &vars); - _ = &mut analyzed_lines.push(myline); + return Err(String::from("Only flair template files will be accepted")); } - return analyzed_lines.join("\n"); + let file = fs::File::open(filepath); + match file { + Ok(f) => { + let reader = BufReader::new(f).lines(); + let mut analyzed_lines: Vec = Vec::new(); + + for line in reader { + let mut myline = line.unwrap(); + myline = analyze_line(&mut myline, &vars); + _ = &mut analyzed_lines.push(myline); + } + return Ok(analyzed_lines.join("\n")); + }, + Err(e) => return Err(e.to_string()), + }; } @@ -126,7 +128,7 @@ pub mod flair { } - ///Used to replace individuals placeholders ("words"). + ///Used to replace individual placeholders ("words"). fn analyze_word(word: &str) -> Placeholder { let mut word = String::from(word).replace("%%", ""); diff --git a/src/main.rs b/src/main.rs index 23681ef..6da6a14 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use std::{thread, time}; fn update_index(vars: &HashMap) { let newvars = vars.clone(); - let analyzed = analyze_file("./templates/index.html.ft", newvars); + let analyzed = analyze_file("./templates/index.html.ft", newvars).unwrap(); fs::write("./static_html/index.html", analyzed).expect("Could not write index_file"); }