Repository: emanuele-em/proxelar (language: rust)

Issue:
Make order with constants and hard coded parts
some parts are hardcoded to fix different and stupid bugs.
It could be a good idea to store all numerical variables in idiomatic const to put in main.rs file:
- offsets
- dinstances
- paddings
- etc...


Discussion / hints:

Fix patch:
diff --git a/mitm_proxy/src/main.rs b/mitm_proxy/src/main.rs
index 3f47312..46d5b8c 100644
--- a/mitm_proxy/src/main.rs
+++ b/mitm_proxy/src/main.rs
@@ -13,10 +13,7 @@ const X: f32 = 980.;
 const Y: f32 = 960.0;
 const PADDING: f32 = 20.;
 const FONT_SIZE: f32 = 17.;
-
-// fn fetch_requests(){
-//     ProxyAPI::new().fetch();
-// }
+const LOGO_ASSET_PATH: &str = "./assets/logo.png";
 
 impl App for MitmProxy {
     fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
@@ -54,7 +51,7 @@ fn load_icon(path: &str) -> eframe::IconData {
 fn main() {
     let native_options = eframe::NativeOptions {
         initial_window_size: Some(Vec2::new(X, Y)),
-        icon_data: Some(load_icon("./assets/logo.png")),
+        icon_data = Some(load_icon(LOGO_ASSET_PATH)),
         ..eframe::NativeOptions::default()
     };
 
diff --git a/mitm_proxy/src/mitm_proxy.rs b/mitm_proxy/src/mitm_proxy.rs
index 9519f24..c887c51 100644
--- a/mitm_proxy/src/mitm_proxy.rs
+++ b/mitm_proxy/src/mitm_proxy.rs
@@ -19,6 +19,22 @@ use egui_extras::{Column, TableBuilder};
 use proxyapi::hyper::Method;
 use serde::{Deserialize, Serialize};
 
+const APP_NAME: &str = "MitmProxy";
+
+const ANCHOR_OFFSET: [f32; 2] = [0.0, -10.0];
+
+const DEFAULT_HEADING_FONT_SIZE: f32 = 30.0;
+const DEFAULT_BODY_FONT_SIZE: f32 = 12.;
+const DEFAULT_BUTTON_FONT_SIZE: f32 = 20.0;
+
+const LOCALHOST: &str = "127.0.0.1";
+const PORT: &str = "8100";
+
+const OPEN_SANS_FONT: &str = "OpenSans";
+const OPEN_SANS_FONT_PATH_AS_BYTES: &'static [u8] = include_bytes!("../../fonts/OpenSans.ttf");
+
+
+
 #[derive(Serialize, Deserialize)]
 struct MitmProxyConfig {
     dark_mode: bool,
@@ -83,7 +99,7 @@ impl MitmProxyState {
             selected_request: None,
             selected_request_method: MethodFilter::All,
             detail_option: InfoOptions::Request,
-            listen_on: "127.0.0.1:8100".to_string(),
+            listen_on: format!("{}:{}", LOCALHOST, PORT),
         }
     }
 }
@@ -98,7 +114,7 @@ pub struct MitmProxy {
 impl MitmProxy {
     pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
         Self::configure_fonts(cc);
-        let config: MitmProxyConfig = confy::load("MitmProxy", None).unwrap_or_default();
+        let config: MitmProxyConfig = confy::load(APP_NAME, None).unwrap_or_default();
         let state = MitmProxyState::new();
 
         MitmProxy {
@@ -138,23 +154,23 @@ impl MitmProxy {
         let mut fonts = FontDefinitions::default();
 
         fonts.font_data.insert(
-            "OpenSans".to_owned(),
-            FontData::from_static(include_bytes!("../../fonts/OpenSans.ttf")),
+            OPEN_SANS_FONT.to_owned(),
+            FontData::from_static(OPEN_SANS_FONT_PATH_AS_BYTES),
         );
 
         fonts
             .families
             .get_mut(&FontFamily::Proportional)
             .unwrap()
-            .insert(0, "OpenSans".to_owned());
+            .insert(0, OPEN_SANS_FONT.to_owned());
 
         cc.egui_ctx.set_fonts(fonts);
 
         let style = Style {
             text_styles: [
-                (Heading, FontId::new(30.0, FontFamily::Proportional)),
-                (Body, FontId::new(12., FontFamily::Proportional)),
-                (Button, FontId::new(20.0, FontFamily::Proportional)),
+                (Heading, FontId::new(DEFAULT_HEADING_FONT_SIZE, FontFamily::Proportional)),
+                (Body, FontId::new(DEFAULT_BODY_FONT_SIZE, FontFamily::Proportional)),
+                (Button, FontId::new(DEFAULT_BUTTON_FONT_SIZE, FontFamily::Proportional)),
             ]
             .into(),
             ..Style::default()
@@ -408,7 +424,7 @@ impl MitmProxy {
             egui::Window::new("bottom_stop")
                 .title_bar(false)
                 .resizable(false)
-                .anchor(Align2::CENTER_BOTTOM, [0.0, -10.0])
+                .anchor(Align2::CENTER_BOTTOM, ANCHOR_OFFSET)
                 .default_height(30.0)
                 .show(ctx, |ui| {
                     ui.horizontal_centered(|ui| {
diff --git a/mitm_proxy/src/requests.rs b/mitm_proxy/src/requests.rs
index 73c4574..b231040 100644
--- a/mitm_proxy/src/requests.rs
+++ b/mitm_proxy/src/requests.rs
@@ -100,8 +100,12 @@ impl RequestInfo {
     pub fn render_row(&self, row: &mut TableRow) {
         let req = self.request.as_ref().unwrap();
         let res = self.response.as_ref().unwrap();
-        let time = (res.time() as f64 - req.time() as f64) * 10_f64.powf(-9.0);
-        let time = f64::trunc(time * 1000.);
+
+        let time = {
+            let innertime = (res.time() as f64 - req.time() as f64) * 10_f64.powf(-9.0);
+            f64::trunc(innertime * 1000.)
+        };
+
         row.col(|ui| {
             ui.label(req.uri().to_string());
         });


Test patch: