From 153e37eafb2b33c9863781b4b5d65210534949e1 Mon Sep 17 00:00:00 2001 From: JJ Date: Thu, 23 Feb 2023 20:56:33 -0800 Subject: refactor slightly for state --- src/lib.rs | 30 +++++++++++++++++++++++------- src/main.rs | 7 ++++--- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6b4be31..afcf8e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 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 "); }, "--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 = 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::::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); } } } -- cgit v1.2.3-70-g09d2