68 lines
2.2 KiB
Rust
68 lines
2.2 KiB
Rust
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,
|
|
args: &HashMap<String, rocket_dyn_templates::tera::Value>
|
|
) -> Result<Value, Error> {
|
|
let host = args.get("static_host");
|
|
return Ok(rocket_dyn_templates::tera::Value::String(format!("{}/{}", host.unwrap().as_str().unwrap(), value.as_str().unwrap()))); // TODO: fix-me here!
|
|
}
|
|
|
|
pub fn translate_filter(
|
|
value: &Value,
|
|
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)
|
|
}
|
|
}
|