mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 08:23:24 +01:00
support plus sign for "into filesize" (#12974)
# Description Fixes https://github.com/nushell/nushell/issues/12968. After apply this patch, we can use explict plus sign character included string with `into filesize` cmd. # User-Facing Changes AS-IS (before fixing) ``` $ "+8 KiB" | into filesize Error: nu:🐚:cant_convert × Can't convert to int. ╭─[entry #31:1:1] 1 │ "+8 KiB" | into filesize · ────┬─── · ╰── can't convert string to int ╰──── ``` TO-BE (after fixing) ``` $ "+8KiB" | into filesize 8.0 KiB ``` # Tests + Formatting Added a test # 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. -->
This commit is contained in:
parent
7746e84199
commit
3f0db11ae5
@ -138,6 +138,7 @@ pub fn action(input: &Value, _args: &CellPathOnlyArgs, span: Span) -> Value {
|
|||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn int_from_string(a_string: &str, span: Span) -> Result<i64, ShellError> {
|
fn int_from_string(a_string: &str, span: Span) -> Result<i64, ShellError> {
|
||||||
// Get the Locale so we know what the thousands separator is
|
// Get the Locale so we know what the thousands separator is
|
||||||
let locale = get_system_locale();
|
let locale = get_system_locale();
|
||||||
@ -148,26 +149,32 @@ fn int_from_string(a_string: &str, span: Span) -> Result<i64, ShellError> {
|
|||||||
let clean_string = no_comma_string.trim();
|
let clean_string = no_comma_string.trim();
|
||||||
|
|
||||||
// Hadle negative file size
|
// Hadle negative file size
|
||||||
if let Some(stripped_string) = clean_string.strip_prefix('-') {
|
if let Some(stripped_negative_string) = clean_string.strip_prefix('-') {
|
||||||
match stripped_string.parse::<bytesize::ByteSize>() {
|
match stripped_negative_string.parse::<bytesize::ByteSize>() {
|
||||||
Ok(n) => Ok(-(n.as_u64() as i64)),
|
Ok(n) => Ok(-(n.as_u64() as i64)),
|
||||||
Err(_) => Err(ShellError::CantConvert {
|
Err(_) => Err(string_convert_error(span)),
|
||||||
to_type: "int".into(),
|
}
|
||||||
from_type: "string".into(),
|
} else if let Some(stripped_positive_string) = clean_string.strip_prefix('+') {
|
||||||
span,
|
match stripped_positive_string.parse::<bytesize::ByteSize>() {
|
||||||
help: None,
|
Ok(n) if stripped_positive_string.starts_with(|c: char| c.is_ascii_digit()) => {
|
||||||
}),
|
Ok(n.as_u64() as i64)
|
||||||
|
}
|
||||||
|
_ => Err(string_convert_error(span)),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
match clean_string.parse::<bytesize::ByteSize>() {
|
match clean_string.parse::<bytesize::ByteSize>() {
|
||||||
Ok(n) => Ok(n.0 as i64),
|
Ok(n) => Ok(n.as_u64() as i64),
|
||||||
Err(_) => Err(ShellError::CantConvert {
|
Err(_) => Err(string_convert_error(span)),
|
||||||
to_type: "int".into(),
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn string_convert_error(span: Span) -> ShellError {
|
||||||
|
ShellError::CantConvert {
|
||||||
|
to_type: "filesize".into(),
|
||||||
from_type: "string".into(),
|
from_type: "string".into(),
|
||||||
span,
|
span,
|
||||||
help: None,
|
help: None,
|
||||||
}),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,8 +63,57 @@ fn into_filesize_negative_filesize() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn into_negative_filesize() {
|
fn into_filesize_negative_str_filesize() {
|
||||||
|
let actual = nu!("'-3kib' | into filesize");
|
||||||
|
|
||||||
|
assert!(actual.out.contains("-3.0 KiB"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn into_filesize_wrong_negative_str_filesize() {
|
||||||
|
let actual = nu!("'--3kib' | into filesize");
|
||||||
|
|
||||||
|
assert!(actual.err.contains("can't convert string to filesize"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn into_filesize_negative_str() {
|
||||||
let actual = nu!("'-1' | into filesize");
|
let actual = nu!("'-1' | into filesize");
|
||||||
|
|
||||||
assert!(actual.out.contains("-1 B"));
|
assert!(actual.out.contains("-1 B"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn into_filesize_wrong_negative_str() {
|
||||||
|
let actual = nu!("'--1' | into filesize");
|
||||||
|
|
||||||
|
assert!(actual.err.contains("can't convert string to filesize"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn into_filesize_positive_str_filesize() {
|
||||||
|
let actual = nu!("'+1Kib' | into filesize");
|
||||||
|
|
||||||
|
assert!(actual.out.contains("1.0 KiB"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn into_filesize_wrong_positive_str_filesize() {
|
||||||
|
let actual = nu!("'++1Kib' | into filesize");
|
||||||
|
|
||||||
|
assert!(actual.err.contains("can't convert string to filesize"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn into_filesize_positive_str() {
|
||||||
|
let actual = nu!("'+1' | into filesize");
|
||||||
|
|
||||||
|
assert!(actual.out.contains("1 B"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn into_filesize_wrong_positive_str() {
|
||||||
|
let actual = nu!("'++1' | into filesize");
|
||||||
|
|
||||||
|
assert!(actual.err.contains("can't convert string to filesize"));
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user