diff --git a/src-tauri/src/evaluation/mod.rs b/src-tauri/src/evaluation/mod.rs index 4c3a194..33ad9ef 100644 --- a/src-tauri/src/evaluation/mod.rs +++ b/src-tauri/src/evaluation/mod.rs @@ -1,16 +1,11 @@ mod state; -pub use state::*; use pest::Parser; pub use state::SiffraState; use crate::grammar::representation::ParsedLine; use crate::grammar::{parse_line, Rule, SiffraParser}; -use crate::representations::{Expression, Value, Float, Dimension}; - -const PI_STRING: &str = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230"; -const E_STRING: &str = "2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059"; - +use crate::representations::{Dimension, Expression, Float, Value}; pub type EvaluationResult = Result, ()>; pub fn evaluate_line(line: &str, state: &mut SiffraState) -> EvaluationResult { @@ -48,18 +43,8 @@ pub fn evaluate_expr(expr: &Expression, state: &SiffraState) -> Result Ok(Value::new( - { - Float::pi() - }, - None, - )), - "e" => Ok(Value::new( - { - Float::e() - }, - None, - )), + "pi" => Ok(Value::new(Float::pi(), None)), + "e" => Ok(Value::new(Float::e(), None)), _ => Err(()), } } @@ -95,7 +80,9 @@ pub fn evaluate_expr(expr: &Expression, state: &SiffraState) -> Result { if args.len() == 1 { - Ok(args[0].try_pow(&Value::new(Float::from(0.5), None)).ok_or(())?) + Ok(args[0] + .try_pow(&Value::new(Float::from(0.5), None)) + .ok_or(())?) } else { Err(()) } diff --git a/src-tauri/src/grammar/mod.rs b/src-tauri/src/grammar/mod.rs index 92b3c9d..db65c00 100644 --- a/src-tauri/src/grammar/mod.rs +++ b/src-tauri/src/grammar/mod.rs @@ -309,7 +309,7 @@ mod tests { #[test] fn test_parse_expr() { - let expr = parse_expr(SiffraParser::parse(Rule::expr, "log2(5(x)(y)) as mol %").unwrap()); + let _expr = parse_expr(SiffraParser::parse(Rule::expr, "log2(5(x)(y)) as mol %").unwrap()); } #[test] @@ -326,6 +326,6 @@ mod tests { #[test] fn test_ungrouped_functions() { - let expr = parse_expr(SiffraParser::parse(Rule::expr, "log 5a").unwrap()); + let _expr = parse_expr(SiffraParser::parse(Rule::expr, "log 5a").unwrap()); } } diff --git a/src-tauri/src/grammar/representation.rs b/src-tauri/src/grammar/representation.rs index 169efab..b482f10 100644 --- a/src-tauri/src/grammar/representation.rs +++ b/src-tauri/src/grammar/representation.rs @@ -1,6 +1,6 @@ +use crate::representations::{Dimension, Quantity}; use crate::representations::{Expression, Float, Value}; use std::str::FromStr; -use crate::representations::{Dimension, Quantity}; #[derive(Debug)] pub enum ParsedLine { @@ -79,19 +79,13 @@ impl TryFrom for Dimension { for (unit, power) in dimension.numerator { if !(unit.name == "unitless" || unit.name == "number") { - quantities.push(( - Quantity::from_str(unit.name.as_str())?, - Float::from(power), - )); + quantities.push((Quantity::from_str(unit.name.as_str())?, Float::from(power))); } } for (unit, power) in dimension.denominator { if !(unit.name == "unitless" || unit.name == "number") { - quantities.push(( - Quantity::from_str(unit.name.as_str())?, - Float::from(-power), - )); + quantities.push((Quantity::from_str(unit.name.as_str())?, Float::from(-power))); } } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 937a21c..a227bf7 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -12,7 +12,7 @@ struct SiffraLineOutput { } fn display_value(val: Value) -> String { - let (mut val, dim) = val.into_parts(); + let (val, dim) = val.into_parts(); let mut output = String::new(); diff --git a/src-tauri/src/representations/expression.rs b/src-tauri/src/representations/expression.rs index 10c564b..fd79d06 100644 --- a/src-tauri/src/representations/expression.rs +++ b/src-tauri/src/representations/expression.rs @@ -1,5 +1,5 @@ -use crate::representations::Value; use crate::representations::Dimension; +use crate::representations::Value; #[derive(Debug, Clone)] pub enum Expression { diff --git a/src-tauri/src/representations/mod.rs b/src-tauri/src/representations/mod.rs index 6b1100b..b9671bd 100644 --- a/src-tauri/src/representations/mod.rs +++ b/src-tauri/src/representations/mod.rs @@ -1,5 +1,5 @@ -mod value; mod expression; +mod value; -pub use value::*; pub use expression::*; +pub use value::*; diff --git a/src-tauri/src/representations/value/dimension/angle.rs b/src-tauri/src/representations/value/dimension/angle.rs index 040f7ee..e421d79 100644 --- a/src-tauri/src/representations/value/dimension/angle.rs +++ b/src-tauri/src/representations/value/dimension/angle.rs @@ -1,21 +1,12 @@ use crate::{quantity, ratio}; - - quantity!( Angle, [ - ( - Radian, - ratio!(1 / 1), - "rad", - "radian", - "radians", - "rads" - ), + (Radian, ratio!(1 / 1), "rad", "radian", "radians", "rads"), ( Degree, - ratio!( 1783366216531 / 102179357533440), + ratio!(1783366216531 / 102179357533440), "deg", "degree", "degs", @@ -23,11 +14,11 @@ quantity!( ), ( Revolution, - ratio!( 1783366216531 / 567663097408 ), + ratio!(1783366216531 / 567663097408), "rev", "revolution", "revs", "revolutions" ) ] -); \ No newline at end of file +); diff --git a/src-tauri/src/representations/value/dimension/mod.rs b/src-tauri/src/representations/value/dimension/mod.rs index d90904a..b67eb5d 100644 --- a/src-tauri/src/representations/value/dimension/mod.rs +++ b/src-tauri/src/representations/value/dimension/mod.rs @@ -1,24 +1,24 @@ mod amount; +mod angle; mod chemical; mod length; mod macros; mod mass; mod time; -mod angle; -use std::str::FromStr; +use crate::representations::Float; use std::collections::BTreeMap; use std::fmt::Display; use std::ops::Neg; -use crate::representations::Float; +use std::str::FromStr; pub use { amount::Amount, - chemical::{ Compound, Element }, + angle::Angle, + chemical::{Compound, Element}, length::Length, mass::Mass, time::Time, - angle::Angle, }; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] @@ -119,7 +119,7 @@ impl Display for Dimension { if *power > Float::from(0) { if !numerator.is_empty() { - quantity_shorthand = format!("*{}", quantity_shorthand); + quantity_shorthand = format!("*{}", quantity_shorthand); } numerator.push_str(&*quantity_shorthand); if *power > Float::from(1) { @@ -127,7 +127,7 @@ impl Display for Dimension { } } else if *power < Float::from(0) { if !denominator.is_empty() { - quantity_shorthand = format!("*{}", quantity_shorthand); + quantity_shorthand = format!("*{}", quantity_shorthand); } denominator.push_str(&*quantity_shorthand); if *power != Float::from(-1) { diff --git a/src-tauri/src/representations/value/float.rs b/src-tauri/src/representations/value/float.rs index b61364d..1e999d6 100644 --- a/src-tauri/src/representations/value/float.rs +++ b/src-tauri/src/representations/value/float.rs @@ -1,13 +1,15 @@ -use std::fmt::Display; -use std::ops::{Add, Div, Mul, Neg, Sub}; -use std::sync::{Arc, Mutex}; -use astro_float::{BigFloat, Radix}; use astro_float::Consts; use astro_float::RoundingMode; +use astro_float::{BigFloat, Radix}; use lazy_static::lazy_static; +use std::fmt::Display; +use std::ops::{Add, Div, Mul, Neg, Sub}; +use std::sync::{Arc, Mutex}; lazy_static! { - static ref CONST_CACHE: Arc> = Arc::new(Mutex::new(Consts::new().expect("Failed to initialize constants"))); + static ref CONST_CACHE: Arc> = Arc::new(Mutex::new( + Consts::new().expect("Failed to initialize constants") + )); } const PRECISION: usize = 256; @@ -103,13 +105,16 @@ impl Float { impl Display for Float { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let mut cache = CONST_CACHE.lock().unwrap(); - let string = self.0.format(Radix::Dec, ROUNDING_MODE, &mut *cache).unwrap(); + let string = self + .0 + .format(Radix::Dec, ROUNDING_MODE, &mut *cache) + .unwrap(); // In scientific notation let string = string.split('e').collect::>(); let (Some(mantissa), Some(exponent)) = (string.get(0), string.get(1)) else { write!(f, "{}", string[0])?; - return Ok(()) + return Ok(()); }; let mut exponent = exponent.parse::().unwrap(); @@ -263,8 +268,6 @@ impl PartialOrd for Float { } } - - impl From for Float where T: Into, @@ -282,8 +285,14 @@ mod tests { fn test_parse() { assert_eq!(Float::parse("1.00").unwrap(), Float::from(1)); assert_eq!(Float::parse("1.0E1").unwrap(), Float::from(10)); - assert!((&Float::parse("1.0E-1").unwrap() - &Float::from(0.1)).abs() < Float::from(0.0000000001)); - assert!((&Float::parse("15.03E+3").unwrap() - &Float::from(15030)).abs() < Float::from(0.0000000001)); + assert!( + (&Float::parse("1.0E-1").unwrap() - &Float::from(0.1)).abs() + < Float::from(0.0000000001) + ); + assert!( + (&Float::parse("15.03E+3").unwrap() - &Float::from(15030)).abs() + < Float::from(0.0000000001) + ); } #[test] @@ -292,7 +301,12 @@ mod tests { assert_eq!(Float::parse("001.00").unwrap().to_string(), "1"); assert_eq!(Float::parse("5e-20").unwrap().to_string(), "5E-20"); assert_eq!(Float::parse("999").unwrap().to_string(), "999"); - assert_eq!(Float::parse(".01123410918273418734182374").unwrap().to_string(), "0.01123410918273418734182374"); + assert_eq!( + Float::parse(".01123410918273418734182374") + .unwrap() + .to_string(), + "0.01123410918273418734182374" + ); assert_eq!(Float::parse("0").unwrap().to_string(), "0.0"); assert_eq!(Float::from(0).to_string(), "0.0"); assert_eq!(Float::from(-1).to_string(), "-1"); @@ -300,4 +314,4 @@ mod tests { assert_eq!(Float::from(f64::INFINITY).to_string(), "Inf"); assert_eq!(Float::from(f64::NEG_INFINITY).to_string(), "-Inf"); } -} \ No newline at end of file +} diff --git a/src-tauri/src/representations/value/mod.rs b/src-tauri/src/representations/value/mod.rs index eea4841..27c0b13 100644 --- a/src-tauri/src/representations/value/mod.rs +++ b/src-tauri/src/representations/value/mod.rs @@ -1,6 +1,4 @@ -use std::fmt::Display; use std::ops::{Add, Div, Mul, Neg, Sub}; -use std::str::FromStr; mod dimension; pub use dimension::*; @@ -96,11 +94,7 @@ impl Value { self.dimension .0 .iter() - .map(|(quantity, power)| { - (quantity.clone(), { - power.clone().neg() - }) - }) + .map(|(quantity, power)| (quantity.clone(), { power.clone().neg() })) .collect(), ), value: self.value.clone().recip(),