diff options
author | JJ | 2023-02-24 04:56:33 +0000 |
---|---|---|
committer | JJ | 2023-02-24 04:56:33 +0000 |
commit | 153e37eafb2b33c9863781b4b5d65210534949e1 (patch) | |
tree | 83b1edca6290df4d0c8d856a406c50e0f02498b9 /src | |
parent | 3f4889db88baf2f88feee691ae561aaddcc7ca2f (diff) |
refactor slightly for state
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 30 | ||||
-rw-r--r-- | src/main.rs | 7 |
2 files changed, 27 insertions, 10 deletions
@@ -13,14 +13,14 @@ const version: &str = env!("CARGO_PKG_VERSION"); const index_path: &str = env!("index_path"); const dictionary_path: &str = env!("dictionary_path"); -pub fn handle(word: String) { +pub fn handle(word: String, state: &State) { // if lets are kinda clunky if let Some(definition) = lookup(&word) { - display(definition); + display(definition, &state); } else if let Some(corrected) = correct(&word) { println!("Could not find word {}, continuing with {}...", word, corrected); if let Some(definition) = lookup(&corrected) { - display(definition); + display(definition, &state); } else { println!("Could not find corrected word {}.", corrected); } @@ -88,11 +88,11 @@ fn correct(word: &str) -> Option<&str> { // now we do somewhat inefficient string manipulation // but it's fine because we're working with MUCH smaller strings lol -fn display(definition: String) { +fn display(definition: String, state: &State) { let definition = Configuration::default().parse(&definition); // this is really quite terrible code - if !display_ii(&definition, |value| value == "English") { + if !display_ii(&definition, |value| value == &state.lang) { display_ii(&definition, |value| true); } } @@ -144,7 +144,23 @@ fn display_ii<F: Fn(&str) -> bool>(definition: &Output, f: F) -> bool { return correct_language; } -pub fn param(word: String) { +// default values on structs please ;_; +pub struct State { + pub full: bool, + pub lang: String, +} + +impl State { + pub fn new() -> State { + return State { + full: false, + lang: String::from("English"), + } + } +} + +// mut state: State, yet state: &mut State?? huh?? +pub fn param(word: String, state: &mut State) { match word.as_str() { // curious about this "--help" => { println!("dictionarium {}", version); @@ -152,7 +168,7 @@ pub fn param(word: String) { println!("Usage: dictionarium <word>"); }, "--full" => { // set some global variable - + state.full = true; }, _ => { println!("Unknown flag \"{}\".", word); diff --git a/src/main.rs b/src/main.rs index 62da4a5..a64aec8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,15 +2,16 @@ use std::env; use dictionarium; fn main() { + let mut state = dictionarium::State::new(); let args: Vec<String> = env::args().skip(1).collect(); if args.len() == 0 { - dictionarium::param(String::from("--help")); + dictionarium::param(String::from("--help"), &mut state); } else { let mut words = Vec::<String>::new(); for word in args { if word.len() > 2 && word.get(0..2).unwrap_or_default() == "--" { - dictionarium::param(word); + dictionarium::param(word, &mut state); } else { words.push(word); } @@ -18,7 +19,7 @@ fn main() { // we accept multiple words gladly for word in words { - dictionarium::handle(word); + dictionarium::handle(word, &state); } } } |