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,11 +1,10 @@
use std::sync::Arc;
use crate::{
ast::{Argument, Block, Expr, ExternalArgument, ImportPattern, RecordItem},
engine::StateWorkingSet,
BlockId, DeclId, Signature, Span, Type, VarId, IN_VARIABLE_ID,
};
use serde::{Deserialize, Serialize};
use super::{Argument, Block, Expr, ExternalArgument, RecordItem};
use crate::ast::ImportPattern;
use crate::DeclId;
use crate::{engine::StateWorkingSet, BlockId, Signature, Span, Type, VarId, IN_VARIABLE_ID};
use std::sync::Arc;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Expression {

View File

@ -1,5 +1,6 @@
use crate::{Config, Record, ShellError, Span, Value};
use serde::{Deserialize, Serialize};
/// Definition of a parsed hook from the config object
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Hooks {

View File

@ -13,11 +13,12 @@
//! (`NoopDebugger` is placed in its place inside `EngineState`.) After deactivating, you can call
//! `Debugger::report()` to get some output from the debugger, if necessary.
use crate::ast::{Block, PipelineElement};
use crate::engine::EngineState;
use crate::{PipelineData, ShellError, Span, Value};
use std::fmt::Debug;
use std::ops::DerefMut;
use crate::{
ast::{Block, PipelineElement},
engine::EngineState,
PipelineData, ShellError, Span, Value,
};
use std::{fmt::Debug, ops::DerefMut};
/// Trait used for static dispatch of `eval_xxx()` evaluator calls
///

View File

@ -3,11 +3,12 @@
//! Profiler implements the Debugger trait and can be used via the `debug profile` command for
//! profiling Nushell code.
use crate::ast::{Block, Expr, PipelineElement};
use crate::debugger::Debugger;
use crate::engine::EngineState;
use crate::record;
use crate::{PipelineData, ShellError, Span, Value};
use crate::{
ast::{Block, Expr, PipelineElement},
debugger::Debugger,
engine::EngineState,
record, PipelineData, ShellError, Span, Value,
};
use std::time::Instant;
#[derive(Debug, Clone, Copy)]

View File

@ -1,5 +1,4 @@
use crate::ast::Call;
use crate::Span;
use crate::{ast::Call, Span};
#[derive(Debug, Clone)]
pub struct UnevaluatedCallInfo {

View File

@ -1,27 +1,24 @@
use crate::{
ast::Block,
debugger::{Debugger, NoopDebugger},
engine::{
usage::{build_usage, Usage},
CachedFile, Command, CommandType, EnvVars, OverlayFrame, ScopeFrame, Stack, StateDelta,
Variable, Visibility, DEFAULT_OVERLAY_NAME,
},
BlockId, Category, Config, DeclId, Example, FileId, HistoryConfig, Module, ModuleId, OverlayId,
ShellError, Signature, Span, Type, Value, VarId, VirtualPathId,
};
use fancy_regex::Regex;
use lru::LruCache;
use super::cached_file::CachedFile;
use super::{usage::build_usage, usage::Usage, StateDelta};
use super::{
Command, CommandType, EnvVars, OverlayFrame, ScopeFrame, Stack, Variable, Visibility,
DEFAULT_OVERLAY_NAME,
};
use crate::ast::Block;
use crate::debugger::{Debugger, NoopDebugger};
use crate::{
BlockId, Config, DeclId, Example, FileId, HistoryConfig, Module, ModuleId, OverlayId,
ShellError, Signature, Span, Type, VarId, VirtualPathId,
};
use crate::{Category, Value};
use std::collections::HashMap;
use std::num::NonZeroUsize;
use std::path::Path;
use std::path::PathBuf;
use std::sync::atomic::Ordering;
use std::sync::{
atomic::{AtomicBool, AtomicU32},
Arc, Mutex, MutexGuard, PoisonError,
use std::{
collections::HashMap,
num::NonZeroUsize,
path::{Path, PathBuf},
sync::{
atomic::{AtomicBool, AtomicU32, Ordering},
Arc, Mutex, MutexGuard, PoisonError,
},
};
type PoisonDebuggerError<'a> = PoisonError<MutexGuard<'a, Box<dyn Debugger>>>;

View File

@ -1,12 +1,14 @@
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use crate::{
engine::{EngineState, DEFAULT_OVERLAY_NAME},
engine::{
EngineState, Redirection, StackCallArgGuard, StackCaptureGuard, StackIoGuard, StackStdio,
DEFAULT_OVERLAY_NAME,
},
IoStream, ShellError, Span, Value, VarId, ENV_VARIABLE_ID, NU_VARIABLE_ID,
};
use super::{Redirection, StackCallArgGuard, StackCaptureGuard, StackIoGuard, StackStdio};
use std::{
collections::{HashMap, HashSet},
sync::Arc,
};
/// Environment variables per overlay
pub type EnvVars = HashMap<String, HashMap<String, Value>>;

View File

@ -1,8 +1,11 @@
use super::cached_file::CachedFile;
use super::{usage::Usage, Command, EngineState, OverlayFrame, ScopeFrame, Variable, VirtualPath};
use crate::ast::Block;
use crate::Module;
use crate::{
ast::Block,
engine::{
usage::Usage, CachedFile, Command, EngineState, OverlayFrame, ScopeFrame, Variable,
VirtualPath,
},
Module,
};
use std::sync::Arc;
#[cfg(feature = "plugin")]

View File

@ -1,17 +1,18 @@
use super::cached_file::CachedFile;
use super::CommandType;
use super::{
usage::build_usage, Command, EngineState, OverlayFrame, StateDelta, Variable, VirtualPath,
Visibility, PWD_ENV,
use crate::{
ast::Block,
engine::{
usage::build_usage, CachedFile, Command, CommandType, EngineState, OverlayFrame,
StateDelta, Variable, VirtualPath, Visibility, PWD_ENV,
},
BlockId, Category, Config, DeclId, FileId, Module, ModuleId, ParseError, ParseWarning, Span,
Type, Value, VarId, VirtualPathId,
};
use crate::ast::Block;
use crate::{BlockId, Config, DeclId, FileId, Module, ModuleId, Span, Type, VarId, VirtualPathId};
use crate::{Category, ParseError, ParseWarning, Value};
use core::panic;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::Arc;
use std::{
collections::{HashMap, HashSet},
path::PathBuf,
sync::Arc,
};
#[cfg(feature = "plugin")]
use crate::{PluginIdentity, RegisteredPlugin};

View File

@ -1,3 +1,4 @@
use crate::{engine::Stack, IoStream};
use std::{
fs::File,
mem,
@ -5,10 +6,6 @@ use std::{
sync::Arc,
};
use crate::IoStream;
use super::Stack;
#[derive(Debug, Clone)]
pub enum Redirection {
/// A pipe redirection.

View File

@ -1,12 +1,11 @@
use crate::debugger::DebugContext;
use crate::{
ast::{
eval_operator, Assignment, Bits, Boolean, Call, Comparison, Expr, Expression,
ExternalArgument, Math, Operator, RecordItem,
},
debugger::DebugContext,
Config, IntoInterruptiblePipelineData, Range, Record, ShellError, Span, Value, VarId,
};
use std::{borrow::Cow, collections::HashMap};
/// To share implementations for regular eval and const eval

View File

@ -1,13 +1,15 @@
use crate::debugger::{DebugContext, WithoutDebug};
use crate::{
ast::{Assignment, Block, Call, Expr, Expression, ExternalArgument},
debugger::{DebugContext, WithoutDebug},
engine::{EngineState, StateWorkingSet},
eval_base::Eval,
record, Config, HistoryFileFormat, PipelineData, Record, ShellError, Span, Value, VarId,
};
use nu_system::os_info::{get_kernel_version, get_os_arch, get_os_family, get_os_name};
use std::borrow::Cow;
use std::path::{Path, PathBuf};
use std::{
borrow::Cow,
path::{Path, PathBuf},
};
pub fn create_nu_constant(engine_state: &EngineState, span: Span) -> Result<Value, ShellError> {
fn canonicalize_path(engine_state: &EngineState, path: &Path) -> PathBuf {

View File

@ -12,9 +12,11 @@ use crate::{
format_error, Config, ShellError, Span, Value,
};
use nu_utils::{stderr_write_all_and_flush, stdout_write_all_and_flush};
use std::io::{self, Cursor, Read, Write};
use std::sync::{atomic::AtomicBool, Arc};
use std::thread;
use std::{
io::{self, Cursor, Read, Write},
sync::{atomic::AtomicBool, Arc},
thread,
};
const LINE_ENDING_PATTERN: &[char] = &['\r', '\n'];

View File

@ -1,9 +1,8 @@
use crate::{PluginExample, Signature};
use serde::Deserialize;
use serde::Serialize;
use crate::engine::Command;
use crate::{BlockId, Category, Flag, PositionalArg, SyntaxShape, Type};
use crate::{
engine::Command, BlockId, Category, Flag, PluginExample, PositionalArg, Signature, SyntaxShape,
Type,
};
use serde::{Deserialize, Serialize};
/// A simple wrapper for Signature that includes examples.
#[derive(Debug, Clone, Serialize, Deserialize)]

View File

@ -1,17 +1,9 @@
use serde::Deserialize;
use serde::Serialize;
use crate::ast::Call;
use crate::engine::Command;
use crate::engine::EngineState;
use crate::engine::Stack;
use crate::BlockId;
use crate::PipelineData;
use crate::ShellError;
use crate::SyntaxShape;
use crate::Type;
use crate::Value;
use crate::VarId;
use crate::{
ast::Call,
engine::{Command, EngineState, Stack},
BlockId, PipelineData, ShellError, SyntaxShape, Type, Value, VarId,
};
use serde::{Deserialize, Serialize};
use std::fmt::Write;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]

View File

@ -1,10 +1,10 @@
use std::path::PathBuf;
use super::NuGlob;
use crate::ast::{CellPath, PathMember};
use crate::engine::{Block, Closure};
use crate::{Range, Record, ShellError, Spanned, Value};
use crate::{
ast::{CellPath, PathMember},
engine::{Block, Closure},
NuGlob, Range, Record, ShellError, Spanned, Value,
};
use chrono::{DateTime, FixedOffset};
use std::path::PathBuf;
pub trait FromValue: Sized {
fn from_value(v: Value) -> Result<Self, ShellError>;

View File

@ -17,23 +17,26 @@ pub use lazy_record::LazyRecord;
pub use range::*;
pub use record::Record;
use crate::ast::{Bits, Boolean, CellPath, Comparison, Math, Operator, PathMember, RangeInclusion};
use crate::engine::{Closure, EngineState};
use crate::{did_you_mean, BlockId, Config, ShellError, Span, Type};
use crate::{
ast::{Bits, Boolean, CellPath, Comparison, Math, Operator, PathMember, RangeInclusion},
did_you_mean,
engine::{Closure, EngineState},
BlockId, Config, ShellError, Span, Type,
};
use chrono::{DateTime, Datelike, FixedOffset, Locale, TimeZone};
use chrono_humanize::HumanTime;
use fancy_regex::Regex;
use nu_utils::locale::LOCALE_OVERRIDE_ENV_VAR;
use nu_utils::{contains_emoji, locale::get_system_locale_string, IgnoreCaseExt};
use nu_utils::{
contains_emoji,
locale::{get_system_locale_string, LOCALE_OVERRIDE_ENV_VAR},
IgnoreCaseExt,
};
use serde::{Deserialize, Serialize};
use std::fmt::Write;
use std::{
borrow::Cow,
fmt::Display,
cmp::Ordering,
fmt::{Debug, Display, Write},
path::PathBuf,
{cmp::Ordering, fmt::Debug},
};
/// Core structured values that pass through the pipeline in Nushell.