From 875a25022fe5ffe1507cee6949fd026857e45841 Mon Sep 17 00:00:00 2001 From: Jack Lavigne Date: Fri, 26 Apr 2024 16:52:37 -0400 Subject: [PATCH] style: format --- src/wasm/src/dispatcher.rs | 14 +- src/wasm/src/meta.rs | 346 ++++++++++++++++----------------- src/wasm/src/network_helper.rs | 108 +++++----- 3 files changed, 238 insertions(+), 230 deletions(-) diff --git a/src/wasm/src/dispatcher.rs b/src/wasm/src/dispatcher.rs index 37c391ce..3b8ca38a 100644 --- a/src/wasm/src/dispatcher.rs +++ b/src/wasm/src/dispatcher.rs @@ -4,11 +4,16 @@ use msfs::{commbus::*, network::NetworkRequestState, sys::sGaugeDrawData, MSFSEv use navigation_database::database::Database; use crate::{ - consts, download::downloader::{DownloadStatus, NavigationDataDownloader}, json_structs::{ + consts, + download::downloader::{DownloadStatus, NavigationDataDownloader}, + json_structs::{ events, functions::{CallFunction, FunctionResult, FunctionStatus, FunctionType}, params, - }, meta, network_helper::NetworkHelper, util::{self, path_exists} + }, + meta, + network_helper::NetworkHelper, + util::{self, path_exists}, }; #[derive(PartialEq, Eq)] @@ -405,7 +410,10 @@ impl<'a> Dispatcher<'a> { } // Network request tasks - for task in queue.iter().filter(|task| task.borrow().status == TaskStatus::InProgress) { + for task in queue + .iter() + .filter(|task| task.borrow().status == TaskStatus::InProgress) + { let response_state = match task.borrow().associated_network_request { Some(ref request) => request.response_state(), None => continue, diff --git a/src/wasm/src/meta.rs b/src/wasm/src/meta.rs index 36540786..9cc22712 100644 --- a/src/wasm/src/meta.rs +++ b/src/wasm/src/meta.rs @@ -1,173 +1,173 @@ -use std::{ - cell::RefCell, - path::{Path, PathBuf}, - rc::Rc, -}; - -use msfs::network::NetworkRequestState; - -use crate::{ - consts, - dispatcher::{Task, TaskStatus}, - network_helper::{Method, NetworkHelper}, - util::path_exists, -}; - -#[derive(serde::Serialize, Debug)] -pub enum InstallStatus { - Bundled, - Manual, - None, -} - -#[derive(serde::Serialize, Debug)] -pub struct NavigationDataStatus { - pub status: InstallStatus, - #[serde(rename = "installedFormat")] - pub installed_format: Option, - #[serde(rename = "installedRevision")] - pub installed_revision: Option, - #[serde(rename = "installedCycle")] - pub installed_cycle: Option, - #[serde(rename = "validityPeriod")] - pub validity_period: Option, - #[serde(rename = "latestCycle")] - pub latest_cycle: String, -} - -#[derive(serde::Deserialize)] -pub struct CurrentCycleResponse { - pub name: String, - pub version: String, - pub configuration: String, - pub cycle: String, -} - -#[derive(serde::Deserialize)] -pub struct InstalledNavigationDataCycleInfo { - pub cycle: String, - pub revision: String, - pub name: String, - pub format: String, - #[serde(rename = "validityPeriod")] - pub validity_period: String, -} - -pub fn start_network_request(task: Rc>) { - let request = NetworkHelper::make_request("https://navdata.api.navigraph.com/info", Method::Get, None, None); - let request = match request { - Ok(request) => request, - Err(e) => { - task.borrow_mut().status = TaskStatus::Failure(e.to_string()); - return; - }, - }; - task.borrow_mut().associated_network_request = Some(request); -} - -pub fn get_navigation_data_install_status(task: Rc>) { - let response_bytes = match task.borrow().associated_network_request.as_ref() { - Some(request) => { - if request.response_state() == NetworkRequestState::DataReady { - let response = request.get_response(); - match response { - Ok(response) => response, - Err(e) => { - task.borrow_mut().status = TaskStatus::Failure(e.to_string()); - return; - }, - } - } else { - return; - } - }, - None => { - task.borrow_mut().status = TaskStatus::Failure("No associated network request".to_string()); - return; - }, - }; - - let response_struct: CurrentCycleResponse = match serde_json::from_slice(&response_bytes) { - Ok(response_struct) => response_struct, - Err(e) => { - task.borrow_mut().status = TaskStatus::Failure(e.to_string()); - return; - }, - }; - - // figure out install status - let found_downloaded = path_exists(Path::new(consts::NAVIGATION_DATA_DOWNLOADED_LOCATION)); - - let found_bundled = path_exists(Path::new(consts::NAVIGATION_DATA_DEFAULT_LOCATION)); - - let status = if found_downloaded { - InstallStatus::Manual - } else if found_bundled { - InstallStatus::Bundled - } else { - InstallStatus::None - }; - - // Open JSON - let json_path = match status { - InstallStatus::Manual => Some(PathBuf::from(consts::NAVIGATION_DATA_DOWNLOADED_LOCATION).join("cycle.json")), - InstallStatus::Bundled => Some(PathBuf::from(consts::NAVIGATION_DATA_DEFAULT_LOCATION).join("cycle.json")), - InstallStatus::None => None, - }; - - let installed_cycle_info = match json_path { - Some(json_path) => { - let json_file = match std::fs::File::open(json_path) { - Ok(json_file) => json_file, - Err(e) => { - task.borrow_mut().status = TaskStatus::Failure(e.to_string()); - return; - }, - }; - - let installed_cycle_info: InstalledNavigationDataCycleInfo = match serde_json::from_reader(json_file) { - Ok(installed_cycle_info) => installed_cycle_info, - Err(e) => { - task.borrow_mut().status = TaskStatus::Failure(e.to_string()); - return; - }, - }; - - Some(installed_cycle_info) - }, - None => None, - }; - - let status = NavigationDataStatus { - status, - installed_format: match &installed_cycle_info { - Some(installed_cycle_info) => Some(installed_cycle_info.format.clone()), - None => None, - }, - installed_revision: match &installed_cycle_info { - Some(installed_cycle_info) => Some(installed_cycle_info.revision.clone()), - None => None, - }, - installed_cycle: match &installed_cycle_info { - Some(installed_cycle_info) => Some(installed_cycle_info.cycle.clone()), - None => None, - }, - validity_period: match &installed_cycle_info { - Some(installed_cycle_info) => Some(installed_cycle_info.validity_period.clone()), - None => None, - }, - latest_cycle: response_struct.cycle, - }; - - let status_as_value = match serde_json::to_value(&status) { - Ok(status_as_value) => status_as_value, - Err(e) => { - task.borrow_mut().status = TaskStatus::Failure(e.to_string()); - return; - }, - }; - - println!("Status: {:#?}", status); - - task.borrow_mut().status = TaskStatus::Success(Some(status_as_value)); -} +use std::{ + cell::RefCell, + path::{Path, PathBuf}, + rc::Rc, +}; + +use msfs::network::NetworkRequestState; + +use crate::{ + consts, + dispatcher::{Task, TaskStatus}, + network_helper::{Method, NetworkHelper}, + util::path_exists, +}; + +#[derive(serde::Serialize, Debug)] +pub enum InstallStatus { + Bundled, + Manual, + None, +} + +#[derive(serde::Serialize, Debug)] +pub struct NavigationDataStatus { + pub status: InstallStatus, + #[serde(rename = "installedFormat")] + pub installed_format: Option, + #[serde(rename = "installedRevision")] + pub installed_revision: Option, + #[serde(rename = "installedCycle")] + pub installed_cycle: Option, + #[serde(rename = "validityPeriod")] + pub validity_period: Option, + #[serde(rename = "latestCycle")] + pub latest_cycle: String, +} + +#[derive(serde::Deserialize)] +pub struct CurrentCycleResponse { + pub name: String, + pub version: String, + pub configuration: String, + pub cycle: String, +} + +#[derive(serde::Deserialize)] +pub struct InstalledNavigationDataCycleInfo { + pub cycle: String, + pub revision: String, + pub name: String, + pub format: String, + #[serde(rename = "validityPeriod")] + pub validity_period: String, +} + +pub fn start_network_request(task: Rc>) { + let request = NetworkHelper::make_request("https://navdata.api.navigraph.com/info", Method::Get, None, None); + let request = match request { + Ok(request) => request, + Err(e) => { + task.borrow_mut().status = TaskStatus::Failure(e.to_string()); + return; + }, + }; + task.borrow_mut().associated_network_request = Some(request); +} + +pub fn get_navigation_data_install_status(task: Rc>) { + let response_bytes = match task.borrow().associated_network_request.as_ref() { + Some(request) => { + if request.response_state() == NetworkRequestState::DataReady { + let response = request.get_response(); + match response { + Ok(response) => response, + Err(e) => { + task.borrow_mut().status = TaskStatus::Failure(e.to_string()); + return; + }, + } + } else { + return; + } + }, + None => { + task.borrow_mut().status = TaskStatus::Failure("No associated network request".to_string()); + return; + }, + }; + + let response_struct: CurrentCycleResponse = match serde_json::from_slice(&response_bytes) { + Ok(response_struct) => response_struct, + Err(e) => { + task.borrow_mut().status = TaskStatus::Failure(e.to_string()); + return; + }, + }; + + // figure out install status + let found_downloaded = path_exists(Path::new(consts::NAVIGATION_DATA_DOWNLOADED_LOCATION)); + + let found_bundled = path_exists(Path::new(consts::NAVIGATION_DATA_DEFAULT_LOCATION)); + + let status = if found_downloaded { + InstallStatus::Manual + } else if found_bundled { + InstallStatus::Bundled + } else { + InstallStatus::None + }; + + // Open JSON + let json_path = match status { + InstallStatus::Manual => Some(PathBuf::from(consts::NAVIGATION_DATA_DOWNLOADED_LOCATION).join("cycle.json")), + InstallStatus::Bundled => Some(PathBuf::from(consts::NAVIGATION_DATA_DEFAULT_LOCATION).join("cycle.json")), + InstallStatus::None => None, + }; + + let installed_cycle_info = match json_path { + Some(json_path) => { + let json_file = match std::fs::File::open(json_path) { + Ok(json_file) => json_file, + Err(e) => { + task.borrow_mut().status = TaskStatus::Failure(e.to_string()); + return; + }, + }; + + let installed_cycle_info: InstalledNavigationDataCycleInfo = match serde_json::from_reader(json_file) { + Ok(installed_cycle_info) => installed_cycle_info, + Err(e) => { + task.borrow_mut().status = TaskStatus::Failure(e.to_string()); + return; + }, + }; + + Some(installed_cycle_info) + }, + None => None, + }; + + let status = NavigationDataStatus { + status, + installed_format: match &installed_cycle_info { + Some(installed_cycle_info) => Some(installed_cycle_info.format.clone()), + None => None, + }, + installed_revision: match &installed_cycle_info { + Some(installed_cycle_info) => Some(installed_cycle_info.revision.clone()), + None => None, + }, + installed_cycle: match &installed_cycle_info { + Some(installed_cycle_info) => Some(installed_cycle_info.cycle.clone()), + None => None, + }, + validity_period: match &installed_cycle_info { + Some(installed_cycle_info) => Some(installed_cycle_info.validity_period.clone()), + None => None, + }, + latest_cycle: response_struct.cycle, + }; + + let status_as_value = match serde_json::to_value(&status) { + Ok(status_as_value) => status_as_value, + Err(e) => { + task.borrow_mut().status = TaskStatus::Failure(e.to_string()); + return; + }, + }; + + println!("Status: {:#?}", status); + + task.borrow_mut().status = TaskStatus::Success(Some(status_as_value)); +} diff --git a/src/wasm/src/network_helper.rs b/src/wasm/src/network_helper.rs index 9468093c..a9005491 100644 --- a/src/wasm/src/network_helper.rs +++ b/src/wasm/src/network_helper.rs @@ -1,54 +1,54 @@ -use std::error::Error; - -use msfs::network::{NetworkRequest, NetworkRequestBuilder, NetworkRequestState}; - -pub enum Method { - Get, -} - -pub struct NetworkHelper { - request: NetworkRequest, -} - -impl NetworkHelper { - pub fn make_request( - url: &str, method: Method, headers: Option>, data: Option<&mut [u8]>, - ) -> Result> { - let mut builder = NetworkRequestBuilder::new(url).ok_or("Failed to create NetworkRequestBuilder")?; - - // Add headers - if let Some(headers) = headers { - for header in headers { - let new_builder = builder.with_header(header).ok_or("Failed to add header")?; - builder = new_builder; - } - } - - // Add data - if let Some(data) = data { - let new_builder = builder.with_data(data); - builder = new_builder; - } - - // Send request - let request = match method { - Method::Get => builder.get().ok_or("Failed to send GET request")?, - }; - - Ok(Self { request }) - } - - pub fn response_state(&self) -> NetworkRequestState { - self.request.state() - } - - pub fn get_response(&self) -> Result, Box> { - if self.request.state() != NetworkRequestState::DataReady { - return Err("Request not finished yet".into()); - } - - let data = self.request.data().ok_or("Failed to get data")?; - - Ok(data) - } -} +use std::error::Error; + +use msfs::network::{NetworkRequest, NetworkRequestBuilder, NetworkRequestState}; + +pub enum Method { + Get, +} + +pub struct NetworkHelper { + request: NetworkRequest, +} + +impl NetworkHelper { + pub fn make_request( + url: &str, method: Method, headers: Option>, data: Option<&mut [u8]>, + ) -> Result> { + let mut builder = NetworkRequestBuilder::new(url).ok_or("Failed to create NetworkRequestBuilder")?; + + // Add headers + if let Some(headers) = headers { + for header in headers { + let new_builder = builder.with_header(header).ok_or("Failed to add header")?; + builder = new_builder; + } + } + + // Add data + if let Some(data) = data { + let new_builder = builder.with_data(data); + builder = new_builder; + } + + // Send request + let request = match method { + Method::Get => builder.get().ok_or("Failed to send GET request")?, + }; + + Ok(Self { request }) + } + + pub fn response_state(&self) -> NetworkRequestState { + self.request.state() + } + + pub fn get_response(&self) -> Result, Box> { + if self.request.state() != NetworkRequestState::DataReady { + return Err("Request not finished yet".into()); + } + + let data = self.request.data().ok_or("Failed to get data")?; + + Ok(data) + } +}