Working on building out async functionality

This commit is contained in:
Luke Else 2024-08-13 17:07:11 +01:00
parent 3e724cf340
commit 1cef80b8b8
7 changed files with 61 additions and 5 deletions

View File

@ -10,4 +10,5 @@ egui = "0.28.1"
eframe = "0.28.1" eframe = "0.28.1"
tracing-subscriber = "0.3.16" tracing-subscriber = "0.3.16"
strum = { version = "0.26.3", features = ["derive"] } strum = { version = "0.26.3", features = ["derive"] }
tokio = "1.39.2" tokio = { version = "1.39.2", features = ["full"] }
tokio-macros = { version = "2.4.0" }

View File

@ -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{} }
}
}

View File

@ -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<String, Box<dyn Error>> {
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)
}
}

View File

@ -1,11 +1,14 @@
use egui::Key; use egui::Key;
use crate::app::metar::Metar;
use super::Tab; use super::Tab;
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub struct MetarTab { pub struct MetarTab {
pub icaos: Vec<String>, pub icaos: Vec<String>,
pub metars: Vec<String>, pub metars: Vec<String>,
pub metar: Metar
} }
impl Default for MetarTab { impl Default for MetarTab {
@ -13,12 +16,13 @@ impl Default for MetarTab {
Self { Self {
icaos: vec!["".to_string()], icaos: vec!["".to_string()],
metars: vec![], metars: vec![],
metar: Metar::default()
} }
} }
} }
impl Tab for MetarTab { 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| { egui::CentralPanel::default().frame(*frame).show(ctx, |ui| {
// Given Key pressed, place focus on next item // Given Key pressed, place focus on next item
let new_icao_focus: bool = ui.input(|i| i.key_pressed(Key::Space)); 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 { if ui.button("Add More").clicked() || new_icao_focus {
self.icaos.push("".to_string()); self.icaos.push("".to_string());
self.metar.api.make_request(String::from("google.com"));
} }
}); });
} }

View File

@ -65,7 +65,7 @@ impl TabState {
/// Tab traits that allow tab display on the main application window /// Tab traits that allow tab display on the main application window
pub trait Tab { 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 /// Function to draw main set of tabs on the window

10
src/error.rs Normal file
View File

@ -0,0 +1,10 @@
use strum::Display;
#[derive(Debug, Display)]
pub enum AvBagError {
UIError,
RequestNotAvailable
}
impl std::error::Error for AvBagError {}

View File

@ -1,10 +1,12 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
mod app; mod app;
mod error;
mod theme; mod theme;
use app::App; use app::App;
use egui::ViewportBuilder; use egui::ViewportBuilder;
fn main() -> Result<(), eframe::Error> { #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Log to stdout (if you run with `RUST_LOG=debug`). // Log to stdout (if you run with `RUST_LOG=debug`).
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
@ -18,5 +20,5 @@ fn main() -> Result<(), eframe::Error> {
let app = Box::new(App::default()); let app = Box::new(App::default());
//Start rendering and run the application //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)))?)
} }