nushell/crates/nu-cmd-base/src/util.rs

58 lines
1.7 KiB
Rust
Raw Normal View History

use nu_protocol::report_error;
use nu_protocol::{
ast::RangeInclusion,
engine::{EngineState, Stack, StateWorkingSet},
Range, ShellError, Span, Value,
};
use std::path::PathBuf;
pub fn get_init_cwd() -> PathBuf {
std::env::current_dir().unwrap_or_else(|_| {
std::env::var("PWD")
.map(Into::into)
.unwrap_or_else(|_| nu_path::home_dir().unwrap_or_default())
})
}
pub fn get_guaranteed_cwd(engine_state: &EngineState, stack: &Stack) -> PathBuf {
nu_engine::env::current_dir(engine_state, stack).unwrap_or_else(|e| {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &e);
crate::util::get_init_cwd()
})
}
type MakeRangeError = fn(&str, Span) -> ShellError;
pub fn process_range(range: &Range) -> Result<(isize, isize), MakeRangeError> {
let start = match &range.from {
Fix process_range on 32-bit platforms (#8842) # Description This PR fixes `commands::str_::substrings_the_input_and_treats_end_index_as_length_if_blank_end_index_given` testcase on 32-bit platform. ``` failures: ---- commands::str_::substrings_the_input_and_treats_end_index_as_length_if_blank_end_index_given stdout ---- === stderr thread 'commands::str_::substrings_the_input_and_treats_end_index_as_length_if_blank_end_index_given' panicked at 'assertion failed: `(left == right)` left: `"arepa"`, right: `"arepas"`', crates/nu-command/tests/commands/str_/mod.rs:363:9 failures: commands::str_::substrings_the_input_and_treats_end_index_as_length_if_blank_end_index_given test result: FAILED. 1072 passed; 1 failed; 23 ignored; 0 measured; 0 filtered out; finished in 2.98s error: test failed, to rerun pass `-p nu-command --test main` ``` https://gitlab.alpinelinux.org/nibon7/aports/-/jobs/1005935#L3864 https://gitlab.alpinelinux.org/nibon7/aports/-/jobs/1005931#L3867 # User-Facing Changes N/A # Tests + Formatting Don't forget to add tests that cover your changes. 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-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
2023-04-10 18:52:42 +00:00
Value::Int { val, .. } => isize::try_from(*val).unwrap_or_default(),
Value::Nothing { .. } => 0,
_ => {
return Err(|msg, span| ShellError::TypeMismatch {
err_message: msg.to_string(),
span,
})
}
};
let end = match &range.to {
Value::Int { val, .. } => {
if matches!(range.inclusion, RangeInclusion::Inclusive) {
Fix process_range on 32-bit platforms (#8842) # Description This PR fixes `commands::str_::substrings_the_input_and_treats_end_index_as_length_if_blank_end_index_given` testcase on 32-bit platform. ``` failures: ---- commands::str_::substrings_the_input_and_treats_end_index_as_length_if_blank_end_index_given stdout ---- === stderr thread 'commands::str_::substrings_the_input_and_treats_end_index_as_length_if_blank_end_index_given' panicked at 'assertion failed: `(left == right)` left: `"arepa"`, right: `"arepas"`', crates/nu-command/tests/commands/str_/mod.rs:363:9 failures: commands::str_::substrings_the_input_and_treats_end_index_as_length_if_blank_end_index_given test result: FAILED. 1072 passed; 1 failed; 23 ignored; 0 measured; 0 filtered out; finished in 2.98s error: test failed, to rerun pass `-p nu-command --test main` ``` https://gitlab.alpinelinux.org/nibon7/aports/-/jobs/1005935#L3864 https://gitlab.alpinelinux.org/nibon7/aports/-/jobs/1005931#L3867 # User-Facing Changes N/A # Tests + Formatting Don't forget to add tests that cover your changes. 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-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
2023-04-10 18:52:42 +00:00
isize::try_from(*val).unwrap_or(isize::max_value())
} else {
Fix process_range on 32-bit platforms (#8842) # Description This PR fixes `commands::str_::substrings_the_input_and_treats_end_index_as_length_if_blank_end_index_given` testcase on 32-bit platform. ``` failures: ---- commands::str_::substrings_the_input_and_treats_end_index_as_length_if_blank_end_index_given stdout ---- === stderr thread 'commands::str_::substrings_the_input_and_treats_end_index_as_length_if_blank_end_index_given' panicked at 'assertion failed: `(left == right)` left: `"arepa"`, right: `"arepas"`', crates/nu-command/tests/commands/str_/mod.rs:363:9 failures: commands::str_::substrings_the_input_and_treats_end_index_as_length_if_blank_end_index_given test result: FAILED. 1072 passed; 1 failed; 23 ignored; 0 measured; 0 filtered out; finished in 2.98s error: test failed, to rerun pass `-p nu-command --test main` ``` https://gitlab.alpinelinux.org/nibon7/aports/-/jobs/1005935#L3864 https://gitlab.alpinelinux.org/nibon7/aports/-/jobs/1005931#L3867 # User-Facing Changes N/A # Tests + Formatting Don't forget to add tests that cover your changes. 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-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
2023-04-10 18:52:42 +00:00
isize::try_from(*val).unwrap_or(isize::max_value()) - 1
}
}
Value::Nothing { .. } => isize::max_value(),
_ => {
return Err(|msg, span| ShellError::TypeMismatch {
err_message: msg.to_string(),
span,
})
}
};
Ok((start, end))
}