From 2bae1475b6e182224b53bd5bedd01dba181e2d04 Mon Sep 17 00:00:00 2001 From: Valerie Date: Wed, 28 Aug 2024 10:40:03 -0400 Subject: [PATCH] gave context items module structure, moved context build to a new method, and implemented empty and set flags --- src/{ => context}/global.rs | 0 src/{ => context}/helper.rs | 0 src/context/mod.rs | 4 +++ src/flag.rs | 1 + src/main.rs | 38 ++--------------------- src/util.rs | 61 +++++++++++++++++++++++++++++++++++-- 6 files changed, 66 insertions(+), 38 deletions(-) rename src/{ => context}/global.rs (100%) rename src/{ => context}/helper.rs (100%) create mode 100644 src/context/mod.rs diff --git a/src/global.rs b/src/context/global.rs similarity index 100% rename from src/global.rs rename to src/context/global.rs diff --git a/src/helper.rs b/src/context/helper.rs similarity index 100% rename from src/helper.rs rename to src/context/helper.rs diff --git a/src/context/mod.rs b/src/context/mod.rs new file mode 100644 index 0000000..ee794e1 --- /dev/null +++ b/src/context/mod.rs @@ -0,0 +1,4 @@ + +pub mod global; +pub mod helper; + diff --git a/src/flag.rs b/src/flag.rs index b2e1be3..c3fa1d7 100644 --- a/src/flag.rs +++ b/src/flag.rs @@ -2,5 +2,6 @@ pub const EMPTY_CONTEXT: [&str;2] = [ "-E", "--empty"]; pub const HELP: [&str;2] = [ "-h", "--help" ]; pub const QUIET: [&str;2] = [ "-q", "--quiet" ]; +pub const SET: &str = "--set"; pub const VERSION: [&str;2] = [ "-V", "--version" ]; diff --git a/src/main.rs b/src/main.rs index 8c9da05..5a6f591 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,6 @@ use std::{ }; use evalexpr::{ - context_map, eval_with_context_mut, ContextWithMutableVariables, HashMapContext, @@ -21,9 +20,9 @@ use termion::{ style }; +mod context; +mod eval; mod flag; -mod global; -mod helper; mod util; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -41,38 +40,7 @@ fn main() { return; } - // build context and handle empty flag - let mut context = - if !args.contains(flag::EMPTY_CONTEXT) { - context_map! { - // globals - "c" => global::LIGHT_SPEED, - "e" => global::EULER, - "phi" => global::GOLDEN_RATIO, - "pi" => global::PI, - "√2" => global::ROOT_TWO, - - // math functions - "fix" => Function::new(|arg| helper::fix(arg)), - "log" => Function::new(|arg| helper::logarithm(arg)), - "sqrt" => Function::new(|arg| helper::square_root(arg)), - - // data science functions - "avg" => Function::new(|arg| helper::average(arg)), - - // radix functions - "bin" => Function::new(|arg| helper::binary(arg)), - "hex" => Function::new(|arg| helper::hexadecimal(arg)), - "oct" => Function::new(|arg| helper::octal(arg)), - - // character aliases - "ϕ" => global::GOLDEN_RATIO, - "π" => global::PI, - "√" => Function::new(|arg| helper::square_root(arg)) - }.unwrap() - } else { - HashMapContext::new() - }; + let mut context = util::build_context(&mut args); // collect args and evaluate if present let expressions = args.finish(); diff --git a/src/util.rs b/src/util.rs index 1cc557e..0eda23c 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,7 +1,12 @@ + use evalexpr::{ - Value, - - EvalexprError + context_map, ContextWithMutableVariables, EvalexprError, HashMapContext, Value +}; +use pico_args::Arguments; + +use crate::{ + context::{ global, helper }, + flag }; pub(crate) fn parse_radix(prefix: &str, base: u32, arg: &Value) -> Result { @@ -16,3 +21,53 @@ pub(crate) fn parse_radix(prefix: &str, base: u32, arg: &Value) -> Result HashMapContext { + let mut output = + if !args.contains(flag::EMPTY_CONTEXT) { + context_map! { + // globals + "c" => global::LIGHT_SPEED, + "e" => global::EULER, + "phi" => global::GOLDEN_RATIO, + "pi" => global::PI, + "√2" => global::ROOT_TWO, + + // math functions + "fix" => Function::new(|arg| helper::fix(arg)), + "log" => Function::new(|arg| helper::logarithm(arg)), + "sqrt" => Function::new(|arg| helper::square_root(arg)), + + // data science functions + "avg" => Function::new(|arg| helper::average(arg)), + + // radix functions + "bin" => Function::new(|arg| helper::binary(arg)), + "hex" => Function::new(|arg| helper::hexadecimal(arg)), + "oct" => Function::new(|arg| helper::octal(arg)), + + // character aliases + "ϕ" => global::GOLDEN_RATIO, + "π" => global::PI, + "√" => Function::new(|arg| helper::square_root(arg)) + }.unwrap() + } else { HashMapContext::new() }; + + while let Ok(value) = args.value_from_str::<&str, String>(flag::SET) { + let split: Vec<&str> = value.split('=').collect(); + if split.len() == 2 { + let key = split[0].to_owned(); + + let value_str = split[1]; + let value = + if let Ok(integer) = value_str.parse::() { Value::Int(integer) } + else if let Ok(float) = value_str.parse::() { Value::Float(float) } + else { Value::from(value_str) }; + + output.set_value(key, value).ok(); } + else { std::process::exit(1); } + } + + output +} +