harn_hostlib/tools/
mod.rs1use harn_vm::VmDictExt;
38
39use harn_vm::VmValue;
40
41use crate::error::HostlibError;
42use crate::registry::{BuiltinRegistry, HostlibCapability};
43
44pub(crate) mod args;
45mod cancel_handle;
46mod diagnostics;
47mod file_io;
48mod git;
49mod inspect_test_results;
50mod lang;
51pub mod long_running;
52mod manage_packages;
53mod outline;
54mod payload;
55pub mod permissions;
56mod proc;
57mod read_command_output;
58mod response;
59mod run_build_command;
60mod run_command;
61mod run_test;
62mod search;
63mod test_parsers;
64mod toolchain_facts;
65mod wait_command;
66
67pub use permissions::FEATURE_TOOLS_DETERMINISTIC;
68
69#[derive(Default)]
71pub struct ToolsCapability;
72
73impl HostlibCapability for ToolsCapability {
74 fn module_name(&self) -> &'static str {
75 "tools"
76 }
77
78 fn register_builtins(&self, registry: &mut BuiltinRegistry) {
79 long_running::register_cleanup_hook();
82
83 registry.register_gated_fn("tools", "hostlib_tools_search", "search", search::run);
84 registry.register_gated_fn(
85 "tools",
86 "hostlib_tools_read_file",
87 "read_file",
88 file_io::read_file,
89 );
90 registry.register_gated_fn(
91 "tools",
92 "hostlib_tools_write_file",
93 "write_file",
94 file_io::write_file,
95 );
96 registry.register_gated_fn(
97 "tools",
98 "hostlib_tools_delete_file",
99 "delete_file",
100 file_io::delete_file,
101 );
102 registry.register_gated_fn(
103 "tools",
104 "hostlib_tools_list_directory",
105 "list_directory",
106 file_io::list_directory,
107 );
108 registry.register_gated_fn(
109 "tools",
110 "hostlib_tools_get_file_outline",
111 "get_file_outline",
112 outline::run,
113 );
114 registry.register_gated_fn("tools", "hostlib_tools_git", "git", git::run);
115
116 registry.register_gated_fn(
117 "tools",
118 "hostlib_tools_run_command",
119 "run_command",
120 run_command::handle,
121 );
122 registry.register_gated_fn(
123 "tools",
124 read_command_output::NAME,
125 "read_command_output",
126 read_command_output::handle,
127 );
128 registry.register_gated_fn(
129 "tools",
130 wait_command::NAME,
131 "wait_command",
132 wait_command::handle,
133 );
134 registry.register_gated_fn(
135 "tools",
136 "hostlib_tools_run_test",
137 "run_test",
138 run_test::handle,
139 );
140 registry.register_gated_fn(
141 "tools",
142 "hostlib_tools_run_build_command",
143 "run_build_command",
144 run_build_command::handle,
145 );
146 registry.register_gated_fn(
147 "tools",
148 "hostlib_tools_inspect_test_results",
149 "inspect_test_results",
150 inspect_test_results::handle,
151 );
152 registry.register_gated_fn(
153 "tools",
154 "hostlib_tools_manage_packages",
155 "manage_packages",
156 manage_packages::handle,
157 );
158 registry.register_gated_fn(
159 "tools",
160 cancel_handle::NAME,
161 "cancel_handle",
162 cancel_handle::handle,
163 );
164 registry.register_gated_fn(
165 "tools",
166 toolchain_facts::NAME,
167 "toolchain_facts",
168 toolchain_facts::handle,
169 );
170
171 registry.register_fn("tools", "hostlib_enable", "enable", handle_enable);
174 }
175}
176
177fn handle_enable(args: &[VmValue]) -> Result<VmValue, HostlibError> {
182 let feature = match args.first() {
183 Some(VmValue::String(s)) => s.to_string(),
184 Some(VmValue::Dict(dict)) => match dict.get("feature") {
185 Some(VmValue::String(s)) => s.to_string(),
186 _ => {
187 return Err(HostlibError::MissingParameter {
188 builtin: "hostlib_enable",
189 param: "feature",
190 });
191 }
192 },
193 _ => {
194 return Err(HostlibError::MissingParameter {
195 builtin: "hostlib_enable",
196 param: "feature",
197 });
198 }
199 };
200
201 match feature.as_str() {
202 permissions::FEATURE_TOOLS_DETERMINISTIC => {
203 let newly_enabled = permissions::enable(&feature);
204 let mut map: harn_vm::value::DictMap = harn_vm::value::DictMap::new();
205 map.put_str("feature", feature);
206 map.insert(harn_vm::value::intern_key("enabled"), VmValue::Bool(true));
207 map.insert(
208 harn_vm::value::intern_key("newly_enabled"),
209 VmValue::Bool(newly_enabled),
210 );
211 Ok(VmValue::dict(map))
212 }
213 other => Err(HostlibError::InvalidParameter {
214 builtin: "hostlib_enable",
215 param: "feature",
216 message: format!("unknown feature `{other}`; supported: [`tools:deterministic`]"),
217 }),
218 }
219}