This commit is contained in:
2023-11-26 20:32:45 +01:00
parent 59ab0b7393
commit 933239a52d
7 changed files with 148 additions and 45 deletions

View File

@@ -1,7 +1,11 @@
use std::collections::HashMap;
use chrono::{Utc, Datelike};
use log::error;
use rocket_dyn_templates::tera::{Value, Error};
use ovlach_data::cv::chrono::from_string;
// TODO: tenhle modul je trochu prasacky..
pub fn static_filter(
value: &Value,
@@ -16,4 +20,48 @@ pub fn translate_filter(
args: &HashMap<String, rocket_dyn_templates::tera::Value>
) -> Result<Value, Error> {
return Ok(rocket_dyn_templates::tera::Value::String(format!("{}", value.as_str().unwrap()))); // TODO: fix-me here!
}
}
pub fn insert_space_every(
value: &Value,
args: &HashMap<String, rocket_dyn_templates::tera::Value>
) -> Result<Value, Error> {
let mut input = value.as_u64().unwrap().to_string();
let times = args.get("times").unwrap().as_u64().unwrap();
let mut result = String::new();
for (index, ch) in input.chars().enumerate() {
if index > 0 && index as u64 % times == 0 {
result.push(' ');
}
result.push(ch);
}
Ok(rocket_dyn_templates::tera::Value::String(result))
}
pub fn calculate_age(
value: &Value,
args: &HashMap<String, rocket_dyn_templates::tera::Value>
) -> Result<Value, Error> {
error!("{:?}", value.as_str());
//let s = value.to_string().trim_matches('"'); // TODO: unwrap here!
let value = from_string(value.as_str().unwrap().into());
match value {
Ok(value) => {
let current_date = Utc::now().naive_utc().date();
let mut age = current_date.year() - value.year();
// Adjust age if the birthday hasn't occurred yet this year
age = if current_date.month() < value.month() ||
(current_date.month() == value.month() && current_date.day() < value.day()) {
age - 1
} else {
age
};
Ok(rocket_dyn_templates::tera::Value::String(age.to_string()))
}
Err(e) => panic!("Can't parse date: {}", e)
}
}