From 28ea880d9d60831f1b9486bb4d05afd6e763f9c9 Mon Sep 17 00:00:00 2001 From: esarver Date: Tue, 2 Apr 2024 10:08:50 -0400 Subject: [PATCH] Remove Errors For `.nodes` (#2) When calling `.nodes ` there were a few unnecessary error cases: 1. If there were no TSP-Link nodes to initialize, an error would be added to the errorqueue - This was fixed by using `tsplink.reset(1)` and `tsplink.initialize(0)` instead of the no-parameter overloads 2. If the given file didn't exist, an error was thrown even though we can create it. - This was fixed by removing the check for file existence. This PR also includes an incidental fix for a clippy warning. We were calling `enumerate` unnecessarily. --- instrument-repl/src/lib.rs | 7 ++- instrument-repl/src/repl.rs | 48 ++++++++++++------- .../src/resources/TspLinkNodeDetails.tsp | 4 +- kic-discover/src/main.rs | 3 +- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/instrument-repl/src/lib.rs b/instrument-repl/src/lib.rs index b6e6208..f6ef8ce 100644 --- a/instrument-repl/src/lib.rs +++ b/instrument-repl/src/lib.rs @@ -1,4 +1,9 @@ -#![feature(lint_reasons, rustdoc_missing_doc_code_examples, stmt_expr_attributes)] +#![feature( + lint_reasons, + rustdoc_missing_doc_code_examples, + stmt_expr_attributes, + io_error_more +)] #![deny( clippy::undocumented_unsafe_blocks, clippy::pedantic, diff --git a/instrument-repl/src/repl.rs b/instrument-repl/src/repl.rs index ce5c851..f46fb4a 100644 --- a/instrument-repl/src/repl.rs +++ b/instrument-repl/src/repl.rs @@ -12,7 +12,7 @@ use colored::Colorize; use regex::Regex; use std::{ fmt::Display, - fs::File, + fs::{self, File}, io::{self, Read, Write}, path::PathBuf, sync::mpsc::{channel, SendError, Sender, TryRecvError}, @@ -121,7 +121,7 @@ impl Repl { Action::PrintText => Self::print_data(*state, response)?, Action::PrintError => Self::print_data(*state, response)?, Action::GetNodeDetails => { - Self::update_node_config_json(self.lang_cong_file_path.clone(), response)?; + Self::update_node_config_json(&self.lang_cong_file_path, &response); } Action::None => {} @@ -350,16 +350,41 @@ impl Repl { } } - fn update_node_config_json(file_path: String, resp: ParsedResponse) -> Result<()> { - match resp { - ParsedResponse::Data(d) => { - Self::write_json_data(file_path, String::from_utf8_lossy(&d).as_ref()) + fn update_node_config_json(file_path: &str, resp: &ParsedResponse) { + if let ParsedResponse::Data(d) = &resp { + if let Err(e) = + Self::write_json_data(file_path.to_string(), String::from_utf8_lossy(d).as_ref()) + { + eprintln!("Unable to write configuration: {e}"); } - _ => Ok(()), } } fn write_json_data(file_path: String, input_line: &str) -> Result<()> { + let path = PathBuf::from(file_path.clone()); + let Some(path) = path.parent() else { + return Err(InstrumentReplError::IOError { + source: std::io::Error::new( + io::ErrorKind::InvalidInput, + "given path did not have a containing folder", + ), + }); + }; + + if path.is_file() { + return Err(InstrumentReplError::IOError { + source: std::io::Error::new( + io::ErrorKind::NotADirectory, + "the parent folder is already a file", + ), + }); + } + + // If the path doesn't already exist, recursively create it. + if !path.is_dir() { + fs::create_dir_all(path)?; + } + if let Ok(mut file) = File::create(file_path) { // Convert the Lua string to JSON let json_value: serde_json::Value = serde_json::from_str(input_line.trim())?; @@ -552,15 +577,6 @@ impl Repl { }; let json_file = PathBuf::from(file.clone()); - if !json_file.is_file() { - return Ok(Request::Usage( - InstrumentReplError::Other(format!( - "unable to find file \"{}\"", - json_file.to_string_lossy() - )) - .to_string(), - )); - } Request::TspLinkNodes { json_file } } }, diff --git a/instrument-repl/src/resources/TspLinkNodeDetails.tsp b/instrument-repl/src/resources/TspLinkNodeDetails.tsp index b1d5ffb..df9c4a7 100644 --- a/instrument-repl/src/resources/TspLinkNodeDetails.tsp +++ b/instrument-repl/src/resources/TspLinkNodeDetails.tsp @@ -15,9 +15,9 @@ end if tsplink.initialize == nil then - tsplink.reset() + tsplink.reset(1) else - tsplink.initialize() + tsplink.initialize(1) end if (tsplink.state == "online") then diff --git a/kic-discover/src/main.rs b/kic-discover/src/main.rs index 02199c2..683341e 100644 --- a/kic-discover/src/main.rs +++ b/kic-discover/src/main.rs @@ -135,8 +135,7 @@ async fn init_rpc() -> anyhow::Result { if let Ok(db) = DISC_INSTRUMENTS.lock() { db.iter() - .enumerate() - .for_each(|(_i, item)| new_out_str = format!("{new_out_str}{item}\n")); + .for_each(|item| new_out_str = format!("{new_out_str}{item}\n")); }; #[cfg(debug_assertions)]