Elnu's code check

This commit is contained in:
Nickiel12 2024-04-06 20:53:36 -07:00
parent 8d532ffd82
commit cf6528285c
6 changed files with 79 additions and 49 deletions

39
Cargo.lock generated
View file

@ -323,6 +323,20 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "err-derive"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c34a887c8df3ed90498c1c437ce21f211c8e27672921a8ffa293cb8d6d4caa9e"
dependencies = [
"proc-macro-error",
"proc-macro2",
"quote",
"rustversion",
"syn 1.0.109",
"synstructure",
]
[[package]]
name = "event-listener"
version = "5.2.0"
@ -871,6 +885,7 @@ version = "1.1.0"
dependencies = [
"async-channel",
"config",
"err-derive",
"futures-core",
"futures-util",
"gilrs",
@ -1294,6 +1309,12 @@ dependencies = [
"semver",
]
[[package]]
name = "rustversion"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80af6f9131f277a45a3fba6ce8e2258037bb0477a67e610d3c1fe046ab31de47"
[[package]]
name = "ryu"
version = "1.0.17"
@ -1426,6 +1447,18 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "synstructure"
version = "0.12.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
"unicode-xid",
]
[[package]]
name = "system-deps"
version = "6.2.1"
@ -1673,6 +1706,12 @@ version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202"
[[package]]
name = "unicode-xid"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
[[package]]
name = "url"
version = "2.5.0"

View file

@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
async-channel = "2.2.0"
config = "0.14.0"
err-derive = "0.3.1"
futures-core = "0.3.30"
futures-util = { version = "0.3.30", features = ["tokio-io"] }
gilrs = "0.10.6"

View file

@ -1,4 +1,5 @@
use config::{Config, FileFormat};
use err_derive::Error;
use log::{error, info};
use std::fs::File;
use std::io::Write;
@ -10,34 +11,23 @@ pub fn load_config() -> AppState {
.add_source(config::File::new("./settings.toml", FileFormat::Toml))
.build();
if let Ok(val) = settings {
if let Ok(state) = val.try_deserialize() {
state
} else {
AppState::default()
}
} else {
AppState::default()
}
settings
.and_then(|val| val.try_deserialize())
.unwrap_or_default()
}
pub fn save_config(config: &AppState) {
match toml::to_string(&config) {
Ok(toml_str) => match File::create("./settings.toml") {
Ok(mut file) => match file.write_all(toml_str.as_bytes()) {
Ok(_) => {
info!("Config file saved succesfully");
}
Err(e) => {
error!("Couldn't write config file contents to open file: {e}");
}
},
Err(e) => {
error!("Couldn't open settings file: {e}");
}
},
Err(e) => {
error!("Could not serialize app state: {e}");
}
#[derive(Error, Debug)]
pub enum SaveConfigError {
#[error(display = "Could not serialize app state: {:?}", _0)]
SerdeError(#[cause] toml::ser::Error),
#[error(display = "Could not write app state to file: {:?}", _0)]
IoError(#[cause] std::io::Error),
}
pub fn save_config(config: &AppState) -> Result<(), SaveConfigError> {
let toml_str = toml::to_string(&config)?;
let mut file = File::create("./settings.toml")?;
file.write_all(toml_str.as_bytes())?;
info!("Config file saved successfully");
Ok(())
}

View file

@ -1,7 +1,7 @@
use gtk::gdk::Display;
use gtk::{glib, Application};
use gtk::{prelude::*, CssProvider};
use log::info;
use log::{error, info};
use simplelog::SimpleLogger;
use std::env;
use tokio::runtime;
@ -16,11 +16,8 @@ const APP_ID: &str = "net.nickiel.joystick-controller-client";
fn main() -> glib::ExitCode {
env::set_var("gtk_csd", "0");
match SimpleLogger::init(simplelog::LevelFilter::Debug, simplelog::Config::default()) {
Ok(_) => {}
Err(e) => {
println!("Failed to init the simplelogger!: {}", e);
}
if let Err(e) = SimpleLogger::init(simplelog::LevelFilter::Debug, simplelog::Config::default()) {
error!("Failed to init the simplelogger!: {e}");
}
let rt = runtime::Runtime::new().expect("Could not start tokio runtime");

View file

@ -54,14 +54,14 @@ async fn socket_write(
}
Err(e) => {
// Channel closed, exit loop
error!("To Socket channel is closed! Exiting: {}", e);
error!("To Socket channel is closed! Exiting: {e}");
break;
}
}
}
socket_send_is_alive.store(false, Ordering::SeqCst);
if let Err(e) = sender.close().await {
error!("Error closing websocket sender: {}", e);
error!("Error closing websocket sender: {e}");
}
info!("Closed socket writing thread");
}

View file

@ -92,20 +92,23 @@ pub fn build_ui(app: &Application, runtime: Handle) {
let ip_text = ip_entry.text();
let port_text = port_entry.text();
if ip_text.len() > 0 {
if ip_text.len() == 0 {
return;
}
if let Ok(val) = port_text.parse::<u32>() {
save_config(&AppState {
if let Err(error) = save_config(&AppState {
ip: ip_text.to_string(),
port: val
});
}) {
error!("{error}");
};
match to_mec.try_send(ApplicationEvent::StartSocket(
format!("ws://{}:{}", ip_text, val),
)) {
Ok(_) => { }
Err(async_channel::TrySendError::Closed(_)) => {panic!("Coordinator MEC is closed. Unrecoverable error.")}
Err(e) => {error!("There was an error sending to the MEC: {}", e)}
}
Ok(_) => {},
Err(async_channel::TrySendError::Closed(_)) => panic!("Coordinator MEC is closed. Unrecoverable error."),
Err(e) => error!("There was an error sending to the MEC: {}", e),
}
}
}));