Compare commits

..

2 Commits

Author SHA1 Message Date
leca 4ec6e32357 some renaming 2024-04-14 09:16:01 +03:00
leca e84484111c some renaming 2024-04-14 09:15:26 +03:00
3 changed files with 31 additions and 28 deletions

View File

@ -1,21 +1,19 @@
use std::fs;
use std::fs::OpenOptions;
use std::io::prelude::*;
#[derive(PartialEq)]
struct Entry {
pub struct DictionaryEntry {
hieroglyph: String,
reading: String,
translation: String,
}
impl std::clone::Clone for Entry {
impl std::clone::Clone for DictionaryEntry {
fn clone(&self) -> Self {
Self { hieroglyph: self.hieroglyph.clone(), reading: self.reading.clone(), translation: self.translation.clone() }
}
}
impl std::fmt::Display for Entry {
impl std::fmt::Display for DictionaryEntry {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
@ -35,8 +33,8 @@ fn get_sym_pos_or_panic(path: &String, line: &str, sym: &str, line_number: usize
}
}
fn read_and_parse(path: &String) -> Vec<Entry> {
let mut result: Vec<Entry> = Vec::new();
fn read_and_parse(path: &String) -> Vec<DictionaryEntry> {
let mut result: Vec<DictionaryEntry> = Vec::new();
let content = fs::read_to_string(path).unwrap();
let content: Vec<&str> = content.split('\n').collect();
@ -61,7 +59,7 @@ fn read_and_parse(path: &String) -> Vec<Entry> {
};
let translation = line[translation_start..translation_end].to_string();
let entry = Entry {
let entry = DictionaryEntry {
hieroglyph,
reading,
translation,
@ -72,9 +70,9 @@ fn read_and_parse(path: &String) -> Vec<Entry> {
result
}
fn deduplicate (entries: &Vec<Entry>) -> Vec<Entry>{
fn deduplicate (entries: &Vec<DictionaryEntry>) -> Vec<DictionaryEntry>{
let mut deduplicated:Vec<Entry> = Vec::new();
let mut deduplicated:Vec<DictionaryEntry> = Vec::new();
for entry in entries.iter() {
let mut is_dup = false;
@ -97,21 +95,26 @@ fn deduplicate (entries: &Vec<Entry>) -> Vec<Entry>{
deduplicated
}
pub fn import(path: &String) {
let mut output = OpenOptions::new()
.write(true)
.append(true)
.open("./reparsed-test.txt")
.unwrap();
pub fn import(path: &String) -> Vec<DictionaryEntry>{
let dictionary = read_and_parse(path);
let dictionary = deduplicate(&dictionary);
for entry in dictionary {
if let Err(e) = writeln!(
output,
"({}) [{}] {{{}}}",
entry.hieroglyph, entry.reading, entry.translation
) {
eprintln!("Couldn't write to file: {}", e);
}
}
dictionary
}
// pub fn write_to_file(path: &String, dict: &Vec<Entry>) {
// let mut output = OpenOptions::new()
// .write(true)
// .append(true)
// .open("./reparsed-test.txt")
// .unwrap();
// for entry in dict {
// if let Err(e) = writeln!(
// output,
// "({}) [{}] {{{}}}",
// entry.hieroglyph, entry.reading, entry.translation
// ) {
// eprintln!("Couldn't write to file: {}", e);
// }
// }
// }

View File

@ -10,7 +10,7 @@ mod db;
mod game;
mod ui;
mod widgets;
mod import;
mod dictionary;
use crate::ui::menu::MenuScene;
@ -39,9 +39,9 @@ const APP_ID: &str = "org.foxarmy.learn-hieroglyph";
fn main() -> glib::ExitCode {
gio::resources_register_include!("compiled.gresource").expect("Cannot include gresources");
db::init();
db::init(); // Create database if not exists, create program's home if not exists, etc...
import::import(&String::from("./reparsed.txt"));
dictionary::import(&String::from("./reparsed.txt")); //
let app: Application = Application::builder().application_id(APP_ID).build();