Skip to main content

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