feat: add absolute dates/months to dictionary
This commit is contained in:
parent
c9433d721d
commit
d18f175aef
10 changed files with 9942 additions and 1834 deletions
|
|
@ -1,31 +1,11 @@
|
|||
#![cfg(test)]
|
||||
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
fmt::Debug,
|
||||
fs::File,
|
||||
io::{BufReader},
|
||||
};
|
||||
|
||||
use crate::{model::Language, timeago::TimeUnit};
|
||||
use crate::{timeago::TimeUnit};
|
||||
use fancy_regex::Regex;
|
||||
use once_cell::sync::Lazy;
|
||||
use serde::Deserialize;
|
||||
|
||||
const DICT_PATH: &str = "testfiles/date/dictionary.json";
|
||||
const TARGET_FILE: &str = "src/dictionary.rs";
|
||||
|
||||
type Dictionary = BTreeMap<Language, DictEntry>;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct DictEntry {
|
||||
#[serde(default)]
|
||||
equivalent: Vec<Language>,
|
||||
#[serde(default)]
|
||||
by_char: bool,
|
||||
timeago_tokens: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
fn parse_tu(tu: &str) -> (u8, Option<TimeUnit>) {
|
||||
static TU_PATTERN: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\d*)(\w?)$").unwrap());
|
||||
match TU_PATTERN.captures(tu).unwrap() {
|
||||
|
|
@ -47,51 +27,62 @@ fn parse_tu(tu: &str) -> (u8, Option<TimeUnit>) {
|
|||
}
|
||||
}
|
||||
|
||||
fn read_dict() -> Dictionary {
|
||||
let json_file = File::open(DICT_PATH).unwrap();
|
||||
serde_json::from_reader(BufReader::new(json_file)).unwrap()
|
||||
}
|
||||
|
||||
// #[test]
|
||||
fn generate_dictionary() {
|
||||
let dict = read_dict();
|
||||
let dict = super::read_dict();
|
||||
|
||||
let code_head = r#"// This file is automatically generated. DO NOT EDIT.
|
||||
use crate::{
|
||||
model::Language,
|
||||
timeago::{TaToken, TimeUnit},
|
||||
};
|
||||
|
||||
pub struct Entry {
|
||||
pub timeago_tokens: phf::Map<&'static str, TaToken>,
|
||||
pub date_order: &'static str,
|
||||
pub months: phf::Map<&'static str, u8>,
|
||||
}
|
||||
"#;
|
||||
|
||||
let mut code_timeago_tokens = r#"#[rustfmt::skip]
|
||||
pub(crate) fn get_timeago_tokens(lang: Language) -> phf::Map<&'static str, TaToken> {
|
||||
pub fn entry(lang: Language) -> Entry {
|
||||
match lang {
|
||||
"#
|
||||
.to_owned();
|
||||
|
||||
dict.iter().for_each(|(lang, entry)| {
|
||||
// Create a map for the language
|
||||
let mut map = phf_codegen::Map::<&str>::new();
|
||||
|
||||
entry.timeago_tokens.iter().for_each(|(txt, tu_str)| {
|
||||
let (n, unit) = parse_tu(&tu_str);
|
||||
match unit {
|
||||
Some(unit) => map.entry(
|
||||
&txt,
|
||||
&format!("TaToken {{ n: {}, unit: Some(TimeUnit::{:?}) }}", n, unit),
|
||||
),
|
||||
None => map.entry(&txt, &format!("TaToken {{ n: {}, unit: None }}", n)),
|
||||
};
|
||||
});
|
||||
|
||||
// Match selector
|
||||
let mut selector = format!("Language::{:?}", lang);
|
||||
entry.equivalent.iter().for_each(|eq| {
|
||||
selector += &format!(" | Language::{:?}", eq);
|
||||
});
|
||||
|
||||
let code_map = &map.build().to_string().replace('\n', "\n ");
|
||||
// Timeago tokens
|
||||
let mut ta_tokens = phf_codegen::Map::<&str>::new();
|
||||
entry.timeago_tokens.iter().for_each(|(txt, tu_str)| {
|
||||
let (n, unit) = parse_tu(&tu_str);
|
||||
match unit {
|
||||
Some(unit) => ta_tokens.entry(
|
||||
&txt,
|
||||
&format!("TaToken {{ n: {}, unit: Some(TimeUnit::{:?}) }}", n, unit),
|
||||
),
|
||||
None => ta_tokens.entry(&txt, &format!("TaToken {{ n: {}, unit: None }}", n)),
|
||||
};
|
||||
});
|
||||
|
||||
code_timeago_tokens += &format!("{} => {},\n ", selector, code_map);
|
||||
// Months
|
||||
let mut months = phf_codegen::Map::<&str>::new();
|
||||
entry.months.iter().for_each(|(txt, n_mon)| {
|
||||
months.entry(&txt, &n_mon.to_string());
|
||||
});
|
||||
|
||||
let code_ta_tokens = &ta_tokens.build().to_string().replace('\n', "\n ");
|
||||
let code_months = &months.build().to_string().replace('\n', "\n ");
|
||||
|
||||
code_timeago_tokens += &format!(
|
||||
"{} => Entry {{\n timeago_tokens: {},\n date_order: \"{}\",\n months: {},\n }},\n ",
|
||||
selector, code_ta_tokens, entry.date_order, code_months
|
||||
);
|
||||
});
|
||||
|
||||
code_timeago_tokens = code_timeago_tokens.trim_end().to_owned() + "\n }\n}\n";
|
||||
|
|
|
|||
Reference in a new issue