move common tools from nu-command to nu-cmd-base (#9455)

related to 
- https://github.com/nushell/nushell/pull/9404

# Description
to support our cratification effort and moving non-1.0 commands outside
of the main focus, this PR
- creates a new `nu-cmd-base` crate to hold the common structs, traits
and functions used by all command-related crates
- to start the transition, moves the `input_handler` module from
`nu-command` to `nu-cmd-base`

# User-Facing Changes
```
$nothing
```

# Tests + Formatting
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
-  `toolkit test`
-  `toolkit test stdlib`

# After Submitting
```
$nothing
```
This commit is contained in:
Antoine Stevan
2023-06-22 23:45:54 +02:00
committed by GitHub
parent 64319ad90f
commit 78697bb8cf
42 changed files with 60 additions and 39 deletions

View File

@ -95,6 +95,7 @@ url = "2.2"
uuid = { version = "1.3", features = ["v4"] }
wax = { version = "0.5" }
which = { version = "4.4", optional = true }
nu-cmd-base = { version = "0.81.1", path = "../nu-cmd-base" }
[target.'cfg(windows)'.dependencies]
winreg = "0.50"

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CmdArgument};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;

View File

@ -1,7 +1,5 @@
use crate::{
input_handler::{operate, CmdArgument},
util,
};
use crate::util;
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CmdArgument};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CmdArgument};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{Command, EngineState, Stack};

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CellPathOnlyArgs};
use nu_cmd_base::input_handler::{operate, CellPathOnlyArgs};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CmdArgument};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CmdArgument};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CellPathOnlyArgs};
use nu_cmd_base::input_handler::{operate, CellPathOnlyArgs};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CmdArgument};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CmdArgument};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CellPathOnlyArgs};
use nu_cmd_base::input_handler::{operate, CellPathOnlyArgs};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CellPathOnlyArgs};
use nu_cmd_base::input_handler::{operate, CellPathOnlyArgs};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CellPathOnlyArgs};
use nu_cmd_base::input_handler::{operate, CellPathOnlyArgs};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},

View File

@ -1,6 +1,6 @@
use crate::input_handler::{operate, CmdArgument};
use crate::{generate_strftime_list, parse_date_from_string};
use chrono::{DateTime, FixedOffset, Local, TimeZone, Utc};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CellPathOnlyArgs};
use nu_cmd_base::input_handler::{operate, CellPathOnlyArgs};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CellPathOnlyArgs};
use nu_cmd_base::input_handler::{operate, CellPathOnlyArgs};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},

View File

@ -1,6 +1,6 @@
use chrono::{FixedOffset, TimeZone};
use crate::input_handler::{operate, CmdArgument};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CmdArgument};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CmdArgument};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{Command, EngineState, Stack};

View File

@ -1,90 +0,0 @@
use nu_protocol::ast::CellPath;
use nu_protocol::{PipelineData, ShellError, Span, Value};
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
pub trait CmdArgument {
fn take_cell_paths(&mut self) -> Option<Vec<CellPath>>;
}
/// Arguments with only cell_path.
///
/// If commands is going to use `operate` function, and it only required optional cell_paths
/// Using this to simplify code.
pub struct CellPathOnlyArgs {
cell_paths: Option<Vec<CellPath>>,
}
impl CmdArgument for CellPathOnlyArgs {
fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
self.cell_paths.take()
}
}
impl From<Vec<CellPath>> for CellPathOnlyArgs {
fn from(cell_paths: Vec<CellPath>) -> Self {
Self {
cell_paths: (!cell_paths.is_empty()).then_some(cell_paths),
}
}
}
/// A simple wrapper for `PipelineData::map` method.
///
/// In detail, for each elements, invoking relative `cmd` with `arg`.
///
/// If `arg` tell us that its cell path is not None, only map over data under these columns.
/// Else it will apply each column inside a table.
///
/// The validation of input element should be handle by `cmd` itself.
pub fn operate<C, A>(
cmd: C,
mut arg: A,
input: PipelineData,
span: Span,
ctrlc: Option<Arc<AtomicBool>>,
) -> Result<PipelineData, ShellError>
where
A: CmdArgument + Send + Sync + 'static,
C: Fn(&Value, &A, Span) -> Value + Send + Sync + 'static + Clone + Copy,
{
match arg.take_cell_paths() {
None => input.map(
move |v| {
match v {
// Propagate errors inside the input
Value::Error { .. } => v,
_ => cmd(&v, &arg, span),
}
},
ctrlc,
),
Some(column_paths) => {
let arg = Arc::new(arg);
input.map(
move |mut v| {
for path in &column_paths {
let opt = arg.clone();
let r = v.update_cell_path(
&path.members,
Box::new(move |old| {
match old {
// Propagate errors inside the input
Value::Error { .. } => old.clone(),
_ => cmd(old, &opt, span),
}
}),
);
if let Err(error) = r {
return Value::Error {
error: Box::new(error),
};
}
}
v
},
ctrlc,
)
}
}
}

View File

@ -15,7 +15,6 @@ mod generators;
mod hash;
mod help;
pub mod hook;
mod input_handler;
mod math;
mod misc;
mod network;

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CellPathOnlyArgs};
use nu_cmd_base::input_handler::{operate, CellPathOnlyArgs};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CellPathOnlyArgs};
use nu_cmd_base::input_handler::{operate, CellPathOnlyArgs};
use nu_engine::CallExt;
use nu_protocol::{
ast::Call, ast::CellPath, engine::Command, engine::EngineState, engine::Stack, Category,

View File

@ -1,8 +1,8 @@
use crate::input_handler::{operate as general_operate, CmdArgument};
use base64::{
alphabet, engine::general_purpose::NO_PAD, engine::general_purpose::PAD,
engine::GeneralPurpose, Engine,
};
use nu_cmd_base::input_handler::{operate as general_operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{EngineState, Stack};

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate as general_operate, CmdArgument};
use nu_cmd_base::input_handler::{operate as general_operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{EngineState, Stack};

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CmdArgument};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{Command, EngineState, Stack};

View File

@ -22,7 +22,7 @@ pub use upcase::SubCommand as StrUpcase;
use nu_engine::CallExt;
use crate::input_handler::{operate as general_operate, CmdArgument};
use nu_cmd_base::input_handler::{operate as general_operate, CmdArgument};
use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::{PipelineData, ShellError, Span, Value};

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CmdArgument};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CmdArgument};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CmdArgument};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;

View File

@ -1,5 +1,5 @@
use crate::input_handler::{operate, CmdArgument};
use crate::{grapheme_flags, util};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},

View File

@ -1,5 +1,5 @@
use crate::grapheme_flags;
use crate::input_handler::{operate, CmdArgument};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;

View File

@ -1,5 +1,5 @@
use crate::input_handler::{operate, CmdArgument};
use fancy_regex::{NoExpand, Regex};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CellPathOnlyArgs};
use nu_cmd_base::input_handler::{operate, CellPathOnlyArgs};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CmdArgument};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;

View File

@ -1,5 +1,5 @@
use crate::input_handler::{operate, CmdArgument};
use crate::{grapheme_flags, util};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;

View File

@ -1,4 +1,4 @@
use crate::input_handler::{operate, CmdArgument};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},