cargo clippy + fmt

This commit is contained in:
Simon Gardling 2025-12-03 11:43:42 -05:00
parent 5a92020dae
commit aa07631296
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
15 changed files with 2069 additions and 2016 deletions

View File

@ -1,4 +0,0 @@
edition = "2021"
fn_params_layout = "Compressed"
fn_single_line = true
hard_tabs = true

View File

@ -7,8 +7,8 @@ use std::{
}; };
use epaint::{ use epaint::{
text::{FontData, FontDefinitions, FontTweak},
FontFamily, FontFamily,
text::{FontData, FontDefinitions, FontTweak},
}; };
use run_script::ScriptOptions; use run_script::ScriptOptions;

View File

@ -1,12 +1,12 @@
use crate::math_app::AppSettings; use crate::math_app::AppSettings;
use crate::misc::{newtons_method_helper, step_helper, EguiHelper}; use crate::misc::{EguiHelper, newtons_method_helper, step_helper};
use egui::{Checkbox, Context}; use egui::{Checkbox, Context};
use egui_plot::{Bar, BarChart, PlotPoint, PlotUi}; use egui_plot::{Bar, BarChart, PlotPoint, PlotUi};
use epaint::Color32; use epaint::Color32;
use parsing::{generate_hint, AutoComplete}; use parsing::{AutoComplete, generate_hint};
use parsing::{process_func_str, BackingFunction}; use parsing::{BackingFunction, process_func_str};
use serde::{ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer, ser::SerializeStruct};
use std::{ use std::{
fmt::{self, Debug}, fmt::{self, Debug},
hash::{Hash, Hasher}, hash::{Hash, Hasher},
@ -23,7 +23,9 @@ pub enum Riemann {
} }
impl fmt::Display for Riemann { impl fmt::Display for Riemann {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self) } fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
} }
/// `FunctionEntry` is a function that can calculate values, integrals, derivatives, etc etc /// `FunctionEntry` is a function that can calculate values, integrals, derivatives, etc etc
@ -142,7 +144,9 @@ impl Default for FunctionEntry {
} }
impl FunctionEntry { impl FunctionEntry {
pub const fn is_some(&self) -> bool { !self.function.is_none() } pub const fn is_some(&self) -> bool {
!self.function.is_none()
}
pub fn settings_window(&mut self, ctx: &Context) { pub fn settings_window(&mut self, ctx: &Context) {
let mut invalidate_nth = false; let mut invalidate_nth = false;
@ -172,7 +176,9 @@ impl FunctionEntry {
} }
/// Get function's cached test result /// Get function's cached test result
pub fn get_test_result(&self) -> &Option<String> { &self.test_result } pub fn get_test_result(&self) -> &Option<String> {
&self.test_result
}
/// Update function string and test it /// Update function string and test it
pub fn update_string(&mut self, raw_func_str: &str) { pub fn update_string(&mut self, raw_func_str: &str) {
@ -198,7 +204,11 @@ impl FunctionEntry {
/// Creates and does the math for creating all the rectangles under the graph /// Creates and does the math for creating all the rectangles under the graph
fn integral_rectangles( fn integral_rectangles(
&mut self, integral_min_x: f64, integral_max_x: f64, sum: Riemann, integral_num: usize, &mut self,
integral_min_x: f64,
integral_max_x: f64,
sum: Riemann,
integral_num: usize,
) -> (Vec<(f64, f64)>, f64) { ) -> (Vec<(f64, f64)>, f64) {
let step = (integral_max_x - integral_min_x) / (integral_num as f64); let step = (integral_max_x - integral_min_x) / (integral_num as f64);
@ -235,7 +245,10 @@ impl FunctionEntry {
/// Helps with processing newton's method depending on level of derivative /// Helps with processing newton's method depending on level of derivative
fn newtons_method_helper( fn newtons_method_helper(
&mut self, threshold: f64, derivative_level: usize, range: &std::ops::Range<f64>, &mut self,
threshold: f64,
derivative_level: usize,
range: &std::ops::Range<f64>,
) -> Vec<PlotPoint> { ) -> Vec<PlotPoint> {
self.function.generate_derivative(derivative_level); self.function.generate_derivative(derivative_level);
self.function.generate_derivative(derivative_level + 1); self.function.generate_derivative(derivative_level + 1);
@ -244,15 +257,15 @@ impl FunctionEntry {
threshold, threshold,
range, range,
self.back_data.as_slice(), self.back_data.as_slice(),
&self.function.get_function_derivative(0), self.function.get_function_derivative(0),
&self.function.get_function_derivative(1), self.function.get_function_derivative(1),
), ),
1 => newtons_method_helper( 1 => newtons_method_helper(
threshold, threshold,
range, range,
self.derivative_data.as_slice(), self.derivative_data.as_slice(),
&self.function.get_function_derivative(1), self.function.get_function_derivative(1),
&self.function.get_function_derivative(2), self.function.get_function_derivative(2),
), ),
_ => unreachable!(), _ => unreachable!(),
}; };
@ -265,7 +278,10 @@ impl FunctionEntry {
/// Does the calculations and stores results in `self` /// Does the calculations and stores results in `self`
pub fn calculate( pub fn calculate(
&mut self, width_changed: bool, min_max_changed: bool, did_zoom: bool, &mut self,
width_changed: bool,
min_max_changed: bool,
did_zoom: bool,
settings: AppSettings, settings: AppSettings,
) { ) {
if self.test_result.is_some() | self.function.is_none() { if self.test_result.is_some() | self.function.is_none() {
@ -353,7 +369,10 @@ impl FunctionEntry {
/// Displays the function's output on PlotUI `plot_ui` with settings `settings`. /// Displays the function's output on PlotUI `plot_ui` with settings `settings`.
/// Returns an `Option<f64>` of the calculated integral. /// Returns an `Option<f64>` of the calculated integral.
pub fn display( pub fn display(
&self, plot_ui: &mut PlotUi, settings: &AppSettings, main_plot_color: Color32, &self,
plot_ui: &mut PlotUi,
settings: &AppSettings,
main_plot_color: Color32,
) -> Option<f64> { ) -> Option<f64> {
if self.test_result.is_some() | self.function.is_none() { if self.test_result.is_some() | self.function.is_none() {
return None; return None;
@ -455,25 +474,37 @@ impl FunctionEntry {
/// Invalidate `back` data /// Invalidate `back` data
#[inline] #[inline]
fn clear_back(&mut self) { self.back_data.clear(); } fn clear_back(&mut self) {
self.back_data.clear();
}
/// Invalidate Integral data /// Invalidate Integral data
#[inline] #[inline]
fn clear_integral(&mut self) { self.integral_data = None; } fn clear_integral(&mut self) {
self.integral_data = None;
}
/// Invalidate Derivative data /// Invalidate Derivative data
#[inline] #[inline]
fn clear_derivative(&mut self) { self.derivative_data.clear(); } fn clear_derivative(&mut self) {
self.derivative_data.clear();
}
/// Invalidates `n`th derivative data /// Invalidates `n`th derivative data
#[inline] #[inline]
fn clear_nth(&mut self) { self.nth_derivative_data = None } fn clear_nth(&mut self) {
self.nth_derivative_data = None
}
/// Invalidate extrema data /// Invalidate extrema data
#[inline] #[inline]
fn clear_extrema(&mut self) { self.extrema_data.clear() } fn clear_extrema(&mut self) {
self.extrema_data.clear()
}
/// Invalidate root data /// Invalidate root data
#[inline] #[inline]
fn clear_roots(&mut self) { self.root_data.clear() } fn clear_roots(&mut self) {
self.root_data.clear()
}
} }

View File

@ -1,8 +1,5 @@
use crate::{ use crate::{
consts::COLORS, consts::COLORS, function_entry::FunctionEntry, misc::random_u64, widgets::widgets_ontop,
function_entry::FunctionEntry,
misc::{random_u64},
widgets::widgets_ontop,
}; };
use egui::{Button, Id, Key, Modifiers, TextEdit, WidgetText}; use egui::{Button, Id, Key, Modifiers, TextEdit, WidgetText};
use emath::vec2; use emath::vec2;
@ -22,14 +19,13 @@ impl Default for FunctionManager {
fn default() -> Self { fn default() -> Self {
let mut vec: Functions = Vec::with_capacity(COLORS.len()); let mut vec: Functions = Vec::with_capacity(COLORS.len());
vec.push(( vec.push((
Id::new(11414819524356497634 as u64), // Random number here to avoid call to crate::misc::random_u64() Id::new(11414819524356497634_u64), // Random number here to avoid call to crate::misc::random_u64()
FunctionEntry::default(), FunctionEntry::default(),
)); ));
Self { functions: vec } Self { functions: vec }
} }
} }
impl Serialize for FunctionManager { impl Serialize for FunctionManager {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
@ -151,7 +147,7 @@ impl FunctionManager {
let autocomplete_popup_id = Id::new("autocomplete popup"); let autocomplete_popup_id = Id::new("autocomplete popup");
egui::popup_below_widget(ui, autocomplete_popup_id.clone(), &re, |ui| { egui::popup_below_widget(ui, autocomplete_popup_id, &re, |ui| {
hints.iter().enumerate().for_each(|(i, candidate)| { hints.iter().enumerate().for_each(|(i, candidate)| {
if ui if ui
.selectable_label(i == function.autocomplete.i, *candidate) .selectable_label(i == function.autocomplete.i, *candidate)
@ -170,7 +166,7 @@ impl FunctionManager {
movement = Movement::Complete; movement = Movement::Complete;
} else { } else {
ui.memory_mut(|x| x.open_popup(autocomplete_popup_id.clone())); ui.memory_mut(|x| x.open_popup(autocomplete_popup_id));
} }
} }
@ -261,12 +257,17 @@ impl FunctionManager {
} }
#[inline] #[inline]
pub fn len(&self) -> usize { self.functions.len() } pub fn len(&self) -> usize {
self.functions.len()
}
#[inline]
pub fn get_entries_mut(&mut self) -> &mut Functions { &mut self.functions } #[inline]
pub fn get_entries_mut(&mut self) -> &mut Functions {
#[inline] &mut self.functions
pub fn get_entries(&self) -> &Functions { &self.functions } }
#[inline]
pub fn get_entries(&self) -> &Functions {
&self.functions
}
} }

View File

@ -13,8 +13,8 @@ pub use crate::{
function_entry::{FunctionEntry, Riemann}, function_entry::{FunctionEntry, Riemann},
math_app::AppSettings, math_app::AppSettings,
misc::{ misc::{
hashed_storage_create, hashed_storage_read, newtons_method, option_vec_printer, EguiHelper, HashBytes, hashed_storage_create, hashed_storage_read, newtons_method,
step_helper, EguiHelper, HashBytes, option_vec_printer, step_helper,
}, },
unicode_helper::{to_chars_array, to_unicode_hash}, unicode_helper::{to_chars_array, to_unicode_hash},
}; };

View File

@ -1,13 +1,13 @@
use crate::{ use crate::{
consts::{build, BUILD_INFO, COLORS, DEFAULT_INTEGRAL_NUM, DEFAULT_MAX_X, DEFAULT_MIN_X}, consts::{BUILD_INFO, COLORS, DEFAULT_INTEGRAL_NUM, DEFAULT_MAX_X, DEFAULT_MIN_X, build},
function_entry::Riemann, function_entry::Riemann,
function_manager::FunctionManager, function_manager::FunctionManager,
misc::option_vec_printer, misc::option_vec_printer,
}; };
use eframe::App; use eframe::App;
use egui::{ use egui::{
style::Margin, Button, CentralPanel, Color32, ComboBox, Context, DragValue, Frame, Key, Layout, Button, CentralPanel, Color32, ComboBox, Context, DragValue, Frame, Key, Layout, SidePanel,
SidePanel, TopBottomPanel, Ui, Vec2, Window, TopBottomPanel, Ui, Vec2, Window, style::Margin,
}; };
use egui_plot::Plot; use egui_plot::Plot;
@ -111,7 +111,9 @@ pub struct MathApp {
} }
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
fn get_window() -> web_sys::Window { web_sys::window().expect("Could not get web_sys window") } fn get_window() -> web_sys::Window {
web_sys::window().expect("Could not get web_sys window")
}
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
fn get_localstorage() -> web_sys::Storage { fn get_localstorage() -> web_sys::Storage {
@ -176,8 +178,11 @@ impl MathApp {
fn decompress_fonts() -> epaint::text::FontDefinitions { fn decompress_fonts() -> epaint::text::FontDefinitions {
let mut data = Vec::new(); let mut data = Vec::new();
let _ = ruzstd::StreamingDecoder::new( let _ =
&mut const { include_bytes!(concat!(env!("OUT_DIR"), "/compressed_data")).as_slice() }, ruzstd::StreamingDecoder::new(
&mut const {
include_bytes!(concat!(env!("OUT_DIR"), "/compressed_data")).as_slice()
},
) )
.expect("unable to decode compressed data") .expect("unable to decode compressed data")
.read_to_end(&mut data) .read_to_end(&mut data)
@ -357,7 +362,7 @@ impl MathApp {
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
{ {
tracing::info!("Saving function data"); tracing::info!("Saving function data");
use crate::misc::{hashed_storage_create, HashBytes}; use crate::misc::{HashBytes, hashed_storage_create};
let hash: HashBytes = let hash: HashBytes =
unsafe { std::mem::transmute::<&str, HashBytes>(build::SHORT_COMMIT) }; unsafe { std::mem::transmute::<&str, HashBytes>(build::SHORT_COMMIT) };
let saved_data = hashed_storage_create( let saved_data = hashed_storage_create(

View File

@ -1,4 +1,4 @@
use base64::{engine::general_purpose, Engine as _}; use base64::{Engine as _, engine::general_purpose};
use egui_plot::{Line, PlotPoint, PlotPoints, Points}; use egui_plot::{Line, PlotPoint, PlotPoints, Points};
use emath::Pos2; use emath::Pos2;
use getrandom::getrandom; use getrandom::getrandom;
@ -80,7 +80,10 @@ pub fn decimal_round(x: f64, n: usize) -> f64 {
/// `f_1` is f'(x) aka the derivative of f(x) /// `f_1` is f'(x) aka the derivative of f(x)
/// The function returns a Vector of `x` values where roots occur /// The function returns a Vector of `x` values where roots occur
pub fn newtons_method_helper( pub fn newtons_method_helper(
threshold: f64, range: &std::ops::Range<f64>, data: &[PlotPoint], f: &FlatExWrapper, threshold: f64,
range: &std::ops::Range<f64>,
data: &[PlotPoint],
f: &FlatExWrapper,
f_1: &FlatExWrapper, f_1: &FlatExWrapper,
) -> Vec<f64> { ) -> Vec<f64> {
data.iter() data.iter()
@ -99,7 +102,10 @@ pub fn newtons_method_helper(
/// `f_1` is f'(x) aka the derivative of f(x) /// `f_1` is f'(x) aka the derivative of f(x)
/// The function returns an `Option<f64>` of the x value at which a root occurs /// The function returns an `Option<f64>` of the x value at which a root occurs
pub fn newtons_method( pub fn newtons_method(
f: &FlatExWrapper, f_1: &FlatExWrapper, start_x: f64, range: &std::ops::Range<f64>, f: &FlatExWrapper,
f_1: &FlatExWrapper,
start_x: f64,
range: &std::ops::Range<f64>,
threshold: f64, threshold: f64,
) -> Option<f64> { ) -> Option<f64> {
let mut x1: f64 = start_x; let mut x1: f64 = start_x;

View File

@ -3,7 +3,10 @@ use egui::{Id, InnerResponse};
/// Creates an area ontop of a widget with an y offset /// Creates an area ontop of a widget with an y offset
pub fn widgets_ontop<R>( pub fn widgets_ontop<R>(
ui: &egui::Ui, id: Id, re: &egui::Response, y_offset: f32, ui: &egui::Ui,
id: Id,
re: &egui::Response,
y_offset: f32,
add_contents: impl FnOnce(&mut egui::Ui) -> R, add_contents: impl FnOnce(&mut egui::Ui) -> R,
) -> InnerResponse<R> { ) -> InnerResponse<R> {
let area = egui::Area::new(id) let area = egui::Area::new(id)

View File

@ -1,8 +1,13 @@
use ytbn_graphing_software::{AppSettings, EguiHelper, FunctionEntry, Riemann}; use ytbn_graphing_software::{AppSettings, EguiHelper, FunctionEntry, Riemann};
fn app_settings_constructor( fn app_settings_constructor(
sum: Riemann, integral_min_x: f64, integral_max_x: f64, pixel_width: usize, sum: Riemann,
integral_num: usize, min_x: f64, max_x: f64, integral_min_x: f64,
integral_max_x: f64,
pixel_width: usize,
integral_num: usize,
min_x: f64,
max_x: f64,
) -> AppSettings { ) -> AppSettings {
AppSettings { AppSettings {
riemann_sum: sum, riemann_sum: sum,
@ -253,10 +258,16 @@ fn do_test(sum: Riemann, area_target: f64) {
} }
#[test] #[test]
fn left_function() { do_test(Riemann::Left, 0.9600000000000001); } fn left_function() {
do_test(Riemann::Left, 0.9600000000000001);
}
#[test] #[test]
fn middle_function() { do_test(Riemann::Middle, 0.92); } fn middle_function() {
do_test(Riemann::Middle, 0.92);
}
#[test] #[test]
fn right_function() { do_test(Riemann::Right, 0.8800000000000001); } fn right_function() {
do_test(Riemann::Right, 0.8800000000000001);
}