This commit is contained in:
Ondrej Vlach 2023-12-02 00:59:31 +01:00
parent b72de5b254
commit a5e0857285
Signed by: ovlach
GPG Key ID: 4FF1A23B4914DE70
7 changed files with 87 additions and 148 deletions

View File

@ -0,0 +1,53 @@
name: test
run-name: test
on:
workflow_call:
push:
branches:
- '*'
jobs:
clippy:
runs-on: ubuntu-latest
steps:
- uses: https://github.com/actions/checkout.git@v4
- uses: https://gitea.com/wolfogre/cache/restore@v3
id: cache
with:
path: |
.cache
target
key: ${{github.repository}}-cache-cargo-clippy-v4-${{ hashFiles('Cargo.lock') }}
restore-keys: ${{github.repository}}-cache-cargo-clippy-v4
- uses: nanobyte-public/rust-action@master
with:
args: CARGO_HOME=./.cache cargo clippy -- -Dwarnings
- uses: https://gitea.com/wolfogre/cache/save@v3
if: steps.cache.outputs.cache-hit != 'true'
with:
path: |
.cache
target
key: ${{github.repository}}-cache-cargo-clippy-v4-${{ hashFiles('Cargo.lock') }}
test:
runs-on: ubuntu-latest
steps:
- uses: https://github.com/actions/checkout.git@v4
- uses: https://gitea.com/wolfogre/cache/restore@v3
id: cache
with:
path: |
.cache
target
key: ${{github.repository}}-cache-cargo-test-v4-${{ hashFiles('Cargo.lock') }}
restore-keys: ${{github.repository}}-cache-cargo-test-v4
- uses: nanobyte-public/rust-action@master
with:
args: |
CARGO_HOME=./.cache cargo test
- uses: https://gitea.com/wolfogre/cache/save@v3
if: steps.cache.outputs.cache-hit != 'true'
with:
path: |
.cache
target
key: ${{github.repository}}-cache-cargo-test-v4-${{ hashFiles('Cargo.lock') }}

26
Cargo.lock generated
View File

