Skip to content

Commit

Permalink
Remove Errors For .nodes (#2)
Browse files Browse the repository at this point in the history
When calling `.nodes <file>` 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.
  • Loading branch information
esarver authored Apr 2, 2024
1 parent 41931a8 commit 28ea880
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
7 changes: 6 additions & 1 deletion instrument-repl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
48 changes: 32 additions & 16 deletions instrument-repl/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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 => {}
Expand Down Expand Up @@ -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())?;
Expand Down Expand Up @@ -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 }
}
},
Expand Down
4 changes: 2 additions & 2 deletions instrument-repl/src/resources/TspLinkNodeDetails.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions kic-discover/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ async fn init_rpc() -> anyhow::Result<ServerHandle> {

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)]
Expand Down

0 comments on commit 28ea880

Please sign in to comment.