Refactor the CLI code a bit (#12782)

# Description
Refactors the code in `nu-cli`, `main.rs`, `run.rs`, and few others.
Namely, I added `EngineState::generate_nu_constant` function to
eliminate some duplicate code. Otherwise, I changed a bunch of areas to
return errors instead of calling `std::process::exit`.

# User-Facing Changes
Should be none.
This commit is contained in:
Ian Manske
2024-05-09 23:29:27 +00:00
committed by GitHub
parent 1b2e680059
commit 72d3860d05
19 changed files with 146 additions and 305 deletions

View File

@ -6,6 +6,7 @@ use crate::{
CachedFile, Command, CommandType, EnvVars, OverlayFrame, ScopeFrame, Stack, StateDelta,
Variable, Visibility, DEFAULT_OVERLAY_NAME,
},
eval_const::create_nu_constant,
BlockId, Category, Config, DeclId, Example, FileId, HistoryConfig, Module, ModuleId, OverlayId,
ShellError, Signature, Span, Type, Value, VarId, VirtualPathId,
};
@ -753,8 +754,8 @@ impl EngineState {
var.const_val.as_ref()
}
pub fn set_variable_const_val(&mut self, var_id: VarId, val: Value) {
self.vars[var_id].const_val = Some(val);
pub fn generate_nu_constant(&mut self) {
self.vars[NU_VARIABLE_ID].const_val = Some(create_nu_constant(self, Span::unknown()));
}
pub fn get_decl(&self, decl_id: DeclId) -> &dyn Command {

View File

@ -41,7 +41,6 @@ pub fn report_error_new(
error: &(dyn miette::Diagnostic + Send + Sync + 'static),
) {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, error);
}

View File

@ -12,7 +12,7 @@ use std::{
};
/// Create a Value for `$nu`.
pub fn create_nu_constant(engine_state: &EngineState, span: Span) -> Result<Value, ShellError> {
pub(crate) fn create_nu_constant(engine_state: &EngineState, span: Span) -> Value {
fn canonicalize_path(engine_state: &EngineState, path: &Path) -> PathBuf {
#[allow(deprecated)]
let cwd = engine_state.current_work_dir();
@ -200,7 +200,7 @@ pub fn create_nu_constant(engine_state: &EngineState, span: Span) -> Result<Valu
},
);
Ok(Value::record(record, span))
Value::record(record, span)
}
fn eval_const_call(