diff options
| -rw-r--r-- | crates/cli/src/inflection.rs | 133 | ||||
| -rw-r--r-- | lib/mihi/src/lib.rs | 105 |
2 files changed, 156 insertions, 82 deletions
diff --git a/crates/cli/src/inflection.rs b/crates/cli/src/inflection.rs index 05c4c70..c8b8574 100644 --- a/crates/cli/src/inflection.rs +++ b/crates/cli/src/inflection.rs @@ -1,4 +1,7 @@ -use mihi::{group_declension_inflections, Category, DeclensionInfo, DeclensionTable, Gender, Word}; +use mihi::{ + configuration, group_declension_inflections, Category, DeclensionInfo, DeclensionTable, Gender, + Word, +}; fn get_inflected_from(word: &Word, row: &[DeclensionInfo; 2]) -> String { if word.is_flag_set("onlysingular") { @@ -27,20 +30,27 @@ fn print_noun_inflection(word: &Word) -> Result<(), String> { println!("\n== Inflection ==\n"); - println!( - "Nominative:\t{}", - get_inflected_from(word, &table.nominative) - ); - println!("Vocative:\t{}", get_inflected_from(word, &table.vocative)); - println!( - "Accusative:\t{}", - get_inflected_from(word, &table.accusative) - ); - println!("Genitive:\t{}", get_inflected_from(word, &table.genitive)); - println!("Dative:\t\t{}", get_inflected_from(word, &table.dative)); - println!("Ablative:\t{}", get_inflected_from(word, &table.ablative)); - if word.locative { - println!("Locative:\t{}", get_inflected_from(word, &table.locative)); + for idx in configuration().case_order.to_usizes() { + match idx { + 0 => println!( + "Nominative:\t{}", + get_inflected_from(word, &table.nominative) + ), + 1 => println!("Vocative:\t{}", get_inflected_from(word, &table.vocative)), + 2 => println!( + "Accusative:\t{}", + get_inflected_from(word, &table.accusative) + ), + 3 => println!("Genitive:\t{}", get_inflected_from(word, &table.genitive)), + 4 => println!("Dative:\t\t{}", get_inflected_from(word, &table.dative)), + 5 => println!("Ablative:\t{}", get_inflected_from(word, &table.ablative)), + 6 => { + if word.locative { + println!("Locative:\t{}", get_inflected_from(word, &table.locative)); + } + } + _ => {} + } } Ok(()) @@ -76,49 +86,56 @@ fn print_adjective_inflection(word: &Word) -> Result<(), String> { println!("\n== Inflection ==\n"); - println!( - "Nominative:\t{} | {} | {}", - get_inflected_from(word, &tables[0].nominative), - get_inflected_from(word, &tables[1].nominative), - get_inflected_from(word, &tables[2].nominative) - ); - println!( - "Vocative:\t{} | {} | {}", - get_inflected_from(word, &tables[0].vocative), - get_inflected_from(word, &tables[1].vocative), - get_inflected_from(word, &tables[2].vocative) - ); - println!( - "Accusative:\t{} | {} | {}", - get_inflected_from(word, &tables[0].accusative), - get_inflected_from(word, &tables[1].accusative), - get_inflected_from(word, &tables[2].accusative) - ); - println!( - "Genitive:\t{} | {} | {}", - get_inflected_from(word, &tables[0].genitive), - get_inflected_from(word, &tables[1].genitive), - get_inflected_from(word, &tables[2].genitive) - ); - println!( - "Dative:\t\t{} | {} | {}", - get_inflected_from(word, &tables[0].dative), - get_inflected_from(word, &tables[1].dative), - get_inflected_from(word, &tables[2].dative) - ); - println!( - "Ablative:\t{} | {} | {}", - get_inflected_from(word, &tables[0].ablative), - get_inflected_from(word, &tables[1].ablative), - get_inflected_from(word, &tables[2].ablative) - ); - if word.locative { - println!( - "Locative:\t{} | {} | {}", - get_inflected_from(word, &tables[0].locative), - get_inflected_from(word, &tables[1].locative), - get_inflected_from(word, &tables[2].locative) - ); + for idx in configuration().case_order.to_usizes() { + match idx { + 0 => println!( + "Nominative:\t{} | {} | {}", + get_inflected_from(word, &tables[0].nominative), + get_inflected_from(word, &tables[1].nominative), + get_inflected_from(word, &tables[2].nominative) + ), + 1 => println!( + "Vocative:\t{} | {} | {}", + get_inflected_from(word, &tables[0].vocative), + get_inflected_from(word, &tables[1].vocative), + get_inflected_from(word, &tables[2].vocative) + ), + 2 => println!( + "Accusative:\t{} | {} | {}", + get_inflected_from(word, &tables[0].accusative), + get_inflected_from(word, &tables[1].accusative), + get_inflected_from(word, &tables[2].accusative) + ), + 3 => println!( + "Genitive:\t{} | {} | {}", + get_inflected_from(word, &tables[0].genitive), + get_inflected_from(word, &tables[1].genitive), + get_inflected_from(word, &tables[2].genitive) + ), + 4 => println!( + "Dative:\t\t{} | {} | {}", + get_inflected_from(word, &tables[0].dative), + get_inflected_from(word, &tables[1].dative), + get_inflected_from(word, &tables[2].dative) + ), + 5 => println!( + "Ablative:\t{} | {} | {}", + get_inflected_from(word, &tables[0].ablative), + get_inflected_from(word, &tables[1].ablative), + get_inflected_from(word, &tables[2].ablative) + ), + 6 => { + if word.locative { + println!( + "Locative:\t{} | {} | {}", + get_inflected_from(word, &tables[0].locative), + get_inflected_from(word, &tables[1].locative), + get_inflected_from(word, &tables[2].locative) + ); + } + } + _ => {} + } } Ok(()) diff --git a/lib/mihi/src/lib.rs b/lib/mihi/src/lib.rs index fc73189..2915368 100644 --- a/lib/mihi/src/lib.rs +++ b/lib/mihi/src/lib.rs @@ -2,12 +2,93 @@ use serde_json::Value; use std::fs; use std::fs::File; use std::io::prelude::*; +use std::io::{self, BufRead, BufReader, Error}; use std::path::{Path, PathBuf}; use rusqlite::{params, Connection}; mod migrate; +/// Returns the configuration path for the application, and it even creates it +/// if it doesn't exist already. +pub fn get_config_path() -> Result<PathBuf, String> { + let dir = match &std::env::var("XDG_CONFIG_HOME") { + Ok(path) => PathBuf::from(path), + Err(_) => match &std::env::var("HOME") { + Ok(path) => Path::new(path).join(".config"), + Err(_) => { + return Err(String::from( + "cannot find a suitable path for the configuration", + )) + } + }, + } + .join("mihi"); + + match fs::create_dir_all(&dir) { + Ok(_) => {} + Err(e) => return Err(e.to_string()), + }; + + Ok(dir) +} + +#[derive(Default, Debug)] +pub enum CaseOrder { + #[default] + European, + English, +} + +impl CaseOrder { + /// Returns the current case order as a bunch of integers which represent + /// each case on a given order. + pub fn to_usizes(&self) -> [usize; 7] { + match self { + CaseOrder::European => [0, 1, 2, 3, 4, 5, 6], + CaseOrder::English => [0, 3, 4, 2, 5, 1, 6], + } + } +} + +#[derive(Debug)] +pub struct Configuration { + pub language: Language, + pub case_order: CaseOrder, +} + +/// Reads the global configuration and returns a proper object for it. It will +/// assume some defaults if there is something that goes wrong when reading it. +pub fn configuration() -> Configuration { + let order = read_line_from(1).unwrap_or(String::from("european")); + let case_order = match order.as_str() { + "english" => CaseOrder::English, + _ => CaseOrder::European, + }; + + Configuration { + language: Language::Latin, + case_order, + } +} + +// Read a specific line from the configuration and return a String. +fn read_line_from(line: usize) -> Result<String, Error> { + let path = get_config_path().map_err(std::io::Error::other)?; + let cfg = path.join("languages.txt"); + + let file = File::open(cfg)?; + let reader = BufReader::new(file); + + let line = reader + .lines() + .nth(line) + .transpose()? + .ok_or_else(|| io::Error::other("line not found"))?; + + Ok(line) +} + #[derive(Clone, Copy, Debug, Default)] pub enum Category { #[default] @@ -138,30 +219,6 @@ impl std::fmt::Display for Language { } } -/// Returns the configuration path for the application, and it even creates it -/// if it doesn't exist already. -pub fn get_config_path() -> Result<PathBuf, String> { - let dir = match &std::env::var("XDG_CONFIG_HOME") { - Ok(path) => PathBuf::from(path), - Err(_) => match &std::env::var("HOME") { - Ok(path) => Path::new(path).join(".config"), - Err(_) => { - return Err(String::from( - "cannot find a suitable path for the configuration", - )) - } - }, - } - .join("mihi"); - - match fs::create_dir_all(&dir) { - Ok(_) => {} - Err(e) => return Err(e.to_string()), - }; - - Ok(dir) -} - /// Add the given language into the configuration of this application. pub fn add_language(language: String) -> Result<(), String> { if language.as_str() != "latin" { |
