Skip to content

Commit

Permalink
feat: #22 ✨ Fix errors reported by linter (/clippy)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddanier committed May 28, 2024
1 parent 4c2fcb0 commit e521169
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 69 deletions.
10 changes: 2 additions & 8 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use nu_parser::{escape_for_script_arg, escape_quote_string};
use nu_protocol::ast::Expression;
use nu_protocol::{
ast::Expr,
engine::{Command, EngineState, Stack, StateWorkingSet},
engine::{EngineState, Stack, StateWorkingSet},
ShellError,
};
use nu_protocol::{report_error, Spanned};
Expand Down Expand Up @@ -163,13 +163,7 @@ pub(crate) fn parse_commandline_args(
}

// Just give the help and exit if the above fails
let full_help = get_full_help(
&Nur.signature(),
&Nur.examples(),
engine_state,
&mut stack,
true,
);
let full_help = get_full_help(&Nur, engine_state, &mut stack);
print!("{full_help}");
std::process::exit(1);
}
Expand Down
6 changes: 1 addition & 5 deletions src/commands/nur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ impl Command for Nur {
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
Ok(Value::string(
get_full_help(&Nur.signature(), &Nur.examples(), engine_state, stack, true),
call.head,
)
.into_pipeline_data())
Ok(Value::string(get_full_help(&Nur, engine_state, stack), call.head).into_pipeline_data())
}

fn examples(&self) -> Vec<Example> {
Expand Down
56 changes: 19 additions & 37 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ use nu_cli::{evaluate_repl, gather_parent_env_vars};
use nu_engine::get_full_help;
use nu_protocol::ast::Block;
use nu_protocol::engine::{Command, Stack, StateWorkingSet};
use nu_protocol::eval_const::create_nu_constant;
use nu_protocol::{
engine::EngineState, report_error, report_error_new, PipelineData, Record, Span, Type, Value,
NU_VARIABLE_ID,
};
use nu_std::load_standard_library;
use nu_utils::stdout_write_all_and_flush;
Expand Down Expand Up @@ -101,13 +99,9 @@ impl NurEngine {
self.engine_state
.set_config_path("config-path", self.state.config_path.clone());

// Set up the $nu constant before evaluating any files (need to have $nu available in them)
let nu_const = create_nu_constant(
&self.engine_state,
PipelineData::empty().span().unwrap_or_else(Span::unknown),
)?;
self.engine_state
.set_variable_const_val(NU_VARIABLE_ID, nu_const);
// Set up the $nu constant before evaluating any files
// (those may need to have $nu available to execute them)
self.engine_state.generate_nu_constant();

// Set up the $nur constant record (like $nu)
let mut nur_record = Record::new();
Expand Down Expand Up @@ -305,7 +299,7 @@ impl NurEngine {
input: PipelineData,
print: bool,
merge_env: bool,
) -> NurResult<i64> {
) -> NurResult<i32> {
let str_contents = contents.to_string();

if str_contents.is_empty() {
Expand All @@ -318,7 +312,7 @@ impl NurEngine {

// Merge env is requested
if merge_env {
match nu_engine::env::current_dir(&self.engine_state, &self.stack) {
match self.engine_state.cwd(Some(&self.stack)) {
Ok(cwd) => {
if let Err(e) = self.engine_state.merge_env(&mut self.stack, cwd) {
let working_set = StateWorkingSet::new(&self.engine_state);
Expand All @@ -334,53 +328,47 @@ impl NurEngine {

// Print result is requested
if print {
let exit_code = result.print(&self.engine_state, &mut self.stack, false, false)?;
Ok(exit_code)
let exit_details = result.print(&self.engine_state, &mut self.stack, false, false)?;
match exit_details {
Some(exit_status) => Ok(exit_status.code()),
None => Ok(0),
}
} else {
if let PipelineData::ExternalStream {
exit_code: Some(exit_code),
..
} = result
{
let mut exit_codes: Vec<_> = exit_code.into_iter().collect();
return match exit_codes.pop() {
#[cfg(unix)]
Some(Value::Error { error, .. }) => Err(NurError::from(*error)),
Some(Value::Int { val, .. }) => Ok(val),
_ => Ok(0),
};
if let Some(exit_status) = result.drain()? {
return Ok(exit_status.code());
}

Ok(0)
}
}

// This is used in tests only currently
#[allow(dead_code)]
pub fn eval<S: ToString>(&mut self, contents: S, input: PipelineData) -> NurResult<i64> {
pub fn eval<S: ToString>(&mut self, contents: S, input: PipelineData) -> NurResult<i32> {
self._eval(None, contents, input, false, false)
}

pub(crate) fn eval_and_print<S: ToString>(
&mut self,
contents: S,
input: PipelineData,
) -> NurResult<i64> {
) -> NurResult<i32> {
self._eval(None, contents, input, true, false)
}

pub(crate) fn eval_and_merge_env<S: ToString>(
&mut self,
contents: S,
input: PipelineData,
) -> NurResult<i64> {
) -> NurResult<i32> {
self._eval(None, contents, input, false, true)
}

pub(crate) fn source<P: AsRef<Path>>(
&mut self,
file_path: P,
input: PipelineData,
) -> NurResult<i64> {
) -> NurResult<i32> {
let contents = fs::read_to_string(&file_path)?;

self._eval(file_path.as_ref().to_str(), contents, input, false, false)
Expand All @@ -390,7 +378,7 @@ impl NurEngine {
&mut self,
file_path: P,
input: PipelineData,
) -> NurResult<i64> {
) -> NurResult<i32> {
let contents = fs::read_to_string(&file_path)?;

self._eval(file_path.as_ref().to_str(), contents, input, false, true)
Expand All @@ -411,13 +399,7 @@ impl NurEngine {
}

pub(crate) fn print_help(&mut self, command: &dyn Command) {
let full_help = get_full_help(
&command.signature(),
&command.examples(),
&self.engine_state,
&mut self.stack,
true,
);
let full_help = get_full_help(command, &self.engine_state, &mut self.stack);

let _ = std::panic::catch_unwind(move || stdout_write_all_and_flush(full_help));
}
Expand Down
22 changes: 3 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ use crate::state::NurState;
use miette::Result;
use nu_ansi_term::Color;
use nu_cmd_base::util::get_init_cwd;
use nu_protocol::{BufferedReader, PipelineData, RawStream, Span};
use nu_protocol::{ByteStream, PipelineData, Span};
use std::env;
use std::io::BufReader;
use std::process::ExitCode;

fn main() -> Result<ExitCode, miette::ErrReport> {
Expand Down Expand Up @@ -162,28 +161,13 @@ fn main() -> Result<ExitCode, miette::ErrReport> {

// Prepare input data - if requested
let input = if parsed_nur_args.attach_stdin {
let stdin = std::io::stdin();
let buf_reader = BufReader::new(stdin);

PipelineData::ExternalStream {
stdout: Some(RawStream::new(
Box::new(BufferedReader::new(buf_reader)),
None,
Span::unknown(),
None,
)),
stderr: None,
exit_code: None,
span: Span::unknown(),
metadata: None,
trim_end_newline: false,
}
PipelineData::ByteStream(ByteStream::stdin(Span::unknown())?, None)
} else {
PipelineData::empty()
};

// Execute the task
let exit_code: i64;
let exit_code: i32;
let run_command = if parsed_nur_args.run_commands.is_some() {
parsed_nur_args.run_commands.clone().unwrap().item
} else {
Expand Down

0 comments on commit e521169

Please sign in to comment.