xs/nu/commands/
remove_command.rs

1use std::str::FromStr;
2
3use nu_engine::CallExt;
4use nu_protocol::engine::{Call, Command, EngineState, Stack};
5use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape, Type};
6
7use scru128::Scru128Id;
8
9use crate::store::Store;
10
11#[derive(Clone)]
12pub struct RemoveCommand {
13    store: Store,
14}
15
16impl RemoveCommand {
17    pub fn new(store: Store) -> Self {
18        Self { store }
19    }
20}
21
22impl Command for RemoveCommand {
23    fn name(&self) -> &str {
24        ".remove"
25    }
26
27    fn signature(&self) -> Signature {
28        Signature::build(".remove")
29            .input_output_types(vec![(Type::Nothing, Type::Nothing)])
30            .required("id", SyntaxShape::String, "The ID of the frame to remove")
31            .category(Category::Experimental)
32    }
33
34    fn description(&self) -> &str {
35        "Removes a frame from the store by its ID"
36    }
37
38    fn run(
39        &self,
40        engine_state: &EngineState,
41        stack: &mut Stack,
42        call: &Call,
43        _input: PipelineData,
44    ) -> Result<PipelineData, ShellError> {
45        let id_str: String = call.req(engine_state, stack, 0)?;
46        let id = Scru128Id::from_str(&id_str).map_err(|e| ShellError::TypeMismatch {
47            err_message: format!("Invalid ID format: {e}"),
48            span: call.span(),
49        })?;
50
51        let store = self.store.clone();
52
53        match store.remove(&id) {
54            Ok(()) => Ok(PipelineData::Empty),
55            Err(e) => Err(ShellError::GenericError {
56                error: "Failed to remove frame".into(),
57                msg: e.to_string(),
58                span: Some(call.head),
59                help: None,
60                inner: vec![],
61            }),
62        }
63    }
64}