brush_core/functions.rs
1//! Structures for managing function registrations and calls.
2
3use std::{collections::HashMap, sync::Arc};
4
5/// An environment for defined, named functions.
6#[derive(Clone, Default)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct FunctionEnv {
9 functions: HashMap<String, Registration>,
10}
11
12impl FunctionEnv {
13 /// Tries to retrieve the registration for a function by name.
14 ///
15 /// # Arguments
16 ///
17 /// * `name` - The name of the function to retrieve.
18 pub fn get(&self, name: &str) -> Option<&Registration> {
19 self.functions.get(name)
20 }
21
22 /// Tries to retrieve a mutable reference to the registration for a
23 /// function by name.
24 ///
25 /// # Arguments
26 ///
27 /// * `name` - The name of the function to retrieve.
28 pub fn get_mut(&mut self, name: &str) -> Option<&mut Registration> {
29 self.functions.get_mut(name)
30 }
31
32 /// Unregisters a function from the environment.
33 ///
34 /// # Arguments
35 ///
36 /// * `name` - The name of the function to remove.
37 pub fn remove(&mut self, name: &str) -> Option<Registration> {
38 self.functions.remove(name)
39 }
40
41 /// Updates a function registration in this environment.
42 ///
43 /// # Arguments
44 ///
45 /// * `name` - The name of the function to update.
46 /// * `registration` - The new registration for the function.
47 pub fn update(&mut self, name: String, registration: Registration) {
48 self.functions.insert(name, registration);
49 }
50
51 /// Clear all functions in this environment.
52 pub fn clear(&mut self) {
53 self.functions.clear();
54 }
55
56 /// Returns an iterator over the functions registered in this environment.
57 pub fn iter(&self) -> impl Iterator<Item = (&String, &Registration)> {
58 self.functions.iter()
59 }
60}
61
62/// Encapsulates a registration for a defined function.
63#[derive(Clone, Debug)]
64#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
65pub struct Registration {
66 /// The parsed definition of the function.
67 definition: Arc<brush_parser::ast::FunctionDefinition>,
68 /// The source info for the function definition.
69 source_info: crate::SourceInfo,
70 /// Whether or not this function definition should be exported to children.
71 exported: bool,
72}
73
74impl From<brush_parser::ast::FunctionDefinition> for Registration {
75 fn from(definition: brush_parser::ast::FunctionDefinition) -> Self {
76 Self {
77 definition: Arc::new(definition),
78 source_info: crate::SourceInfo::default(),
79 exported: false,
80 }
81 }
82}
83
84impl Registration {
85 /// Creates a new function registration.
86 ///
87 /// # Arguments
88 ///
89 /// * `definition` - The function definition.
90 /// * `source_info` - Source information for the function definition.
91 pub fn new(
92 definition: brush_parser::ast::FunctionDefinition,
93 source_info: &crate::SourceInfo,
94 ) -> Self {
95 Self {
96 definition: Arc::new(definition),
97 source_info: source_info.clone(),
98 exported: false,
99 }
100 }
101
102 /// Returns a reference to the function definition.
103 pub fn definition(&self) -> &brush_parser::ast::FunctionDefinition {
104 &self.definition
105 }
106
107 /// Returns a reference to the source info for the function definition.
108 pub const fn source(&self) -> &crate::SourceInfo {
109 &self.source_info
110 }
111
112 /// Marks the function for export.
113 pub const fn export(&mut self) {
114 self.exported = true;
115 }
116
117 /// Unmarks the function for export.
118 pub const fn unexport(&mut self) {
119 self.exported = false;
120 }
121
122 /// Returns whether this function is exported.
123 pub const fn is_exported(&self) -> bool {
124 self.exported
125 }
126}