clash_brush_core/
functions.rs1use std::{collections::HashMap, sync::Arc};
4
5#[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 pub fn get(&self, name: &str) -> Option<&Registration> {
19 self.functions.get(name)
20 }
21
22 pub fn get_mut(&mut self, name: &str) -> Option<&mut Registration> {
29 self.functions.get_mut(name)
30 }
31
32 pub fn remove(&mut self, name: &str) -> Option<Registration> {
38 self.functions.remove(name)
39 }
40
41 pub fn update(&mut self, name: String, registration: Registration) {
48 self.functions.insert(name, registration);
49 }
50
51 pub fn clear(&mut self) {
53 self.functions.clear();
54 }
55
56 pub fn iter(&self) -> impl Iterator<Item = (&String, &Registration)> {
58 self.functions.iter()
59 }
60}
61
62#[derive(Clone, Debug)]
64#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
65pub struct Registration {
66 definition: Arc<brush_parser::ast::FunctionDefinition>,
68 source_info: crate::SourceInfo,
70 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 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 pub fn definition(&self) -> &brush_parser::ast::FunctionDefinition {
104 &self.definition
105 }
106
107 pub const fn source(&self) -> &crate::SourceInfo {
109 &self.source_info
110 }
111
112 pub const fn export(&mut self) {
114 self.exported = true;
115 }
116
117 pub const fn unexport(&mut self) {
119 self.exported = false;
120 }
121
122 pub const fn is_exported(&self) -> bool {
124 self.exported
125 }
126}