intelli_shell/process/
completion_delete.rs

1use color_eyre::Result;
2use tokio_util::sync::CancellationToken;
3
4use super::{Process, ProcessOutput};
5use crate::{
6    cli::CompletionDeleteProcess, config::Config, errors::AppError, format_error, format_msg,
7    service::IntelliShellService,
8};
9
10impl Process for CompletionDeleteProcess {
11    async fn execute(
12        self,
13        config: Config,
14        service: IntelliShellService,
15        _cancellation_token: CancellationToken,
16    ) -> Result<ProcessOutput> {
17        let root_cmd = self.command.as_deref().unwrap_or_default();
18        let variable_name = &self.variable;
19        match service.delete_variable_completion_by_key(root_cmd, variable_name).await {
20            Ok(None) if root_cmd.trim().is_empty() => Ok(ProcessOutput::fail().stderr(format_error!(
21                config.theme,
22                "Completion for global {} variable not found",
23                config.theme.secondary.apply(variable_name),
24            ))),
25            Ok(None) => Ok(ProcessOutput::fail().stderr(format_error!(
26                config.theme,
27                "Completion for {} variable within {} commands not found",
28                config.theme.secondary.apply(variable_name),
29                config.theme.secondary.apply(&root_cmd),
30            ))),
31            Ok(Some(c)) if c.is_global() => Ok(ProcessOutput::success().stderr(format_msg!(
32                config.theme,
33                "Completion for global {} variable deleted",
34                config.theme.secondary.apply(&c.variable),
35            ))),
36            Ok(Some(c)) => Ok(ProcessOutput::success().stderr(format_msg!(
37                config.theme,
38                "Completion for {} variable within {} commands deleted",
39                config.theme.secondary.apply(&c.variable),
40                config.theme.secondary.apply(&c.root_cmd),
41            ))),
42            Err(AppError::UserFacing(err)) => Ok(ProcessOutput::fail().stderr(format_error!(config.theme, "{err}"))),
43            Err(AppError::Unexpected(report)) => Err(report),
44        }
45    }
46}