@ -1128,6 +1128,21 @@ dependencies = [
"version_check",
]
[[package]]
name = "nanobyte_tera"
version = "0.1.0"
source = "git+https://glpat-Us_EdFTzQLv4shViQXi_:glpat-Us_EdFTzQLv4shViQXi_@gitlab.nanobyte.cz/tools/nanobyte_tera.git?branch=master#75c20a9806663ca04c6f8a7afff64d7b5906d113"
dependencies = [
"chrono",
"fluent-bundle",
"fluent-resmgr",
"log",
"ovlach_data",
"rocket_dyn_templates",
"sha256",
"unic-langid",
]
[[package]]
name = "native-tls"
version = "0.2.11"
@ -1287,7 +1302,9 @@ dependencies = [
"fluent-bundle",
"fluent-resmgr",
"log",
"nanobyte_tera",
"ovlach_data",
"ovlach_tera",
"phf",
"reqwest",
"rocket",
@ -1299,6 +1316,15 @@ dependencies = [
"unic-langid",
]
[[package]]
name = "ovlach_tera"
version = "0.1.0"
source = "git+https://glpat-_yPuXbEzECyk3FaHudCN:glpat-_yPuXbEzECyk3FaHudCN@gitlab.nanobyte.cz/ondrej/ovlach_tera.git?branch=master#0ab1bbadd76c1336e00b2a38e572048003879ab6"
dependencies = [
"rocket_dyn_templates",
"serde",
]
[[package]]
name = "parking_lot"
version = "0.12.1"

View File

@ -20,4 +20,6 @@ sha256 = "1.4.0"
fluent-bundle = "0.15.2"
fluent-resmgr = "0.0.6"
unic-langid = "0.9.1"
phf = { version = "0.11.2", features = ["macros"] }
phf = { version = "0.11.2", features = ["macros"] }
nanobyte_tera = { git = "https://glpat-Us_EdFTzQLv4shViQXi_:glpat-Us_EdFTzQLv4shViQXi_@gitlab.nanobyte.cz/tools/nanobyte_tera.git", branch = "master" }
ovlach_tera = { git = "https://glpat-_yPuXbEzECyk3FaHudCN:glpat-_yPuXbEzECyk3FaHudCN@gitlab.nanobyte.cz/ondrej/ovlach_tera.git", branch = "master" }

View File

@ -1,8 +1,9 @@
use nanobyte_tera::{hosts::static_filter, l18n::translate_filter, date::{calculate_age, get_year}, string::insert_space_every, gravatar::gravatar_link};
use ovlach_tera::entity::lang_entity;
use rocket::{*, fairing::AdHoc};
use rocket_dyn_templates::Template;
use ::serde::Deserialize;
use tools::tera::{static_filter, translate_filter, calculate_age, insert_space_every, lang_entity, gravatar_link, format_date};
pub mod routes;
pub mod services;
@ -36,7 +37,8 @@ pub fn rocket_builder() -> Rocket<Build> {
engines.tera.register_filter("gravatar_link", gravatar_link);
// filters specific to API
engines.tera.register_filter("lang_entity", lang_entity);
engines.tera.register_filter("format_date", format_date);
engines.tera.register_filter("format_date", get_year); // deprecated
engines.tera.register_filter("get_year", get_year);
Ok(())
})
).attach(

View File

@ -1,3 +1,2 @@
pub mod tera;
pub mod rocket;
pub mod lang;

View File

@ -1,143 +0,0 @@
use std::{collections::HashMap, str::FromStr};
use chrono::{Utc, Datelike, NaiveDate};
use log::{error, debug};
use rocket_dyn_templates::tera::{Value, Error};
use ovlach_data::cv::chrono::from_string;
use sha256::digest;
use fluent_resmgr::resource_manager::ResourceManager;
use unic_langid::LanguageIdentifier;
use crate::tools::lang::get_lang_name_from_code;
// 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> {
// TODO: prepis me!
let mgr = ResourceManager::new("./resources/{locale}/{res_id}".into());
let lang = args["lang"].as_str().unwrap();
let locales = vec![LanguageIdentifier::from_str(lang).unwrap()];
let val = value.as_str().unwrap().to_string();
let bundle = mgr.get_bundle(locales, vec!["ovlach_frontend".to_string()]);
let mut errors = vec![];
match bundle.get_message(&val) {
Some(msg) => {
match msg.value() {
Some(pattern) => {
let output = bundle.format_pattern(pattern, None, &mut errors);
return Ok(rocket_dyn_templates::tera::Value::String(output.clone().to_string()))
},
None => {
error!("Can't translate message: {}", val);
return Ok(rocket_dyn_templates::tera::Value::String(format!("{}", val)));
},
}
},
None => {
error!("Can't translate message: {}", val);
return Ok(rocket_dyn_templates::tera::Value::String(format!("{}", val)));
},
};
}
pub fn lang_entity(
value: &Value,
attrs: &HashMap<String, rocket_dyn_templates::tera::Value>
) -> Result<Value, Error> {
// TODO: rewrite me!
let chosen_lang = attrs["lang"].as_str().unwrap();
let output_value = match value.as_object().unwrap().get(get_lang_name_from_code(chosen_lang)) {
Some(value) => value.as_str().unwrap(),
None => value["en"].as_str().unwrap(), // TODO: default
};
return Ok(rocket_dyn_templates::tera::Value::String(output_value.to_string())); // TODO: fix-me here!
}
pub fn gravatar_link(
value: &Value,
_: &HashMap<String, rocket_dyn_templates::tera::Value>
) -> Result<Value, Error> {
let val = digest(value.as_str().unwrap());
return Ok(rocket_dyn_templates::tera::Value::String(format!("https://gravatar.com/avatar/{}", val))); // TODO: fix-me here!
}
pub fn format_date(
value: &Value,
_: &HashMap<String, rocket_dyn_templates::tera::Value>
) -> Result<Value, Error> {
if value.is_null() {
return Ok(rocket_dyn_templates::tera::Value::String("".to_string()))
}
let val = value.as_str().unwrap();
if val == "" {
return Ok(rocket_dyn_templates::tera::Value::String("".to_string()))
}
let value = from_string(value.as_str().unwrap().into());
match value {
Ok(date) => Ok(rocket_dyn_templates::tera::Value::String(date.format("%Y").to_string())),
Err(e) => {
error!("Can't parse date: {}", e);
return Ok(rocket_dyn_templates::tera::Value::String("".to_string())) // TOdo: error
}
}
}
pub fn insert_space_every(
value: &Value,
args: &HashMap<String, rocket_dyn_templates::tera::Value>
) -> Result<Value, Error> {
let 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,
_: &HashMap<String, rocket_dyn_templates::tera::Value>
) -> Result<Value, Error> {
//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)
}
}

View File

@ -178,7 +178,7 @@
<div class="text-muted text-small mb-3">{{ education.from | format_date(type="job") }} - {{ education.to | format_date(type="job") }}</div>
<div>{{ education.description | lang_entity(lang=lang) }}</div>
{% else %}
2011 - 2013
{{ education.from | format_date(type="job") }}
{% endif %}
</div>
</div>