nushell/crates/nu-command/src/length.rs

54 lines
1.3 KiB
Rust
Raw Normal View History

2021-09-03 02:15:01 +00:00
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, Value};
pub struct Length;
impl Command for Length {
fn name(&self) -> &str {
"length"
}
fn usage(&self) -> &str {
"Count the number of elements in the input."
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("length")
}
fn run(
&self,
_context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
match input {
Value::List { vals: val, .. } => {
2021-09-04 06:52:28 +00:00
let length = val.len();
Ok(Value::Int {
val: length as i64,
span: call.head,
})
}
2021-09-07 07:35:59 +00:00
Value::Stream { stream, .. } => {
2021-09-04 06:52:28 +00:00
let length = stream.count();
Ok(Value::Int {
val: length as i64,
span: call.head,
})
}
2021-09-03 02:57:18 +00:00
Value::Nothing { .. } => Ok(Value::Int {
val: 0,
span: call.head,
}),
2021-09-03 02:15:01 +00:00
_ => Ok(Value::Int {
val: 1,
span: call.head,
}),
}
}
}