nushell/src/commands/first.rs

35 lines
819 B
Rust
Raw Normal View History

2019-05-15 18:12:38 +02:00
use crate::errors::ShellError;
use crate::prelude::*;
2019-05-16 02:21:46 +02:00
// TODO: "Amount remaining" wrapper
2019-05-15 18:12:38 +02:00
2019-06-02 20:53:30 +02:00
pub fn first(args: CommandArgs) -> Result<OutputStream, ShellError> {
2019-06-08 00:35:07 +02:00
if args.positional.len() == 0 {
2019-06-15 20:36:17 +02:00
return Err(ShellError::maybe_labeled_error(
"First requires an amount",
"needs parameter",
args.name_span,
));
2019-06-08 00:35:07 +02:00
}
let amount = args.positional[0].as_i64();
let amount = match amount {
Ok(o) => o,
Err(_) => {
return Err(ShellError::labeled_error(
"Value is not a number",
"expected integer",
args.positional[0].span,
))
}
};
2019-05-16 02:21:46 +02:00
let input = args.input;
2019-05-15 18:12:38 +02:00
Ok(input
.take(amount as u64)
2019-05-22 09:12:03 +02:00
.map(|v| ReturnValue::Value(v))
.boxed())
2019-05-15 18:12:38 +02:00
}