nushell/crates/nu-command/src/path/mod.rs
Ian Manske c747ec75c9
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.
2024-03-26 21:17:30 +00:00

70 lines
2.0 KiB
Rust

mod basename;
mod dirname;
mod exists;
mod expand;
mod join;
mod parse;
pub mod path_;
mod relative_to;
mod split;
mod r#type;
pub use basename::SubCommand as PathBasename;
pub use dirname::SubCommand as PathDirname;
pub use exists::SubCommand as PathExists;
pub use expand::SubCommand as PathExpand;
pub use join::SubCommand as PathJoin;
pub use parse::SubCommand as PathParse;
pub use path_::PathCommand as Path;
pub use r#type::SubCommand as PathType;
pub use relative_to::SubCommand as PathRelativeTo;
pub use split::SubCommand as PathSplit;
use nu_protocol::{ShellError, Span, Value};
use std::path::Path as StdPath;
#[cfg(windows)]
const ALLOWED_COLUMNS: [&str; 4] = ["prefix", "parent", "stem", "extension"];
#[cfg(not(windows))]
const ALLOWED_COLUMNS: [&str; 3] = ["parent", "stem", "extension"];
trait PathSubcommandArguments {}
fn operate<F, A>(cmd: &F, args: &A, v: Value, name: Span) -> Value
where
F: Fn(&StdPath, Span, &A) -> Value + Send + Sync + 'static,
A: PathSubcommandArguments + Send + Sync + 'static,
{
let span = v.span();
match v {
Value::String { val, .. } => cmd(StdPath::new(&val), span, args),
_ => handle_invalid_values(v, name),
}
}
fn handle_invalid_values(rest: Value, name: Span) -> Value {
Value::error(err_from_value(&rest, name), name)
}
fn err_from_value(rest: &Value, name: Span) -> ShellError {
match rest {
Value::Error { error, .. } => *error.clone(),
_ => {
if rest.is_nothing() {
ShellError::OnlySupportsThisInputType {
exp_input_type: "string, record or list".into(),
wrong_type: "nothing".into(),
dst_span: name,
src_span: rest.span(),
}
} else {
ShellError::PipelineMismatch {
exp_input_type: "string, row or list".into(),
dst_span: name,
src_span: rest.span(),
}
}
}
}
}