From 781aced32547ee0f4ab1d7b2dca67d3b1c7feb41 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Thu, 10 Mar 2022 16:03:13 -0500 Subject: [PATCH] move text stuff to json --- Cargo.toml | 1 + assets/text.json | 37 ++++++++++++++++++++++++++++++ assets/text_help_expr.txt | 13 ----------- assets/text_help_function.txt | 3 --- assets/text_help_other.txt | 2 -- assets/text_help_panel.txt | 5 ----- assets/text_help_vars.txt | 2 -- src/egui_app.rs | 42 +++++++++++++++++------------------ src/misc.rs | 14 ++++++++++++ 9 files changed, 72 insertions(+), 47 deletions(-) create mode 100644 assets/text.json delete mode 100644 assets/text_help_expr.txt delete mode 100644 assets/text_help_function.txt delete mode 100644 assets/text_help_other.txt delete mode 100644 assets/text_help_panel.txt delete mode 100644 assets/text_help_vars.txt diff --git a/Cargo.toml b/Cargo.toml index 43ccc51..3f1fd6d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ exmex = { git = "https://github.com/bertiqwerty/exmex.git", branch = "main", fea lazy_static = "1.4.0" tar = "0.4.38" ruzstd = { git = "https://github.com/KillingSpark/zstd-rs.git" } +serde_json = "1.0.79" [build-dependencies] shadow-rs = "0.9" diff --git a/assets/text.json b/assets/text.json new file mode 100644 index 0000000..f887251 --- /dev/null +++ b/assets/text.json @@ -0,0 +1,37 @@ +{ + "help_expr": [ + "- sqrt(x): square root of x", + "- abs(x): absolute value of x", + "- ln(x): log with base e", + "- log10(x): base 10 logarithm of x", + "- log(x): same as log10(x)", + "- sin(x): Sine of x", + "- cos(x): Cosine of x", + "- tan(x): Tangent of x", + "- asin(x): arcsine of x", + "- acos(x): arccosine of x", + "- atan(x): arctangent of x", + "- atan2, sinh, cosh, tanh, asinh, acosh, atanh", + "- floor, ceil, round, signum" + ], + "help_vars": [ + "- Euler's number is supported via 'E' (note it being uppercase)", + "- PI is available through 'pi' or 'π'" + ], + "help_panel": [ + "- The 'Panel' button on the top bar toggles if the side bar should be shown or not.", + "- The 'Add Function' button on the top panel adds a new function to be graphed. You can then configure that function in the side panel.", + "- The 'Help' button on the top bar opens and closes this window!", + "- The 'Info' button provides information on the build currently running.", + "- The Sun/Moon button toggles Dark and Light mode." + ], + "help_function": [ + "- The 'X' button before the '∫' button allows you to delete the function in question. Deleting a function is prevented if only 1 function exists.", + "- The ∫ button (between the 'X' and 'd/dx' buttons) indicates whether to integrate the function in question.", + "- The 'd/dx' button next to the function input indicates whether or not calculating the derivative is enabled or not." + ], + "help_other": [ + "- Extrema (local minimums and maximums) and Roots (intersections with the x-axis) are displayed though yellow and light blue points on the graph. You can toggle these in the Side Panel.", + "- In some edge cases, math functions may not parse correctly. More specifically with implicit multiplication. If you incounter this issue, please do report it on the project's Github page (linked on the side panel). But a current workaround would be explicitly stating a multiplication operation through the use of an asterisk." + ] +} \ No newline at end of file diff --git a/assets/text_help_expr.txt b/assets/text_help_expr.txt deleted file mode 100644 index f42b76d..0000000 --- a/assets/text_help_expr.txt +++ /dev/null @@ -1,13 +0,0 @@ -- sqrt(x): square root of x -- abs(x): absolute value of x -- ln(x): log with base e -- log10(x): base 10 logarithm of x -- log(x): same as log10(x) -- sin(x): Sine of x -- cos(x): Cosine of x -- tan(x): Tangent of x -- asin(x): arcsine of x -- acos(x): arccosine of x -- atan(x): arctangent of x -- atan2, sinh, cosh, tanh, asinh, acosh, atanh -- floor, ceil, round, signum \ No newline at end of file diff --git a/assets/text_help_function.txt b/assets/text_help_function.txt deleted file mode 100644 index 220cfb4..0000000 --- a/assets/text_help_function.txt +++ /dev/null @@ -1,3 +0,0 @@ -- The 'X' button before the '∫' button allows you to delete the function in question. Deleting a function is prevented if only 1 function exists. -- The ∫ button (between the 'X' and 'd/dx' buttons) indicates whether to integrate the function in question. -- The 'd/dx' button next to the function input indicates whether or not calculating the derivative is enabled or not. \ No newline at end of file diff --git a/assets/text_help_other.txt b/assets/text_help_other.txt deleted file mode 100644 index 9d9fa97..0000000 --- a/assets/text_help_other.txt +++ /dev/null @@ -1,2 +0,0 @@ -- Extrema (local minimums and maximums) and Roots (intersections with the x-axis) are displayed though yellow and light blue points on the graph. You can toggle these in the Side Panel. -- In some edge cases, math functions may not parse correctly. More specifically with implicit multiplication. If you incounter this issue, please do report it on the project's Github page (linked on the side panel). But a current workaround would be explicitly stating a multiplication operation through the use of an asterisk. \ No newline at end of file diff --git a/assets/text_help_panel.txt b/assets/text_help_panel.txt deleted file mode 100644 index 971fd4b..0000000 --- a/assets/text_help_panel.txt +++ /dev/null @@ -1,5 +0,0 @@ -- The 'Panel' button on the top bar toggles if the side bar should be shown or not. -- The 'Add Function' button on the top panel adds a new function to be graphed. You can then configure that function in the side panel. -- The 'Help' button on the top bar opens and closes this window! -- The 'Info' button provides information on the build currently running. -- The Sun/Moon button toggles Dark and Light mode. \ No newline at end of file diff --git a/assets/text_help_vars.txt b/assets/text_help_vars.txt deleted file mode 100644 index 3b76a72..0000000 --- a/assets/text_help_vars.txt +++ /dev/null @@ -1,2 +0,0 @@ -- Euler's number is supported via 'E' (note it being uppercase) -- PI is available through 'pi' or 'π' \ No newline at end of file diff --git a/src/egui_app.rs b/src/egui_app.rs index ca1af6c..64f738e 100644 --- a/src/egui_app.rs +++ b/src/egui_app.rs @@ -1,5 +1,5 @@ use crate::function::{FunctionEntry, RiemannSum, EMPTY_FUNCTION_ENTRY}; -use crate::misc::{debug_log, log_helper}; +use crate::misc::{debug_log, log_helper, parse_value}; use crate::parsing::{process_func_str, test_func}; use const_format::formatc; @@ -11,6 +11,7 @@ use egui::{ }; use epi::{Frame, Storage}; use instant::Duration; +use serde_json::Value; use shadow_rs::shadow; use std::{ collections::BTreeMap, @@ -112,24 +113,18 @@ lazy_static::lazy_static! { panic!("Font File {} not expected!", path_string); } } - } else if path_string.ends_with(".txt") { - // Parse text files + } else if path_string.ends_with(".json") { + // Parse json file let string_data = str::from_utf8(&data).unwrap().to_string(); match path_string.as_ref() { - "text_help_expr.txt" => { - text_help_expr = Some(string_data); - }, - "text_help_vars.txt" => { - text_help_vars = Some(string_data); - }, - "text_help_panel.txt" => { - text_help_panel = Some(string_data); - }, - "text_help_function.txt" => { - text_help_function = Some(string_data); - }, - "text_help_other.txt" => { - text_help_other = Some(string_data); + "text.json" => { + let json_data: Value = serde_json::from_str(&string_data).unwrap(); + text_help_expr = Some(parse_value(&json_data["help_expr"])); + text_help_vars = Some(parse_value(&json_data["help_vars"])); + text_help_panel = Some(parse_value(&json_data["help_panel"])); + text_help_function = Some(parse_value(&json_data["help_function"])); + text_help_other = Some(parse_value(&json_data["help_other"])); + }, _ => { panic!("Text file {} not expected!", path_string); @@ -197,25 +192,28 @@ fn test_file_data() { FontData::from_owned(include_bytes!("../assets/Hack-Regular.ttf").to_vec()) ); + let json_data: Value = serde_json::from_str(&include_str!("../assets/text.json")).unwrap(); + assert_eq!( FILE_DATA.text_help_expr, - include_str!("../assets/text_help_expr.txt") + parse_value(&json_data["help_expr"]) ); + assert_eq!( FILE_DATA.text_help_vars, - include_str!("../assets/text_help_vars.txt") + parse_value(&json_data["help_vars"]) ); assert_eq!( FILE_DATA.text_help_panel, - include_str!("../assets/text_help_panel.txt") + parse_value(&json_data["help_panel"]) ); assert_eq!( FILE_DATA.text_help_function, - include_str!("../assets/text_help_function.txt") + parse_value(&json_data["help_function"]) ); assert_eq!( FILE_DATA.text_help_other, - include_str!("../assets/text_help_other.txt") + parse_value(&json_data["help_other"]) ); } diff --git a/src/misc.rs b/src/misc.rs index 2319004..dedf687 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -179,3 +179,17 @@ pub fn newtons_method( } output_list } + +pub fn parse_value(value: &serde_json::Value) -> String { + let string_vector: Vec<&str> = value + .as_array() + .unwrap() + .iter() + .map(|ele| ele.as_str().unwrap()) + .collect::>(); + string_vector + .iter() + .fold(String::new(), |s, l| s + l + "\n") + .trim() + .to_string() +}