From 053e938d3215693203dbcfaa223df327f8067220 Mon Sep 17 00:00:00 2001 From: Ivan Bushchik Date: Wed, 31 May 2023 10:27:23 +0300 Subject: [PATCH] Update to 0.4.0 - Context handling - evalexpr 10.0.0 Signed-off-by: Ivan Bushchik --- .gitignore | 1 + .rustfmt.toml | 10 ++++++ Cargo.lock | 16 ---------- Cargo.toml | 4 +-- src/main.rs | 87 ++++++++++++++++++++++++++++----------------------- src/tasks.rs | 4 +-- 6 files changed, 62 insertions(+), 60 deletions(-) create mode 100644 .rustfmt.toml delete mode 100644 Cargo.lock diff --git a/.gitignore b/.gitignore index ea8c4bf..869df07 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +Cargo.lock \ No newline at end of file diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..e8949b3 --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,10 @@ +edition = "2021" +hard_tabs = true +merge_derives = true +reorder_imports = true +reorder_modules = true +use_field_init_shorthand = true +use_small_heuristics = "Off" +wrap_comments = true +comment_width = 80 +indent_style = "Block" \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock deleted file mode 100644 index 4f80a2b..0000000 --- a/Cargo.lock +++ /dev/null @@ -1,16 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "evalexpr" -version = "8.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aacfb566035f8cd02f6ec9247c242f3f9904a0b288ea383abcf4e95df6436a34" - -[[package]] -name = "feval" -version = "0.3.0" -dependencies = [ - "evalexpr", -] diff --git a/Cargo.toml b/Cargo.toml index 87e2f5b..be1693f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "feval" -version = "0.3.0" +version = "0.4.0" edition = "2021" description = "clt for fast evaluations" readme = "README.md" @@ -9,4 +9,4 @@ license = "MIT" authors = ["Ivan ivabus Bushchik"] [dependencies] -evalexpr = "8.1.0" +evalexpr = "10.0.0" diff --git a/src/main.rs b/src/main.rs index 88ec33a..c1b7fac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,56 +1,63 @@ -use std::{env::args, io::{stdin, stdout, Write}}; +use std::{ + env::args, + io::{stdin, stdout, Write}, +}; use evalexpr; +use evalexpr::HashMapContext; use crate::tasks::tasks::all; mod tasks; fn help() { - println!(r#"feval: help + println!( + r#"feval: help usage: provide exactly one argument (with expression) or no arguments for shell mode. -example: feval "math::sin(30 * math::pi / 180)""#); +example: feval "math::sin(30 * math::pi / 180)""# + ); } fn main_loop() { - print!(">>> "); - stdout().flush().unwrap(); - let mut input = String::new(); - while stdin().read_line(&mut input).unwrap() != 0 { - let result = evalexpr::eval(&all(input.trim().to_string())); - match result { - Ok(succ_res) => println!("{}", succ_res), - Err(err) => println!("Error: {}", err) - } - input.clear(); - print!(">>> "); - stdout().flush().unwrap(); - } + print!(">>> "); + stdout().flush().unwrap(); + let mut input = String::new(); + let mut context = HashMapContext::new(); + while stdin().read_line(&mut input).unwrap() != 0 { + let result = evalexpr::eval_with_context_mut(&all(input.trim().to_string()), &mut context); + match result { + Ok(succ_res) => println!("{}", succ_res), + Err(err) => println!("Error: {}", err), + } + input.clear(); + print!(">>> "); + stdout().flush().unwrap(); + } } fn main() { - let args: Vec = args().collect(); - match args.len() { - 2 => { - let expr = all(args[1].clone()); - let result = evalexpr::eval(&expr.trim()); - match result { - Ok(succ_res) => println!("{}", succ_res), - Err(err) => { - println!("Error: {}", err); - std::process::exit(127); - } - } - }, - 1 => {main_loop()}, - _ => { - if args.len() > 2 { - println!("Too many args."); - help(); - } else if args.len() < 1 { - println!("Too few args."); - help(); - } - } - } + let args: Vec = args().collect(); + match args.len() { + 2 => { + let expr = all(args[1].clone()); + let result = evalexpr::eval(&expr.trim()); + match result { + Ok(succ_res) => println!("{}", succ_res), + Err(err) => { + println!("Error: {}", err); + std::process::exit(127); + } + } + } + 1 => main_loop(), + _ => { + if args.len() > 2 { + println!("Too many args."); + help(); + } else if args.len() < 1 { + println!("Too few args."); + help(); + } + } + } } diff --git a/src/tasks.rs b/src/tasks.rs index 3423daf..7847ffd 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -1,5 +1,5 @@ pub mod tasks { - use std::f64::consts; + use std::f64::consts; pub fn all(expr: String) -> String { convert_constants(exit(expr)) @@ -13,4 +13,4 @@ pub mod tasks { } expr } -} \ No newline at end of file +}