brush_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 registered.
24 ///
25 /// # Arguments
26 ///
27 /// * `name` - The in-shell name of the builtin.
28 /// * `registration` - The registration handle for the builtin.
29 pub fn register_builtin_if_unset<S: Into<String>>(
30 &mut self,
31 name: S,
32 registration: builtins::Registration<SE>,
33 ) {
34 self.builtins.entry(name.into()).or_insert(registration);
35 }
36
37 /// Tries to retrieve a mutable reference to an existing builtin registration.
38 /// Returns `None` if no such registration exists.
39 ///
40 /// # Arguments
41 ///
42 /// * `name` - The name of the builtin to lookup.
43 pub fn builtin_mut(&mut self, name: &str) -> Option<&mut builtins::Registration<SE>> {
44 self.builtins.get_mut(name)
45 }
46
47 /// Returns the registered builtins for the shell.
48 pub const fn builtins(&self) -> &HashMap<String, builtins::Registration<SE>> {
49 &self.builtins
50 }
51}