1use std::collections::HashMap;
4use std::sync::Arc;
5
6#[derive(Clone, Default)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct FunctionEnv {
10 functions: HashMap<String, Registration>,
11}
12
13impl FunctionEnv {
14 pub fn get(&self, name: &str) -> Option<&Registration> {
20 self.functions.get(name)
21 }
22
23 pub fn get_mut(&mut self, name: &str) -> Option<&mut Registration> {
30 self.functions.get_mut(name)
31 }
32
33 pub fn remove(&mut self, name: &str) -> Option<Registration> {
39 self.functions.remove(name)
40 }
41
42 pub fn update(&mut self, name: String, registration: Registration) {
49 self.functions.insert(name, registration);
50 }
51
52 pub fn clear(&mut self) {
54 self.functions.clear();
55 }
56
57 pub fn iter(&self) -> impl Iterator<Item = (&String, &Registration)> {
59 self.functions.iter()
60 }
61}
62
63#[derive(Clone, Debug)]
65#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
66pub struct Registration {
67 definition: Arc<shrub_parser::ast::FunctionDefinition>,
69 source_info: crate::SourceInfo,
71 exported: bool,
73}
74
75impl From<shrub_parser::ast::FunctionDefinition> for Registration {
76 fn from(definition: shrub_parser::ast::FunctionDefinition) -> Self {
77 Self {
78 definition: Arc::new(definition),
79 source_info: crate::SourceInfo::default(),
80 exported: false,
81 }
82 }
83}
84
85impl Registration {
86 pub fn new(
93 definition: shrub_parser::ast::FunctionDefinition,
94 source_info: &crate::SourceInfo,
95 ) -> Self {
96 Self {
97 definition: Arc::new(definition),
98 source_info: source_info.clone(),
99 exported: false,
100 }
101 }
102
103 pub fn definition(&self) -> &shrub_parser::ast::FunctionDefinition {
105 &self.definition
106 }
107
108 pub const fn source(&self) -> &crate::SourceInfo {
110 &self.source_info
111 }
112
113 pub const fn export(&mut self) {
115 self.exported = true;
116 }
117
118 pub const fn unexport(&mut self) {
120 self.exported = false;
121 }
122
123 pub const fn is_exported(&self) -> bool {
125 self.exported
126 }
127}