nushell/crates/nu-command/src/conversions/into/filesize.rs
Fernando Herrera b35914bd17
Category option for signature (#343)
* category option for signature

* category option for signature

* column description for $scope
2021-11-17 17:22:37 +13:00

151 lines
4.2 KiB
Rust

use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
};
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"into filesize"
}
fn signature(&self) -> Signature {
Signature::build("into filesize")
.rest(
"rest",
SyntaxShape::CellPath,
"column paths to convert to filesize (for table input)",
)
.category(Category::Conversions)
}
fn usage(&self) -> &str {
"Convert value to filesize"
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
into_filesize(engine_state, stack, call, input)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Convert string to filesize in table",
example: "[[bytes]; ['5'] [3.2] [4] [2kb]] | into filesize bytes",
result: None,
},
Example {
description: "Convert string to filesize",
example: "'2' | into filesize",
result: Some(Value::Filesize {
val: 2,
span: Span::unknown(),
}),
},
Example {
description: "Convert decimal to filesize",
example: "8.3 | into filesize",
result: Some(Value::Filesize {
val: 8,
span: Span::unknown(),
}),
},
Example {
description: "Convert int to filesize",
example: "5 | into filesize",
result: Some(Value::Filesize {
val: 5,
span: Span::unknown(),
}),
},
Example {
description: "Convert file size to filesize",
example: "4KB | into filesize",
result: Some(Value::Filesize {
val: 4000,
span: Span::unknown(),
}),
},
]
}
}
fn into_filesize(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let head = call.head;
let column_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
input.map(
move |v| {
if column_paths.is_empty() {
action(&v, head)
} else {
let mut ret = v;
for path in &column_paths {
let r =
ret.update_cell_path(&path.members, Box::new(move |old| action(old, head)));
if let Err(error) = r {
return Value::Error { error };
}
}
ret
}
},
engine_state.ctrlc.clone(),
)
}
pub fn action(input: &Value, span: Span) -> Value {
match input {
Value::Filesize { .. } => input.clone(),
Value::Int { val, .. } => Value::Filesize { val: *val, span },
Value::Float { val, .. } => Value::Filesize {
val: *val as i64,
span,
},
Value::String { val, .. } => match int_from_string(val, span) {
Ok(val) => Value::Filesize { val, span },
Err(error) => Value::Error { error },
},
_ => Value::Error {
error: ShellError::UnsupportedInput(
"'into filesize' for unsupported type".into(),
span,
),
},
}
}
fn int_from_string(a_string: &str, span: Span) -> Result<i64, ShellError> {
match a_string.parse::<bytesize::ByteSize>() {
Ok(n) => Ok(n.0 as i64),
Err(_) => Err(ShellError::CantConvert("int".into(), "string".into(), span)),
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}