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,12 +1,6 @@
use indexmap::indexmap;
use indexmap::map::IndexMap;
use nu_engine::CallExt;
use nu_protocol::engine::{EngineState, Stack, StateWorkingSet};
use nu_protocol::record;
use nu_protocol::{
ast::Call, engine::Command, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData,
PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use indexmap::{indexmap, IndexMap};
use nu_engine::command_prelude::*;
use nu_protocol::engine::StateWorkingSet;
use once_cell::sync::Lazy;
use std::sync::{atomic::AtomicBool, Arc};

View File

@ -1,16 +1,7 @@
use nu_protocol::IntoPipelineData;
use std::io::Cursor;
use std::iter::Peekable;
use std::str::CharIndices;
use itertools::Itertools;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
record, Category, Example, IntoInterruptiblePipelineData, PipelineData, Range, Record,
ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
use nu_protocol::Range;
use std::{io::Cursor, iter::Peekable, str::CharIndices};
type Input<'t> = Peekable<CharIndices<'t>>;

View File

@ -1,12 +1,18 @@
use base64::{
alphabet, engine::general_purpose::NO_PAD, engine::general_purpose::PAD,
engine::GeneralPurpose, Engine,
alphabet,
engine::{
general_purpose::{NO_PAD, PAD},
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};
use nu_protocol::{PipelineData, ShellError, Span, Spanned, Value};
use nu_protocol::{
ast::{Call, CellPath},
engine::{EngineState, Stack},
PipelineData, ShellError, Span, Spanned, Value,
};
pub const CHARACTER_SET_DESC: &str = "specify the character rules for encoding the input.\n\
\tValid values are 'standard', 'standard-no-padding', 'url-safe', 'url-safe-no-padding',\

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, Spanned,
SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct Decode;

View File

@ -1,9 +1,5 @@
use super::base64::{operate, ActionType, CHARACTER_SET_DESC};
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 DecodeBase64;

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, Spanned,
SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct Encode;

View File

@ -1,9 +1,5 @@
use super::base64::{operate, ActionType, CHARACTER_SET_DESC};
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 EncodeBase64;

View File

@ -1,17 +1,10 @@
use crate::{generate_strftime_list, parse_date_from_string};
use chrono::{DateTime, Locale, TimeZone};
use nu_engine::command_prelude::*;
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type,
Value,
};
use nu_utils::locale::{get_system_locale_string, LOCALE_OVERRIDE_ENV_VAR};
use std::fmt::{Display, Write};
use crate::{generate_strftime_list, parse_date_from_string};
#[derive(Clone)]
pub struct FormatDate;

View File

@ -1,10 +1,5 @@
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};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
struct Arguments {
format_value: String,

View File

@ -1,11 +1,6 @@
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};
use nu_protocol::{
format_filesize, Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape,
Type, Value,
};
use nu_engine::command_prelude::*;
use nu_protocol::format_filesize;
struct Arguments {
format_value: String,

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 Format;

View File

@ -11,11 +11,11 @@ pub use char_::Char;
pub use detect_columns::*;
pub use encode_decode::*;
pub use format::*;
use nu_engine::CallExt;
pub use parse::*;
pub use split::*;
pub use str_::*;
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{EngineState, Stack, StateWorkingSet},

View File

@ -1,13 +1,9 @@
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use fancy_regex::Regex;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
record, Category, Example, ListStream, PipelineData, ShellError, Signature, Span, Spanned,
SyntaxShape, Type, Value,
use nu_engine::command_prelude::*;
use nu_protocol::ListStream;
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
#[derive(Clone)]

View File

@ -1,9 +1,5 @@
use crate::grapheme_flags;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
};
use nu_engine::command_prelude::*;
use unicode_segmentation::UnicodeSegmentation;
#[derive(Clone)]

View File

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

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 SplitCommand;

View File

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

View File

@ -1,11 +1,7 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type,
Value,
};
use nu_engine::command_prelude::*;
use regex::Regex;
#[derive(Clone)]
pub struct SubCommand;

View File

@ -1,11 +1,7 @@
use crate::grapheme_flags;
use fancy_regex::Regex;
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
use unicode_segmentation::UnicodeSegmentation;
#[derive(Clone)]

View File

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

View File

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

View File

@ -8,12 +8,8 @@ pub use downcase::SubCommand as StrDowncase;
pub use str_::Str;
pub use upcase::SubCommand as StrUpcase;
use nu_engine::CallExt;
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};
use nu_engine::command_prelude::*;
struct Arguments<F: Fn(&str) -> String + Send + Sync + 'static> {
case_operation: &'static F,

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 Str;

View File

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

View File

@ -1,12 +1,6 @@
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::record;
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
use nu_utils::IgnoreCaseExt;
#[derive(Clone)]

View File

@ -1,11 +1,6 @@
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},
engine::{Command, EngineState, Stack},
levenshtein_distance, record, Category, Example, PipelineData, ShellError, Signature, Span,
SyntaxShape, Type, Value,
};
use nu_engine::command_prelude::*;
use nu_protocol::levenshtein_distance;
#[derive(Clone)]
pub struct SubCommand;

View File

@ -1,10 +1,6 @@
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::Category;
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value};
use nu_engine::command_prelude::*;
use nu_utils::IgnoreCaseExt;
struct Arguments {

View File

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

View File

@ -1,13 +1,10 @@
use crate::grapheme_flags;
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_cmd_base::util;
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Range, ShellError, Signature, Span, Spanned, SyntaxShape,
Type, Value,
use nu_cmd_base::{
input_handler::{operate, CmdArgument},
util,
};
use nu_engine::command_prelude::*;
use nu_protocol::Range;
use unicode_segmentation::UnicodeSegmentation;
struct Arguments {

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, SyntaxShape, Type,
Value,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct StrJoin;

View File

@ -1,12 +1,7 @@
use crate::grapheme_flags;
use crate::grapheme_flags_const;
use crate::{grapheme_flags, grapheme_flags_const};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;
use nu_protocol::engine::{Command, EngineState, Stack, StateWorkingSet};
use nu_protocol::Category;
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value};
use nu_engine::command_prelude::*;
use nu_protocol::engine::StateWorkingSet;
use unicode_segmentation::UnicodeSegmentation;
struct Arguments {

View File

@ -1,12 +1,6 @@
use fancy_regex::{NoExpand, Regex};
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},
engine::{Command, EngineState, Stack},
record, Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape,
Type, Value,
};
use nu_engine::command_prelude::*;
struct Arguments {
all: bool,

View File

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

View File

@ -1,11 +1,6 @@
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::Category;
use nu_protocol::Spanned;
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value};
use nu_engine::command_prelude::*;
use nu_utils::IgnoreCaseExt;
struct Arguments {

View File

@ -1,9 +1,5 @@
use fancy_regex::Regex;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
record, Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
};
use nu_engine::command_prelude::*;
use std::collections::BTreeMap;
use std::{fmt, str};
use unicode_segmentation::UnicodeSegmentation;

View File

@ -1,14 +1,10 @@
use crate::grapheme_flags;
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_cmd_base::util;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::Category;
use nu_protocol::{
Example, PipelineData, Range, ShellError, Signature, Span, SyntaxShape, Type, Value,
use nu_cmd_base::{
input_handler::{operate, CmdArgument},
util,
};
use nu_engine::command_prelude::*;
use nu_protocol::Range;
use std::cmp::Ordering;
use unicode_segmentation::UnicodeSegmentation;

View File

@ -1,11 +1,5 @@
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type,
Value,
};
use nu_engine::command_prelude::*;
#[derive(Clone)]
pub struct SubCommand;