xs/nu/commands/
get_command.rs

1use 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            .category(Category::Experimental)
29    }
30
31    fn description(&self) -> &str {
32        "Retrieves a frame by its ID from the store"
33    }
34
35    fn run(
36        &self,
37        engine_state: &EngineState,
38        stack: &mut Stack,
39        call: &Call,
40        _input: PipelineData,
41    ) -> Result<PipelineData, ShellError> {
42        let id_str: String = call.req(engine_state, stack, 0)?;
43        let id = id_str.parse().map_err(|e| ShellError::TypeMismatch {
44            err_message: format!("Invalid ID format: {e}"),
45            span: call.span(),
46        })?;
47
48        let store = self.store.clone();
49
50        if let Some(frame) = store.get(&id) {
51            Ok(PipelineData::Value(
52                util::frame_to_value(&frame, call.head),
53                None,
54            ))
55        } else {
56            Err(ShellError::GenericError {
57                error: "Frame not found".into(),
58                msg: format!("No frame found with ID: {id_str}"),
59                span: Some(call.head),
60                help: None,
61                inner: vec![],
62            })
63        }
64    }
65}