Add command_prelude module (#12291)

# Description
When implementing a `Command`, one must also import all the types
present in the function signatures for `Command`. This makes it so that
we often import the same set of types in each command implementation
file. E.g., something like this:
```rust
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
    record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
    ShellError, Signature, Span, Type, Value,
};
```

This PR adds the `nu_engine::command_prelude` module which contains the
necessary and commonly used types to implement a `Command`:
```rust
// command_prelude.rs
pub use crate::CallExt;
pub use nu_protocol::{
    ast::{Call, CellPath},
    engine::{Command, EngineState, Stack},
    record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, IntoSpanned,
    PipelineData, Record, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
```

This should reduce the boilerplate needed to implement a command and
also gives us a place to track the breadth of the `Command` API. I tried
to be conservative with what went into the prelude modules, since it
might be hard/annoying to remove items from the prelude in the future.
Let me know if something should be included or excluded.
This commit is contained in:
Ian Manske
2024-03-26 21:17:30 +00:00
committed by GitHub
parent f8c1e3ac61
commit c747ec75c9
660 changed files with 1634 additions and 4332 deletions

View File

@ -1,8 +1,4 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct Alias;

View File

@ -1,6 +1,4 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Type};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct Break;

View File

@ -1,11 +1,5 @@
use nu_engine::{get_eval_block, redirect_env, CallExt};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type,
Value,
};
use nu_engine::{command_prelude::*, get_eval_block, redirect_env};
use nu_protocol::engine::Closure;
#[derive(Clone)]
pub struct Collect;

View File

@ -1,6 +1,4 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct Const;

View File

@ -1,6 +1,4 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Type};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct Continue;

View File

@ -1,8 +1,4 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct Def;

View File

@ -1,9 +1,7 @@
use nu_engine::CallExt;
use nu_engine::command_prelude::*;
use nu_protocol::{
ast::Call,
engine::{Closure, Command, EngineState, Stack, StateWorkingSet},
record, Category, Example, IntoPipelineData, PipelineData, PipelineMetadata, Record,
ShellError, Signature, Type, Value,
engine::{Closure, StateWorkingSet},
PipelineMetadata,
};
#[derive(Clone)]

View File

@ -1,14 +1,7 @@
use nu_engine::{command_prelude::*, get_eval_block_with_early_return, redirect_env};
use nu_protocol::{engine::Closure, IoStream, ListStream, RawStream};
use std::thread;
use nu_engine::{get_eval_block_with_early_return, redirect_env, CallExt};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoSpanned, IoStream, ListStream, PipelineData, RawStream, ShellError,
Signature, Span, SyntaxShape, Type, Value,
};
#[derive(Clone)]
pub struct Do;

View File

@ -1,10 +1,4 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape,
Type, Value,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct Echo;

View File

@ -1,10 +1,5 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, LabeledError, PipelineData, Record, ShellError, Signature, Span,
SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
use nu_protocol::LabeledError;
#[derive(Clone)]
pub struct ErrorMake;

View File

@ -1,9 +1,4 @@
use nu_engine::get_full_help;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
use nu_engine::{command_prelude::*, get_full_help};
#[derive(Clone)]
pub struct ExportCommand;

View File

@ -1,8 +1,4 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct ExportAlias;

View File

@ -1,8 +1,4 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct ExportConst;

View File

@ -1,8 +1,4 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct ExportDef;

View File

@ -1,6 +1,4 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct ExportExtern;

View File

@ -1,8 +1,4 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct ExportModule;

View File

@ -1,8 +1,4 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct ExportUse;

View File

@ -1,6 +1,4 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct Extern;

View File

@ -1,11 +1,5 @@
use nu_engine::{get_eval_block, get_eval_expression, CallExt};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Block, Command, EngineState, Stack};
use nu_protocol::{
record, Category, Example, ListStream, PipelineData, ShellError, Signature, SyntaxShape, Type,
Value,
};
use nu_engine::{command_prelude::*, get_eval_block, get_eval_expression};
use nu_protocol::{engine::Block, ListStream};
#[derive(Clone)]
pub struct For;

View File

@ -1,6 +1,4 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct Hide;

