Skip to main content

shrub_core/shell/
builtin_registry.rs

1//! Builtin command management for shell instances.
2
3use std::collections::HashMap;
4
5use crate::{builtins, extensions};
6
7impl<SE: extensions::ShellExtensions> crate::Shell<SE> {
8    /// Register a builtin to the shell's environment, replacing any existing
9    /// registration with the same name.
10    ///
11    /// # Arguments
12    ///
13    /// * `name` - The in-shell name of the builtin.
14    /// * `registration` - The registration handle for the builtin.
15    pub fn register_builtin<S: Into<String>>(
16        &mut self,
17        name: S,
18        registration: builtins::Registration<SE>,
19    ) {
20        self.builtins.insert(name.into(), registration);
21    }
22
23    /// Register a builtin only if no builtin with that name is already
24    /// registered.
25    ///
26    /// # Arguments
27    ///
28    /// * `name` - The in-shell name of the builtin.
29    /// * `registration` - The registration handle for the builtin.
30    pub fn register_builtin_if_unset<S: Into<String>>(
31        &mut self,
32        name: S,
33        registration: builtins::Registration<SE>,
34    ) {
35        self.builtins.entry(name.into()).or_insert(registration);
36    }
37
38    /// Tries to retrieve a mutable reference to an existing builtin
39    /// registration. Returns `None` if no such registration exists.
40    ///
41    /// # Arguments
42    ///
43    /// * `name` - The name of the builtin to lookup.
44    pub fn builtin_mut(&mut self, name: &str) -> Option<&mut builtins::Registration<SE>> {
45        self.builtins.get_mut(name)
46    }
47
48    /// Returns the registered builtins for the shell.
49    pub const fn builtins(&self) -> &HashMap<String, builtins::Registration<SE>> {
50        &self.builtins
51    }
52}