Rust implementation of gen-indexes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

183 lines
7.2 KiB

// made for 42l stats
mod consts;
use std::{collections::HashMap, process::Command, str, io::Write, str::FromStr};
use sailfish::TemplateOnce;
use chrono::Local;
use humanesort::prelude::*;
fn print_type_of<T>(_: &T) {
println!("{}", std::any::type_name::<T>())
}
#[derive(TemplateOnce)]
#[template(path = "index.stpl")]
struct IndexTemplate<'a> {
years: &'a Vec<String>,
months: &'a Vec<String>,
weeks: &'a Vec<String>,
sel_year: String,
}
#[derive(TemplateOnce)]
#[template(path = "index-service.stpl")]
struct IndexSrvTemplate<'a> {
services: &'a Vec<String>,
sel_year: String,
sel_period: String,
folder_num: u8
}
fn main() {
let our_services: HashMap<&str, &str> = consts::create_service_map();
let current_year = Local::now().format("%Y").to_string();
let years_list = list_files(format!("{}{}",consts::BASE_INDEX, consts::MONTHLY_REPORTS).as_str());
if !is_cur_year_log(current_year.as_str(), &years_list) {
println!("Current year not logged yet. Happy New Year!");
println!("Interrupting the program to avoid unpredictable behavior");
std::process::exit(1);
}
let mut months_list = Vec::new();
let mut weeks_list = Vec::new();
for i in 0..years_list.len() {
let selected_year = &years_list[i];
months_list = list_files(format!("{}{}{}",consts::BASE_INDEX, consts::MONTHLY_REPORTS, selected_year).as_str());
weeks_list = list_files(format!("{}{}{}",consts::BASE_INDEX, consts::WEEKLY_REPORTS, selected_year).as_str());
months_list.humane_sort();
weeks_list.humane_sort();
if !std::path::Path::new(format!("{}{}{}{}", consts::BASE_INDEX, consts::MONTHLY_REPORTS, selected_year, "/index.html").as_str()).exists() {
let ctx = IndexTemplate {
years: &years_list,
months: &months_list,
weeks: &weeks_list,
sel_year: selected_year.to_string(),
};
let mut file = std::fs::File::create(format!("{}{}{}{}", consts::BASE_INDEX, consts::MONTHLY_REPORTS, selected_year, "/index.html").as_str()).unwrap();
let write = writeln!(&mut file, "{}", ctx.render_once().expect("Failed to render_once monthly index"));
let write = match write {
Ok(_) => (),
Err(error) => println!("Whoops: {}", error),
};
}
if !std::path::Path::new(format!("{}{}{}{}", consts::BASE_INDEX, consts::WEEKLY_REPORTS, selected_year, "/index.html").as_str()).exists() {
let ctx = IndexTemplate {
years: &years_list,
months: &months_list,
weeks: &weeks_list,
sel_year: selected_year.to_string(),
};
let mut file = std::fs::File::create(format!("{}{}{}{}", consts::BASE_INDEX, consts::WEEKLY_REPORTS, selected_year, "/index.html").as_str()).unwrap();
let write = writeln!(&mut file, "{}", ctx.render_once().unwrap());
let write = match write {
Ok(_) => (),
Err(error) => println!("Whoops: {}", error),
};
}
for j in 0..weeks_list.len() {
let week_num = u8::from_str(&weeks_list[j]).unwrap();
let path = format!("{}{}{}/{}/", consts::BASE_INDEX, consts::WEEKLY_REPORTS, years_list[i], week_num);
let list_services = list_files(format!("{}all/", &path).as_str());
if !std::path::Path::new(format!("{}{}", path, "index.html").as_str()).exists() {
let ctx = IndexSrvTemplate {
services: &list_services,
sel_year: selected_year.to_string(),
sel_period: "weekly".to_string(),
folder_num: week_num,
};
let mut file = std::fs::File::create(format!("{}{}", path, "index.html").as_str()).unwrap();
let write = writeln!(&mut file, "{}", ctx.render_once().unwrap());
let write = match write {
Ok(_) => (),
Err(error) => println!("Whoops: {}", error),
};
}
}
for j in 0..months_list.len() {
let month_num = u8::from_str(&months_list[j]).unwrap();
let path = format!("{}{}{}/{}/", consts::BASE_INDEX, consts::MONTHLY_REPORTS, years_list[i], month_num);
let list_services = list_files(format!("{}all/", &path).as_str());
if !std::path::Path::new(format!("{}{}", path, "index.html").as_str()).exists() {
let ctx = IndexSrvTemplate {
services: &list_services,
sel_year: selected_year.to_string(),
sel_period: "monthly".to_string(),
folder_num: month_num,
};
let mut file = std::fs::File::create(format!("{}{}", path, "index.html").as_str()).unwrap();
let write = writeln!(&mut file, "{}", ctx.render_once().unwrap());
let write = match write {
Ok(_) => (),
Err(error) => println!("Whoops: {}", error),
};
}
}
}
// Rendering main Index
if !std::path::Path::new(format!("{}{}", consts::BASE_INDEX, "/index.html").as_str()).exists() {
let ctx = IndexTemplate {
years: &years_list,
months: &months_list,
weeks: &weeks_list,
sel_year: current_year,
};
let mut file = std::fs::File::create(format!("{}{}", consts::BASE_INDEX, "/index.html").as_str()).unwrap();
let write = writeln!(&mut file, "{}", ctx.render_once().unwrap());
let write = match write {
Ok(_) => (),
Err(error) => println!("Whoops: {}", error),
};
}
}
// format month number (here value) is convert to his proper name
fn month_format(value: usize) -> &'static str {
consts::MONTH_NAMES[value - 1]
}
// Check if service exists using its file name without the extension
fn srv_format<'a>(file: &'a str, services: HashMap<&str, &'a str>) -> &'a str {
let value = file.split(".");
let split: Vec<&str> = value.collect();
let service_formated = split.first().unwrap();
println!("{:?}", service_formated);
for (service, name) in services {
if service_formated == &service {
return name;
}
}
println!("No service name specified for file {}", file);
return file;
}
// shell's command 'ls' implementation
fn list_files(folder: &str) -> Vec<String> {
let command = Command::new("ls")
.arg(folder)
.output()
.expect("Failed to execute the command :( ");
let output_string = str::from_utf8(command.stdout.as_slice())
.unwrap()
.split("\n");
let mut output_vec = Vec::new();
for string in output_string {
output_vec.push(string.to_string());
}
output_vec.pop();
if output_vec.last().unwrap() == "index.html" {
output_vec.pop();
}
output_vec
}
// Check if currrent year (cur_year) is already logged in the years_list
fn is_cur_year_log(cur_year: &str, years_list: &Vec<String>) -> bool {
for years in years_list {
if years == cur_year {
return true;
}
}
return false;
}