diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index b9f2987528..12d4682006 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -18,7 +18,7 @@ Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass -- `cargo run -- crates/nu-utils/standard_library/tests.nu` to run the tests for the standard library +- `cargo run -- crates/nu-std/tests.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d246f3ddc2..3af32c7821 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -116,7 +116,7 @@ jobs: run: cargo install --path . --locked --no-default-features - name: Standard library tests - run: nu crates/nu-utils/standard_library/tests.nu + run: nu crates/nu-std/tests.nu - name: Setup Python uses: actions/setup-python@v4 diff --git a/Cargo.lock b/Cargo.lock index 9f7f71e9ed..4960f54dd9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2699,6 +2699,7 @@ dependencies = [ "nu-plugin", "nu-pretty-hex", "nu-protocol", + "nu-std", "nu-system", "nu-table", "nu-term-grid", @@ -3013,6 +3014,16 @@ dependencies = [ "typetag", ] +[[package]] +name = "nu-std" +version = "0.78.1" +dependencies = [ + "miette", + "nu-cli", + "nu-parser", + "nu-protocol", +] + [[package]] name = "nu-system" version = "0.78.1" diff --git a/Cargo.toml b/Cargo.toml index e88786b967..cb780a76a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,7 @@ members = [ "crates/nu_plugin_query", "crates/nu_plugin_custom_values", "crates/nu_plugin_formats", + "crates/nu-std", "crates/nu-utils", ] @@ -60,6 +61,7 @@ nu-protocol = { path = "./crates/nu-protocol", version = "0.78.1" } nu-system = { path = "./crates/nu-system", version = "0.78.1" } nu-table = { path = "./crates/nu-table", version = "0.78.1" } nu-term-grid = { path = "./crates/nu-term-grid", version = "0.78.1" } +nu-std = { path = "./crates/nu-std", version = "0.78.1" } nu-utils = { path = "./crates/nu-utils", version = "0.78.1" } nu-ansi-term = "0.47.0" diff --git a/crates/nu-std/Cargo.toml b/crates/nu-std/Cargo.toml new file mode 100644 index 0000000000..22c6b5bbec --- /dev/null +++ b/crates/nu-std/Cargo.toml @@ -0,0 +1,14 @@ +[package] +authors = ["The Nushell Project Developers"] +description = "The standard library of Nushell" +repository = "https://github.com/nushell/nushell/tree/main/crates/nu-std" +edition = "2021" +license = "MIT" +name = "nu-std" +version = "0.78.1" + +[dependencies] +miette = { version = "5.6.0", features = ["fancy-no-backtrace"] } +nu-cli = { version = "0.78.1", path = "../nu-cli" } +nu-parser = { version = "0.78.1", path = "../nu-parser" } +nu-protocol = { version = "0.78.1", path = "../nu-protocol" } diff --git a/crates/nu-utils/standard_library/README.md b/crates/nu-std/README.md similarity index 100% rename from crates/nu-utils/standard_library/README.md rename to crates/nu-std/README.md diff --git a/crates/nu-std/src/lib.rs b/crates/nu-std/src/lib.rs new file mode 100644 index 0000000000..8597e00e36 --- /dev/null +++ b/crates/nu-std/src/lib.rs @@ -0,0 +1,88 @@ +use nu_cli::report_error; +use nu_parser::{parse, parse_module_block}; +use nu_protocol::{engine::StateWorkingSet, Module, ShellError, Span}; + +fn get_standard_library() -> &'static str { + include_str!("../std.nu") +} + +fn load_prelude(working_set: &mut StateWorkingSet, prelude: Vec<(&str, &str)>, module: &Module) { + let mut decls = Vec::new(); + let mut errs = Vec::new(); + for (name, search_name) in prelude { + if let Some(id) = module.decls.get(&search_name.as_bytes().to_vec()) { + let decl = (name.as_bytes().to_vec(), id.to_owned()); + decls.push(decl); + } else { + errs.push(ShellError::GenericError( + format!("could not load `{}` from `std`.", search_name), + String::new(), + None, + None, + Vec::new(), + )); + } + } + + if !errs.is_empty() { + report_error( + working_set, + &ShellError::GenericError( + "Unable to load the prelude of the standard library.".into(), + String::new(), + None, + Some("this is a bug: please file an issue in the [issue tracker](https://github.com/nushell/nushell/issues/new/choose)".to_string()), + errs, + ), + ); + } + + working_set.use_decls(decls); +} + +pub fn load_standard_library( + engine_state: &mut nu_protocol::engine::EngineState, +) -> Result<(), miette::ErrReport> { + let delta = { + let name = "std".to_string(); + let content = get_standard_library().as_bytes(); + + let mut working_set = StateWorkingSet::new(engine_state); + + let start = working_set.next_span_start(); + working_set.add_file(name.clone(), content); + let end = working_set.next_span_start(); + + let (_, module, comments) = + parse_module_block(&mut working_set, Span::new(start, end), name.as_bytes()); + + if let Some(err) = working_set.parse_errors.first() { + report_error(&working_set, err); + } + + parse(&mut working_set, Some(&name), content, true); + + if let Some(err) = working_set.parse_errors.first() { + report_error(&working_set, err); + } + + let prelude = vec![ + ("std help", "help"), + ("std help commands", "help commands"), + ("std help aliases", "help aliases"), + ("std help modules", "help modules"), + ("std help externs", "help externs"), + ("std help operators", "help operators"), + ]; + + load_prelude(&mut working_set, prelude, &module); + + working_set.add_module(&name, module, comments); + + working_set.render() + }; + + engine_state.merge_delta(delta)?; + + Ok(()) +} diff --git a/crates/nu-utils/standard_library/std.nu b/crates/nu-std/std.nu similarity index 100% rename from crates/nu-utils/standard_library/std.nu rename to crates/nu-std/std.nu diff --git a/crates/nu-utils/standard_library/test_asserts.nu b/crates/nu-std/test_asserts.nu similarity index 100% rename from crates/nu-utils/standard_library/test_asserts.nu rename to crates/nu-std/test_asserts.nu diff --git a/crates/nu-utils/standard_library/test_dirs.nu b/crates/nu-std/test_dirs.nu similarity index 100% rename from crates/nu-utils/standard_library/test_dirs.nu rename to crates/nu-std/test_dirs.nu diff --git a/crates/nu-utils/standard_library/test_logger.nu b/crates/nu-std/test_logger.nu similarity index 100% rename from crates/nu-utils/standard_library/test_logger.nu rename to crates/nu-std/test_logger.nu diff --git a/crates/nu-utils/standard_library/test_std.nu b/crates/nu-std/test_std.nu similarity index 100% rename from crates/nu-utils/standard_library/test_std.nu rename to crates/nu-std/test_std.nu diff --git a/crates/nu-utils/standard_library/test_xml.nu b/crates/nu-std/test_xml.nu similarity index 100% rename from crates/nu-utils/standard_library/test_xml.nu rename to crates/nu-std/test_xml.nu diff --git a/crates/nu-utils/standard_library/tests.nu b/crates/nu-std/tests.nu similarity index 100% rename from crates/nu-utils/standard_library/tests.nu rename to crates/nu-std/tests.nu diff --git a/src/run.rs b/src/run.rs index ffcede2585..303925bdbf 100644 --- a/src/run.rs +++ b/src/run.rs @@ -6,96 +6,11 @@ use crate::{ }; #[cfg(feature = "plugin")] use nu_cli::read_plugin_file; -use nu_cli::{evaluate_commands, evaluate_file, evaluate_repl, report_error}; -use nu_parser::{parse, parse_module_block}; -use nu_protocol::{engine::StateWorkingSet, Module, PipelineData, ShellError, Span}; +use nu_cli::{evaluate_commands, evaluate_file, evaluate_repl}; +use nu_protocol::PipelineData; +use nu_std::load_standard_library; use nu_utils::utils::perf; -fn get_standard_library() -> &'static str { - include_str!("../crates/nu-utils/standard_library/std.nu") -} - -fn load_prelude(working_set: &mut StateWorkingSet, prelude: Vec<(&str, &str)>, module: &Module) { - let mut decls = Vec::new(); - let mut errs = Vec::new(); - for (name, search_name) in prelude { - if let Some(id) = module.decls.get(&search_name.as_bytes().to_vec()) { - let decl = (name.as_bytes().to_vec(), id.to_owned()); - decls.push(decl); - } else { - errs.push(ShellError::GenericError( - format!("could not load `{}` from `std`.", search_name), - String::new(), - None, - None, - Vec::new(), - )); - } - } - - if !errs.is_empty() { - report_error( - working_set, - &ShellError::GenericError( - "Unable to load the prelude of the standard library.".into(), - String::new(), - None, - Some("this is a bug: please file an issue in the [issue tracker](https://github.com/nushell/nushell/issues/new/choose)".to_string()), - errs, - ), - ); - } - - working_set.use_decls(decls); -} - -fn load_standard_library( - engine_state: &mut nu_protocol::engine::EngineState, -) -> Result<(), miette::ErrReport> { - let delta = { - let name = "std".to_string(); - let content = get_standard_library().as_bytes(); - - let mut working_set = StateWorkingSet::new(engine_state); - - let start = working_set.next_span_start(); - working_set.add_file(name.clone(), content); - let end = working_set.next_span_start(); - - let (_, module, comments) = - parse_module_block(&mut working_set, Span::new(start, end), name.as_bytes()); - - if let Some(err) = working_set.parse_errors.first() { - report_error(&working_set, err); - } - - parse(&mut working_set, Some(&name), content, true); - - if let Some(err) = working_set.parse_errors.first() { - report_error(&working_set, err); - } - - let prelude = vec![ - ("std help", "help"), - ("std help commands", "help commands"), - ("std help aliases", "help aliases"), - ("std help modules", "help modules"), - ("std help externs", "help externs"), - ("std help operators", "help operators"), - ]; - - load_prelude(&mut working_set, prelude, &module); - - working_set.add_module(&name, module, comments); - - working_set.render() - }; - - engine_state.merge_delta(delta)?; - - Ok(()) -} - pub(crate) fn run_commands( engine_state: &mut nu_protocol::engine::EngineState, parsed_nu_cli_args: command::NushellCliArgs, diff --git a/toolkit.nu b/toolkit.nu index 1139c2ff60..8ba0f138fa 100644 --- a/toolkit.nu +++ b/toolkit.nu @@ -37,7 +37,7 @@ export def test [ # run the tests for the standard library export def "test stdlib" [] { - cargo run -- crates/nu-utils/standard_library/tests.nu + cargo run -- crates/nu-std/tests.nu } # print the pipe input inside backticks, dimmed and italic, as a pretty command