xs/nu/commands/
get_command.rs1use nu_engine::CallExt;
2use nu_protocol::engine::{Call, Command, EngineState, Stack};
3use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape, Type};
4
5use crate::nu::util;
6use crate::store::Store;
7
8#[derive(Clone)]
9pub struct GetCommand {
10 store: Store,
11}
12
13impl GetCommand {
14 pub fn new(store: Store) -> Self {
15 Self { store }
16 }
17}
18
19impl Command for GetCommand {
20 fn name(&self) -> &str {
21 ".get"
22 }
23
24 fn signature(&self) -> Signature {
25 Signature::build(".get")
26 .input_output_types(vec![(Type::Nothing, Type::Any)])
27 .required("id", SyntaxShape::String, "The ID of the frame to retrieve")
28 .switch(
29 "with-timestamp",
30 "include timestamp extracted from frame ID",
31 None,
32 )
33 .category(Category::Experimental)
34 }
35
36 fn description(&self) -> &str {
37 "Retrieves a frame by its ID from the store"
38 }
39
40 fn run(
41 &self,
42 engine_state: &EngineState,
43 stack: &mut Stack,
44 call: &Call,
45 _input: PipelineData,
46 ) -> Result<PipelineData, ShellError> {
47 let id_str: String = call.req(engine_state, stack, 0)?;
48 let with_timestamp = call.has_flag(engine_state, stack, "with-timestamp")?;
49 let id = id_str.parse().map_err(|e| ShellError::TypeMismatch {
50 err_message: format!("Invalid ID format: {e}"),
51 span: call.span(),
52 })?;
53
54 let store = self.store.clone();
55
56 if let Some(frame) = store.get(&id) {
57 Ok(PipelineData::Value(
58 util::frame_to_value(&frame, call.head, with_timestamp),
59 None,
60 ))
61 } else {
62 Err(ShellError::GenericError {
63 error: "Frame not found".into(),
64 msg: format!("No frame found with ID: {id_str}"),
65 span: Some(call.head),
66 help: None,
67 inner: vec![],
68 })
69 }
70 }
71}