feat: word lang included to allow better filtering

This commit is contained in:
2026-07-26 12:07:52 +02:00
parent 29ea04aed6
commit 9ccc447c64
2 changed files with 80 additions and 18 deletions

View File

@@ -1,7 +1,11 @@
use rusqlite::{Connection, params};
#[derive(Clone, Debug)]
pub struct Word(pub String, pub String);
pub struct Word {
pub key: String,
pub description: String,
pub lang: String,
}
pub struct FileDatabase {
connection: Connection,
@@ -33,7 +37,8 @@ impl FileDatabase {
.execute(
"CREATE TABLE IF NOT EXISTS word (
key TEXT PRIMARY KEY,
description TEXT NOT NULL
description TEXT NOT NULL,
lang VARCHAR(2) NOT NULL
)",
(), // empty list of parameters.
)
@@ -48,8 +53,10 @@ pub trait WordSource {
fn edit_word(&self, word: Word) -> Result<Word, ()>;
fn get_all_words(&self) -> Vec<Word>;
fn normalize_word_key(word_key: &str) -> String;
fn normalize_lang_key(word_key: &str) -> String;
}
// TODO: improve fuzzy
fn fuzzy_compare(a: &str, b: &str) -> bool {
if a.eq(b) || a.contains(b) || b.contains(a) {
return true;
@@ -59,7 +66,9 @@ fn fuzzy_compare(a: &str, b: &str) -> bool {
impl WordSource for FileDatabase {
fn find_by_word(&self, word: &str) -> Result<Vec<Word>, ()> {
let statement = self.connection.prepare("SELECT key, description FROM word");
let statement = self
.connection
.prepare("SELECT key, description, lang FROM word");
let mut word_list: Vec<Word> = vec![];
if let Ok(mut query) = statement {
@@ -68,7 +77,12 @@ impl WordSource for FileDatabase {
let found_word: String = row.get(0).unwrap();
if fuzzy_compare(word, &found_word) {
let found_word_description: String = row.get(1).unwrap();
word_list.push(Word(found_word, found_word_description));
let found_word_lang: String = row.get(2).unwrap();
word_list.push(Word {
key: found_word,
description: found_word_description,
lang: found_word_lang,
});
}
}
} else {
@@ -79,8 +93,12 @@ impl WordSource for FileDatabase {
fn insert_word(&self, word: Word) -> Result<(), ()> {
let result = self.connection.execute(
"INSERT INTO word (key, description) VALUES (?1, ?2)",
(&Self::normalize_word_key(&word.0), &word.1),
"INSERT INTO word (key, description, lang) VALUES (?1, ?2, ?3)",
(
&Self::normalize_word_key(&word.key),
&word.description,
&Self::normalize_lang_key(&word.lang),
),
);
if let Ok(count) = result {
if count > 0 {
@@ -92,8 +110,12 @@ impl WordSource for FileDatabase {
fn edit_word(&self, word: Word) -> Result<Word, ()> {
let result = self.connection.execute(
"UPDATE word SET key = ?1, description = ?2 WHERE key = ?1",
(&Self::normalize_word_key(&word.0), &word.1),
"UPDATE word SET key = ?1, description = ?2, lang = ?3 WHERE key = ?1",
(
&Self::normalize_word_key(&word.key),
&word.description,
&Self::normalize_lang_key(&word.lang),
),
);
match result {
Ok(_) => {
@@ -121,7 +143,9 @@ impl WordSource for FileDatabase {
}
fn get_all_words(&self) -> Vec<Word> {
let statement = self.connection.prepare("select key, description from word");
let statement = self
.connection
.prepare("select key, description, lang from word");
let mut word_list: Vec<Word> = vec![];
// TODO: make this pretty with functional programming
@@ -130,7 +154,12 @@ impl WordSource for FileDatabase {
while let Some(row) = rows.next().unwrap() {
let found_word: String = row.get(0).unwrap();
let found_word_description: String = row.get(1).unwrap();
word_list.push(Word(found_word, found_word_description));
let found_word_lang: String = row.get(2).unwrap();
word_list.push(Word {
key: found_word,
description: found_word_description,
lang: found_word_lang,
});
}
}
@@ -142,4 +171,10 @@ impl WordSource for FileDatabase {
// final and real word -> Maybe full refactor to include a word id
word_key.to_lowercase().to_string()
}
fn normalize_lang_key(word_key: &str) -> String {
// TODO: real normalization, not just lowercase, but keep in mind it must be kept as the
// final and real word -> Maybe full refactor to include a word id
word_key.to_lowercase().to_string()
}
}