Skip to main content

xs/nu/commands/
get_command.rs

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