View File

@ -1,10 +1,5 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
did_you_mean, Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape,
Type, Value,
};
use nu_engine::command_prelude::*;
use nu_protocol::did_you_mean;
#[derive(Clone)]
pub struct HideEnv;

View File

@ -1,10 +1,9 @@
use nu_engine::{get_eval_block, get_eval_expression, get_eval_expression_with_input, CallExt};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Block, Command, EngineState, Stack, StateWorkingSet};
use nu_protocol::eval_const::{eval_const_subexpression, eval_constant, eval_constant_with_input};
use nu_engine::{
command_prelude::*, get_eval_block, get_eval_expression, get_eval_expression_with_input,
};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
engine::{Block, StateWorkingSet},
eval_const::{eval_const_subexpression, eval_constant, eval_constant_with_input},
};
#[derive(Clone)]

View File

@ -1,8 +1,5 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack, StateWorkingSet};
use nu_protocol::{
Category, Example, IoStream, PipelineData, ShellError, Signature, Span, Type, Value,
};
use nu_engine::command_prelude::*;
use nu_protocol::{engine::StateWorkingSet, IoStream};
#[derive(Clone)]
pub struct Ignore;

View File

@ -1,14 +1,8 @@
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use nu_engine::{eval_block, CallExt};
use nu_protocol::ast::Call;
use nu_protocol::debugger::WithoutDebug;
use nu_protocol::engine::{Closure, Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, LazyRecord, PipelineData, ShellError, Signature, Span,
Spanned, SyntaxShape, Type, Value,
use nu_engine::{command_prelude::*, eval_block};
use nu_protocol::{debugger::WithoutDebug, engine::Closure, LazyRecord};
use std::{
collections::{hash_map::Entry, HashMap},
sync::{Arc, Mutex},
};
#[derive(Clone)]

View File

@ -1,9 +1,4 @@
use nu_engine::get_eval_block;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
use nu_engine::{command_prelude::*, get_eval_block};
#[derive(Clone)]
pub struct Let;

View File

@ -1,10 +1,5 @@
use nu_engine::{get_eval_block, CallExt};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Block, Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
use nu_engine::{command_prelude::*, get_eval_block};
use nu_protocol::engine::Block;
#[derive(Clone)]
pub struct Loop;

View File

@ -1,9 +1,9 @@
use nu_engine::{get_eval_block, get_eval_expression, get_eval_expression_with_input, CallExt};
use nu_protocol::ast::{Call, Expr, Expression};
use nu_protocol::engine::{Command, EngineState, Matcher, Stack};
use nu_engine::{
command_prelude::*, get_eval_block, get_eval_expression, get_eval_expression_with_input,
};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
ast::{Expr, Expression},
engine::Matcher,
};
#[derive(Clone)]

View File

@ -1,8 +1,4 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct Module;

View File

@ -1,10 +1,4 @@
use nu_engine::get_eval_block;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
use nu_engine::{command_prelude::*, get_eval_block};
#[derive(Clone)]
pub struct Mut;

View File

@ -1,9 +1,4 @@
use nu_engine::get_full_help;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
use nu_engine::{command_prelude::*, get_full_help};
#[derive(Clone)]
pub struct Overlay;

View File

@ -1,9 +1,4 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct OverlayHide;

View File

@ -1,8 +1,4 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct OverlayList;

View File

@ -1,9 +1,4 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct OverlayNew;

View File

@ -1,10 +1,8 @@
use nu_engine::{find_in_dirs_env, get_dirs_var_from_call, get_eval_block, redirect_env, CallExt};
use nu_parser::trim_quotes_str;
use nu_protocol::ast::{Call, Expr};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
use nu_engine::{
command_prelude::*, find_in_dirs_env, get_dirs_var_from_call, get_eval_block, redirect_env,
};
use nu_parser::trim_quotes_str;
use nu_protocol::ast::Expr;
use std::path::Path;

View File

@ -1,9 +1,4 @@
use nu_engine::get_full_help;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
use nu_engine::{command_prelude::*, get_full_help};
#[derive(Clone)]
pub struct PluginCommand;

View File

@ -1,10 +1,5 @@
use itertools::Itertools;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
record, Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature,
Type, Value,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct PluginList;

