Merge pull request #69 from kubouch/simple-module

Primitive module implementation
This commit is contained in:
JT
2021-09-27 05:14:23 +13:00
committed by GitHub
8 changed files with 372 additions and 3 deletions

View File

@ -7,7 +7,7 @@ use nu_protocol::{
use crate::{
where_::Where, Alias, Benchmark, BuildString, Def, Do, Each, External, For, Git, GitCheckout,
If, Length, Let, LetEnv, Lines, ListGitBranches, Ls, Table,
If, Length, Let, LetEnv, Lines, ListGitBranches, Ls, Module, Table, Use,
};
pub fn create_default_context() -> Rc<RefCell<EngineState>> {
@ -46,6 +46,10 @@ pub fn create_default_context() -> Rc<RefCell<EngineState>> {
working_set.add_decl(Box::new(Ls));
working_set.add_decl(Box::new(Module));
working_set.add_decl(Box::new(Use));
working_set.add_decl(Box::new(Table));
working_set.add_decl(Box::new(External));

View File

@ -15,8 +15,10 @@ mod let_env;
mod lines;
mod list_git_branches;
mod ls;
mod module;
mod run_external;
mod table;
mod use_;
mod where_;
pub use alias::Alias;
@ -36,5 +38,7 @@ pub use let_env::LetEnv;
pub use lines::Lines;
pub use list_git_branches::ListGitBranches;
pub use ls::Ls;
pub use module::Module;
pub use run_external::External;
pub use table::Table;
pub use use_::Use;

View File

@ -0,0 +1,34 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, SyntaxShape, Value};
pub struct Module;
impl Command for Module {
fn name(&self) -> &str {
"module"
}
fn usage(&self) -> &str {
"Define a custom module"
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("module")
.required("module_name", SyntaxShape::String, "module name")
.required(
"block",
SyntaxShape::Block(Some(vec![])),
"body of the module",
)
}
fn run(
&self,
_context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
Ok(Value::Nothing { span: call.head })
}
}

View File

@ -0,0 +1,28 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, SyntaxShape, Value};
pub struct Use;
impl Command for Use {
fn name(&self) -> &str {
"use"
}
fn usage(&self) -> &str {
"Use definitions from a module"
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("use").required("module_name", SyntaxShape::String, "module name")
}
fn run(
&self,
_context: &EvaluationContext,
call: &Call,
_input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
Ok(Value::Nothing { span: call.head })
}
}