From 1cef80b8b860bcfd37b12a40a51376b922cb4243 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Tue, 13 Aug 2024 17:07:11 +0100 Subject: [PATCH] Working on building out async functionality --- Cargo.toml | 3 ++- src/app/metar/mod.rs | 11 +++++++++++ src/app/req/mod.rs | 27 +++++++++++++++++++++++++++ src/app/ui/tabs/metar_tab.rs | 7 ++++++- src/app/ui/tabs/mod.rs | 2 +- src/error.rs | 10 ++++++++++ src/main.rs | 6 ++++-- 7 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 src/error.rs diff --git a/Cargo.toml b/Cargo.toml index 7c9f199..79c3886 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,5 @@ egui = "0.28.1" eframe = "0.28.1" tracing-subscriber = "0.3.16" strum = { version = "0.26.3", features = ["derive"] } -tokio = "1.39.2" +tokio = { version = "1.39.2", features = ["full"] } +tokio-macros = { version = "2.4.0" } diff --git a/src/app/metar/mod.rs b/src/app/metar/mod.rs index 8b13789..ae2b9c3 100644 --- a/src/app/metar/mod.rs +++ b/src/app/metar/mod.rs @@ -1 +1,12 @@ +use super::req::Requests; +#[derive(PartialEq, Eq, Debug)] +pub struct Metar { + pub api: Requests, +} + +impl Default for Metar { + fn default() -> Self { + Metar { api: Requests{} } + } +} diff --git a/src/app/req/mod.rs b/src/app/req/mod.rs index 8b13789..a006569 100644 --- a/src/app/req/mod.rs +++ b/src/app/req/mod.rs @@ -1 +1,28 @@ +use std::error::Error; +use tokio::{io::AsyncReadExt, net::TcpStream}; + +#[derive(PartialEq, Eq, Debug)] +pub struct Requests {} + +impl Requests { + pub async fn make_request(&self, target: String) -> Result> { + + let tasks = tokio::spawn(async { + let mut buf: String = String::from(""); + + let mut stream = match TcpStream::connect(target).await { + Ok(stream) => stream, + Err(err) => return Err(Box::new(err)), + }; + // Buffer to read HTTP request into + let _ = stream.read_to_string(&mut buf); + + Ok(buf) + }); + + let buf = tasks.await??; + + Ok(buf) + } +} diff --git a/src/app/ui/tabs/metar_tab.rs b/src/app/ui/tabs/metar_tab.rs index e42c24b..60316de 100644 --- a/src/app/ui/tabs/metar_tab.rs +++ b/src/app/ui/tabs/metar_tab.rs @@ -1,11 +1,14 @@ use egui::Key; +use crate::app::metar::Metar; + use super::Tab; #[derive(Debug, PartialEq, Eq)] pub struct MetarTab { pub icaos: Vec, pub metars: Vec, + pub metar: Metar } impl Default for MetarTab { @@ -13,12 +16,13 @@ impl Default for MetarTab { Self { icaos: vec!["".to_string()], metars: vec![], + metar: Metar::default() } } } impl Tab for MetarTab { - fn display_tab(&mut self, ctx: &egui::Context, frame: &egui::Frame) { + async fn display_tab(&mut self, ctx: &egui::Context, frame: &egui::Frame) { egui::CentralPanel::default().frame(*frame).show(ctx, |ui| { // Given Key pressed, place focus on next item let new_icao_focus: bool = ui.input(|i| i.key_pressed(Key::Space)); @@ -29,6 +33,7 @@ impl Tab for MetarTab { if ui.button("Add More").clicked() || new_icao_focus { self.icaos.push("".to_string()); + self.metar.api.make_request(String::from("google.com")); } }); } diff --git a/src/app/ui/tabs/mod.rs b/src/app/ui/tabs/mod.rs index 64f9c5b..c3a64d8 100644 --- a/src/app/ui/tabs/mod.rs +++ b/src/app/ui/tabs/mod.rs @@ -65,7 +65,7 @@ impl TabState { /// Tab traits that allow tab display on the main application window pub trait Tab { - fn display_tab(&mut self, ctx: &egui::Context, frame: &egui::Frame); + async fn display_tab(&mut self, ctx: &egui::Context, frame: &egui::Frame); } /// Function to draw main set of tabs on the window diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..09e99cb --- /dev/null +++ b/src/error.rs @@ -0,0 +1,10 @@ +use strum::Display; + + +#[derive(Debug, Display)] +pub enum AvBagError { + UIError, + RequestNotAvailable +} + +impl std::error::Error for AvBagError {} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7d87cb8..e2e9719 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,12 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release mod app; +mod error; mod theme; use app::App; use egui::ViewportBuilder; -fn main() -> Result<(), eframe::Error> { +#[tokio::main] +async fn main() -> Result<(), Box> { // Log to stdout (if you run with `RUST_LOG=debug`). tracing_subscriber::fmt::init(); @@ -18,5 +20,5 @@ fn main() -> Result<(), eframe::Error> { let app = Box::new(App::default()); //Start rendering and run the application - eframe::run_native(app.name, options, Box::new(|_cc| Ok(app))) + Ok(eframe::run_native(app.name, options, Box::new(|_cc| Ok(app)))?) }