View File

@ -1,9 +1,4 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct PluginStop;

View File

@ -1,6 +1,4 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct Register;

View File

@ -1,9 +1,4 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct Return;

View File

@ -1,9 +1,4 @@
use nu_engine::scope::ScopeData;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Type,
};
use nu_engine::{command_prelude::*, scope::ScopeData};
#[derive(Clone)]
pub struct ScopeAliases;

View File

@ -1,9 +1,4 @@
use nu_engine::get_full_help;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
};
use nu_engine::{command_prelude::*, get_full_help};
#[derive(Clone)]
pub struct Scope;

View File

@ -1,9 +1,4 @@
use nu_engine::scope::ScopeData;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Type,
};
use nu_engine::{command_prelude::*, scope::ScopeData};
#[derive(Clone)]
pub struct ScopeCommands;

View File

@ -1,7 +1,4 @@
use nu_engine::scope::ScopeData;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type};
use nu_engine::{command_prelude::*, scope::ScopeData};
#[derive(Clone)]
pub struct ScopeEngineStats;

View File

@ -1,9 +1,4 @@
use nu_engine::scope::ScopeData;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Type,
};
use nu_engine::{command_prelude::*, scope::ScopeData};
#[derive(Clone)]
pub struct ScopeExterns;

View File

@ -1,9 +1,4 @@
use nu_engine::scope::ScopeData;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Type,
};
use nu_engine::{command_prelude::*, scope::ScopeData};
#[derive(Clone)]
pub struct ScopeModules;

View File

@ -1,9 +1,4 @@
use nu_engine::scope::ScopeData;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Type,
};
use nu_engine::{command_prelude::*, scope::ScopeData};
#[derive(Clone)]
pub struct ScopeVariables;

View File

@ -1,11 +1,5 @@
use nu_engine::{get_eval_block, CallExt, EvalBlockFn};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Block, Closure, Command, EngineState, Stack};
use nu_protocol::{
record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span,
SyntaxShape, Type, Value,
};
use nu_engine::{command_prelude::*, get_eval_block, EvalBlockFn};
use nu_protocol::engine::{Block, Closure};
#[derive(Clone)]
pub struct Try;

View File

@ -1,10 +1,7 @@
use nu_engine::{find_in_dirs_env, get_dirs_var_from_call, get_eval_block, redirect_env};
use nu_protocol::ast::{Call, Expr, Expression};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
use nu_engine::{
command_prelude::*, find_in_dirs_env, get_dirs_var_from_call, get_eval_block, redirect_env,
};
use nu_protocol::ast::{Expr, Expression};
#[derive(Clone)]
pub struct Use;

View File

@ -1,8 +1,5 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack, StateWorkingSet};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, Record, ShellError, Signature, Type, Value,
};
use nu_engine::command_prelude::*;
use nu_protocol::engine::StateWorkingSet;
use shadow_rs::shadow;
shadow!(build);

View File

@ -1,10 +1,5 @@
use nu_engine::{get_eval_block, get_eval_expression, CallExt};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Block, Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value,
};
use nu_engine::{command_prelude::*, get_eval_block, get_eval_expression};
use nu_protocol::engine::Block;
#[derive(Clone)]
pub struct While;

View File

@ -1,6 +1,5 @@
use nu_protocol::engine::{EngineState, StateWorkingSet};
use crate::*;
use nu_protocol::engine::{EngineState, StateWorkingSet};
pub fn create_default_context() -> EngineState {
let mut engine_state = EngineState::new();

View File

@ -1,12 +1,11 @@
use itertools::Itertools;
use nu_protocol::debugger::WithoutDebug;
use nu_engine::command_prelude::*;
use nu_protocol::{
ast::{Block, RangeInclusion},
engine::{EngineState, Stack, StateDelta, StateWorkingSet},
Example, PipelineData, Signature, Span, Type, Value,
debugger::WithoutDebug,
engine::{StateDelta, StateWorkingSet},
};
use std::collections::HashSet;
use std::sync::Arc;
use std::{collections::HashSet, sync::Arc};
pub fn check_example_input_and_output_types_match_command_signature(
example: &Example,