mirror of
https://github.com/nushell/nushell
synced 2024-12-26 13:03:07 +00:00
refactor the lib.rs
of nu-std
after the new virtual files PR (#9289)
This commit is contained in:
parent
e5ff72120a
commit
3e55addbdd
3 changed files with 25 additions and 58 deletions
|
@ -12,61 +12,27 @@ pub fn load_standard_library(
|
||||||
engine_state: &mut nu_protocol::engine::EngineState,
|
engine_state: &mut nu_protocol::engine::EngineState,
|
||||||
) -> Result<(), miette::ErrReport> {
|
) -> Result<(), miette::ErrReport> {
|
||||||
let (block, delta) = {
|
let (block, delta) = {
|
||||||
|
// Using full virtual path to avoid potential conflicts with user having 'std' directory
|
||||||
|
// in their working directory.
|
||||||
|
let std_dir = PathBuf::from(NU_STDLIB_VIRTUAL_DIR).join("std");
|
||||||
|
|
||||||
let mut std_files = vec![
|
let mut std_files = vec![
|
||||||
(
|
("mod.nu", include_str!("../std/mod.nu")),
|
||||||
PathBuf::from(NU_STDLIB_VIRTUAL_DIR)
|
("dirs.nu", include_str!("../std/dirs.nu")),
|
||||||
.join("std")
|
("dt.nu", include_str!("../std/dt.nu")),
|
||||||
.join("mod.nu"),
|
("help.nu", include_str!("../std/help.nu")),
|
||||||
include_str!("../std/mod.nu"),
|
("iter.nu", include_str!("../std/iter.nu")),
|
||||||
),
|
("log.nu", include_str!("../std/log.nu")),
|
||||||
(
|
("testing.nu", include_str!("../std/testing.nu")),
|
||||||
PathBuf::from(NU_STDLIB_VIRTUAL_DIR)
|
("xml.nu", include_str!("../std/xml.nu")),
|
||||||
.join("std")
|
|
||||||
.join("dirs.nu"),
|
|
||||||
include_str!("../std/dirs.nu"),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
PathBuf::from(NU_STDLIB_VIRTUAL_DIR)
|
|
||||||
.join("std")
|
|
||||||
.join("dt.nu"),
|
|
||||||
include_str!("../std/dt.nu"),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
PathBuf::from(NU_STDLIB_VIRTUAL_DIR)
|
|
||||||
.join("std")
|
|
||||||
.join("help.nu"),
|
|
||||||
include_str!("../std/help.nu"),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
PathBuf::from(NU_STDLIB_VIRTUAL_DIR)
|
|
||||||
.join("std")
|
|
||||||
.join("iter.nu"),
|
|
||||||
include_str!("../std/iter.nu"),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
PathBuf::from(NU_STDLIB_VIRTUAL_DIR)
|
|
||||||
.join("std")
|
|
||||||
.join("log.nu"),
|
|
||||||
include_str!("../std/log.nu"),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
PathBuf::from(NU_STDLIB_VIRTUAL_DIR)
|
|
||||||
.join("std")
|
|
||||||
.join("testing.nu"),
|
|
||||||
include_str!("../std/testing.nu"),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
PathBuf::from(NU_STDLIB_VIRTUAL_DIR)
|
|
||||||
.join("std")
|
|
||||||
.join("xml.nu"),
|
|
||||||
include_str!("../std/xml.nu"),
|
|
||||||
),
|
|
||||||
];
|
];
|
||||||
|
|
||||||
let mut working_set = StateWorkingSet::new(engine_state);
|
let mut working_set = StateWorkingSet::new(engine_state);
|
||||||
let mut std_virt_paths = vec![];
|
let mut std_virt_paths = vec![];
|
||||||
|
|
||||||
for (name, content) in std_files.drain(..) {
|
for (name, content) in std_files.drain(..) {
|
||||||
|
let name = std_dir.join(name);
|
||||||
|
|
||||||
let file_id =
|
let file_id =
|
||||||
working_set.add_file(name.to_string_lossy().to_string(), content.as_bytes());
|
working_set.add_file(name.to_string_lossy().to_string(), content.as_bytes());
|
||||||
let virtual_file_id = working_set.add_virtual_path(
|
let virtual_file_id = working_set.add_virtual_path(
|
||||||
|
@ -76,19 +42,21 @@ pub fn load_standard_library(
|
||||||
std_virt_paths.push(virtual_file_id);
|
std_virt_paths.push(virtual_file_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Using full virtual path to avoid potential conflicts with user having 'std' directory
|
let std_dir = std_dir.to_string_lossy().to_string();
|
||||||
// in their working directory.
|
|
||||||
let std_dir = PathBuf::from(NU_STDLIB_VIRTUAL_DIR)
|
|
||||||
.join("std")
|
|
||||||
.to_string_lossy()
|
|
||||||
.to_string();
|
|
||||||
let source = format!(
|
let source = format!(
|
||||||
r#"
|
r#"
|
||||||
# Define the `std` module
|
# Define the `std` module
|
||||||
module {std_dir}
|
module {std_dir}
|
||||||
|
|
||||||
# Prelude
|
# Prelude
|
||||||
use std dirs [ enter, shells, g, n, p, dexit ]
|
use std dirs [
|
||||||
|
enter
|
||||||
|
shells
|
||||||
|
g
|
||||||
|
n
|
||||||
|
p
|
||||||
|
dexit
|
||||||
|
]
|
||||||
"#
|
"#
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
# Assert commands and test runner.
|
# Assert commands and test runner.
|
||||||
#
|
#
|
||||||
##################################################################################
|
##################################################################################
|
||||||
export use log.nu
|
use log.nu
|
||||||
|
|
||||||
# Universal assert command
|
# Universal assert command
|
||||||
#
|
#
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use std "log debug"
|
use std log
|
||||||
use std "log error"
|
|
||||||
use std "assert"
|
use std "assert"
|
||||||
use std "assert skip"
|
use std "assert skip"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue