mirror of
https://github.com/nushell/nushell.git
synced 2025-05-24 16:41:17 +00:00
# Description As part of the refactor to split spans off of Value, this moves to using helper functions to create values, and using `.span()` instead of matching span out of Value directly. Hoping to get a few more helping hands to finish this, as there are a lot of commands to update :) # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # 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` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` 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. --> --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com> Co-authored-by: WindSoilder <windsoilder@outlook.com>
111 lines
3.9 KiB
Rust
111 lines
3.9 KiB
Rust
use crate::{ShellError, Span, Value};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum Unit {
|
|
// Filesize units: metric
|
|
Byte,
|
|
Kilobyte,
|
|
Megabyte,
|
|
Gigabyte,
|
|
Terabyte,
|
|
Petabyte,
|
|
Exabyte,
|
|
|
|
// Filesize units: ISO/IEC 80000
|
|
Kibibyte,
|
|
Mebibyte,
|
|
Gibibyte,
|
|
Tebibyte,
|
|
Pebibyte,
|
|
Exbibyte,
|
|
|
|
// Duration units
|
|
Nanosecond,
|
|
Microsecond,
|
|
Millisecond,
|
|
Second,
|
|
Minute,
|
|
Hour,
|
|
Day,
|
|
Week,
|
|
}
|
|
|
|
impl Unit {
|
|
pub fn to_value(&self, size: i64, span: Span) -> Result<Value, ShellError> {
|
|
match self {
|
|
Unit::Byte => Ok(Value::filesize(size, span)),
|
|
Unit::Kilobyte => Ok(Value::filesize(size * 1000, span)),
|
|
Unit::Megabyte => Ok(Value::filesize(size * 1000 * 1000, span)),
|
|
Unit::Gigabyte => Ok(Value::filesize(size * 1000 * 1000 * 1000, span)),
|
|
Unit::Terabyte => Ok(Value::filesize(size * 1000 * 1000 * 1000 * 1000, span)),
|
|
Unit::Petabyte => Ok(Value::filesize(
|
|
size * 1000 * 1000 * 1000 * 1000 * 1000,
|
|
span,
|
|
)),
|
|
Unit::Exabyte => Ok(Value::filesize(
|
|
size * 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
|
|
span,
|
|
)),
|
|
|
|
Unit::Kibibyte => Ok(Value::filesize(size * 1024, span)),
|
|
Unit::Mebibyte => Ok(Value::filesize(size * 1024 * 1024, span)),
|
|
Unit::Gibibyte => Ok(Value::filesize(size * 1024 * 1024 * 1024, span)),
|
|
Unit::Tebibyte => Ok(Value::filesize(size * 1024 * 1024 * 1024 * 1024, span)),
|
|
Unit::Pebibyte => Ok(Value::filesize(
|
|
size * 1024 * 1024 * 1024 * 1024 * 1024,
|
|
span,
|
|
)),
|
|
Unit::Exbibyte => Ok(Value::filesize(
|
|
size * 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
|
|
span,
|
|
)),
|
|
|
|
Unit::Nanosecond => Ok(Value::duration(size, span)),
|
|
Unit::Microsecond => Ok(Value::duration(size * 1000, span)),
|
|
Unit::Millisecond => Ok(Value::duration(size * 1000 * 1000, span)),
|
|
Unit::Second => Ok(Value::duration(size * 1000 * 1000 * 1000, span)),
|
|
Unit::Minute => match size.checked_mul(1000 * 1000 * 1000 * 60) {
|
|
Some(val) => Ok(Value::duration(val, span)),
|
|
None => Err(ShellError::GenericError(
|
|
"duration too large".into(),
|
|
"duration too large".into(),
|
|
Some(span),
|
|
None,
|
|
Vec::new(),
|
|
)),
|
|
},
|
|
Unit::Hour => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60) {
|
|
Some(val) => Ok(Value::duration(val, span)),
|
|
None => Err(ShellError::GenericError(
|
|
"duration too large".into(),
|
|
"duration too large".into(),
|
|
Some(span),
|
|
None,
|
|
Vec::new(),
|
|
)),
|
|
},
|
|
Unit::Day => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60 * 24) {
|
|
Some(val) => Ok(Value::duration(val, span)),
|
|
None => Err(ShellError::GenericError(
|
|
"duration too large".into(),
|
|
"duration too large".into(),
|
|
Some(span),
|
|
None,
|
|
Vec::new(),
|
|
)),
|
|
},
|
|
Unit::Week => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60 * 24 * 7) {
|
|
Some(val) => Ok(Value::duration(val, span)),
|
|
None => Err(ShellError::GenericError(
|
|
"duration too large".into(),
|
|
"duration too large".into(),
|
|
Some(span),
|
|
None,
|
|
Vec::new(),
|
|
)),
|
|
},
|
|
}
|
|
}
|
|
}
|