nushell/crates/nu-command/src/conversions/into/filesize.rs

183 lines
5.7 KiB
Rust
Raw Normal View History

2021-10-11 03:56:19 +02:00
use nu_protocol::{
ast::Call,
2021-10-25 18:58:58 +02:00
engine::{Command, EngineState, Stack},
2021-10-25 06:01:02 +02:00
Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
2021-10-11 03:56:19 +02:00
};
2021-10-25 06:01:02 +02:00
#[derive(Clone)]
2021-10-11 03:56:19 +02:00
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)",
)
}
fn usage(&self) -> &str {
"Convert value to filesize"
}
fn run(
&self,
2021-10-25 08:31:39 +02:00
_engine_state: &EngineState,
_stack: &mut Stack,
2021-10-11 03:56:19 +02:00
call: &Call,
2021-10-25 06:01:02 +02:00
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
2021-10-25 08:31:39 +02:00
into_filesize(call, input)
2021-10-11 03:56:19 +02:00
}
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: Some(Value::List {
// vals: vec![
// Value::Record {
// cols: vec!["bytes".to_string()],
// vals: vec![Value::Filesize {
// val: 5,
// span: Span::unknown(),
// }],
// span: Span::unknown(),
// },
// Value::Record {
// cols: vec!["bytes".to_string()],
// vals: vec![Value::Filesize {
// val: 3,
// span: Span::unknown(),
// }],
// span: Span::unknown(),
// },
// Value::Record {
// cols: vec!["bytes".to_string()],
// vals: vec![Value::Filesize {
// val: 4,
// span: Span::unknown(),
// }],
// span: Span::unknown(),
// },
// Value::Record {
// cols: vec!["bytes".to_string()],
// vals: vec![Value::Filesize {
// val: 2000,
// span: Span::unknown(),
// }],
// span: Span::unknown(),
// },
// ],
// span: Span::unknown(),
// }),
// },
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(
call: &Call,
2021-10-25 06:01:02 +02:00
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
2021-10-11 03:56:19 +02:00
let head = call.head;
// let call_paths: Vec<ColumnPath> = args.rest(0)?;
2021-10-25 06:01:02 +02:00
Ok(input
.map(move |v| {
action(v, head)
2021-10-11 03:56:19 +02:00
2021-10-25 06:01:02 +02:00
// FIXME: Add back cell_path support
// if column_paths.is_empty() {
// action(&v, v.tag())
// } else {
// let mut ret = v;
// for path in &column_paths {
// ret = ret.swap_data_by_column_path(
// path,
// Box::new(move |old| action(old, old.tag())),
// )?;
// }
2021-10-11 03:56:19 +02:00
2021-10-25 06:01:02 +02:00
// Ok(ret)
// }
})
.into_pipeline_data())
2021-10-11 03:56:19 +02:00
}
pub fn action(input: Value, span: Span) -> Value {
match input {
Value::Filesize { .. } => input,
Value::Int { val, .. } => Value::Filesize { 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),
2021-10-11 03:56:19 +02:00
Err(_) => Err(ShellError::CantConvert("int".into(), span)),
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}