Improved analyze_file
This commit is contained in:
parent
c969dd5b9c
commit
2c94e1e052
54
src/lib.rs
54
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<String, String> 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, String>) -> String {
|
||||
pub fn analyze_file(filepath: &str, vars: HashMap<String, String>) -> Result<String, String> {
|
||||
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<String> = 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<String> = 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("%%", "");
|
||||
|
||||
|
@ -9,7 +9,7 @@ use std::{thread, time};
|
||||
|
||||
fn update_index(vars: &HashMap<String, String>) {
|
||||
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");
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user