1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape, UntaggedValue};
use nu_source::Tagged;
pub struct First;
impl WholeStreamCommand for First {
fn name(&self) -> &str {
"first"
}
fn signature(&self) -> Signature {
Signature::build("first").optional(
"rows",
SyntaxShape::Int,
"starting from the front, the number of rows to return",
)
}
fn usage(&self) -> &str {
"Show only the first number of rows."
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
first(args)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Return the first item of a list/table",
example: "echo [1 2 3] | first",
result: Some(vec![UntaggedValue::int(1).into()]),
},
Example {
description: "Return the first 2 items of a list/table",
example: "echo [1 2 3] | first 2",
result: Some(vec![
UntaggedValue::int(1).into(),
UntaggedValue::int(2).into(),
]),
},
]
}
}
fn first(args: CommandArgs) -> Result<OutputStream, ShellError> {
let rows: Option<Tagged<usize>> = args.opt(0)?;
let tag = args.call_info.name_tag;
let mut rows_desired = if let Some(quantity) = rows {
*quantity
} else {
1
};
let mut input_peek = args.input.peekable();
match &mut input_peek.next_if(|val| val.is_binary()) {
Some(v) => match &v.value {
UntaggedValue::Primitive(_) => {
let bytes = match v.as_binary_vec() {
Ok(b) => b,
_ => {
return Err(ShellError::labeled_error(
"error converting data as_binary_vec",
"error conversion",
tag,
))
}
};
if bytes.len() >= rows_desired {
let output_bytes = bytes[0..rows_desired].to_vec();
Ok(OutputStream::one(UntaggedValue::binary(output_bytes)))
} else {
let mut bigger: Vec<u8> = vec![];
bigger.extend(bytes);
while bigger.len() < rows_desired {
match input_peek.next() {
Some(data) => match data.value.into_value(&tag).as_binary_vec() {
Ok(bits) => bigger.extend(bits),
_ => {
return Err(ShellError::labeled_error(
"error converting data as_binary_vec",
"error conversion",
tag,
))
}
},
_ => {
rows_desired = bigger.len();
break;
}
}
}
let output_bytes = bigger[0..rows_desired].to_vec();
Ok(OutputStream::one(UntaggedValue::binary(output_bytes)))
}
}
UntaggedValue::Row(_) => Ok(input_peek.take(rows_desired).into_output_stream()),
UntaggedValue::Table(_) => Err(ShellError::labeled_error(
"unsure how to handle UntaggedValue::Table",
"found table",
tag,
)),
UntaggedValue::Error(_) => Err(ShellError::labeled_error(
"unsure how to handle UntaggedValue::Error",
"found error",
tag,
)),
UntaggedValue::Block(_) => Err(ShellError::labeled_error(
"unsure how to handled UntaggedValue::Block",
"found block",
tag,
)),
#[cfg(all(not(target_arch = "wasm32"), feature = "dataframe"))]
UntaggedValue::DataFrame(_) | UntaggedValue::FrameStruct(_) => {
Err(ShellError::labeled_error(
"unsure how to handled dataframe struct",
"found dataframe",
tag,
))
}
},
None => Ok(input_peek.take(rows_desired).into_output_stream()),
}
}
#[cfg(test)]
mod tests {
use super::First;
use super::ShellError;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
test_examples(First {})
}
}