GitRoot
Craft your forge, Build your project, Grow your community freely
1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
2//
3// SPDX-License-Identifier: MIT
4
5use crate::{
6 fs::FsBase,
7 model::{
8 Call, CallRes, Commit, Exec, ExecStatus, ForgeConf, HttpRequest, HttpResponse,
9 ReportToGitroot,
10 },
11 ptr::{ptr_to_string, string_to_ptr},
12};
13
14#[link(wasm_import_module = "gitroot")]
15unsafe extern "C" {
16 #[link_name = "forgeConf"]
17 pub unsafe fn _forge_conf() -> u64;
18 #[link_name = "modifyContent"]
19 pub unsafe fn _modify_content(
20 filepathPtr: u32,
21 filepathSize: u32,
22 contentPtr: u32,
23 contentSize: u32,
24 );
25 #[link_name = "modifyWebContent"]
26 pub unsafe fn _modify_web_content(
27 filepathPtr: u32,
28 filepathSize: u32,
29 contentPtr: u32,
30 contentSize: u32,
31 );
32 #[link_name = "replaceWebContent"]
33 pub unsafe fn _replace_web_content(
34 filepathPtr: u32,
35 filepathSize: u32,
36 oldContentPtr: u32,
37 oldContentSize: u32,
38 contentPtr: u32,
39 contentSize: u32,
40 );
41 #[link_name = "modifyCacheContent"]
42 pub unsafe fn _modify_cache_content(
43 filepathPtr: u32,
44 filepathSize: u32,
45 contentPtr: u32,
46 contentSize: u32,
47 );
48
49 #[link_name = "copyFile"]
50 pub unsafe fn _copy_file(
51 from_fs_ptr: u32,
52 from_fs_size: u32,
53 from_path_ptr: u32,
54 from_path_size: u32,
55 to_fs_ptr: u32,
56 to_fs_size: u32,
57 to_path_ptr: u32,
58 to_path_size: u32,
59 ) -> u64;
60
61 #[link_name = "deleteFile"]
62 pub unsafe fn _delete_file(
63 from_fs_ptr: u32,
64 from_fs_size: u32,
65 from_path_ptr: u32,
66 from_path_size: u32,
67 ) -> u64;
68
69 #[link_name = "moveFile"]
70 pub unsafe fn _move_file(
71 from_fs_ptr: u32,
72 from_fs_size: u32,
73 from_path_ptr: u32,
74 from_path_size: u32,
75 to_fs_ptr: u32,
76 to_fs_size: u32,
77 to_path_ptr: u32,
78 to_path_size: u32,
79 ) -> u64;
80
81 #[link_name = "replaceContent"]
82 pub unsafe fn _replace_content(
83 from_fs_ptr: u32,
84 from_fs_size: u32,
85 filepath_ptr: u32,
86 filepath_size: u32,
87 old_content_ptr: u32,
88 old_content_size: u32,
89 content_ptr: u32,
90 content_size: u32,
91 ) -> u64;
92
93 #[link_name = "writeContent"]
94 pub unsafe fn _write_content(
95 from_fs_ptr: u32,
96 from_fs_size: u32,
97 filepath_ptr: u32,
98 filepath_size: u32,
99 content_ptr: u32,
100 content_size: u32,
101 ) -> u64;
102 #[link_name = "commitAll"]
103 pub unsafe fn _commit_all(ptr: u32, size: u32);
104 #[link_name = "diffWithParent"]
105 pub unsafe fn _diff_with_parent(
106 hash_ptr: u32,
107 hash_size: u32,
108 oldfile_ptr: u32,
109 oldfile_size: u32,
110 newfile_ptr: u32,
111 newfile_size: u32,
112 ) -> u64;
113 #[link_name = "log"]
114 pub unsafe fn _log(ptr: u32, size: u32);
115 #[link_name = "logError"]
116 pub unsafe fn _log_error(messagePtr: u32, messageSize: u32, errPtr: u32, errSize: u32);
117 #[link_name = "merge"]
118 pub unsafe fn _merge(from_ptr: u32, from_size: u32, to_ptr: u32, to_size: u32);
119 #[link_name = "commits"]
120 pub unsafe fn _commits(from_ptr: u32, from_size: u32, to_ptr: u32, to_size: u32) -> u64;
121 #[link_name = "exec"]
122 pub unsafe fn _exec(cmd_ptr: u32, cmd_size: u32) -> u64;
123 #[link_name = "report"]
124 pub unsafe fn _report(report_ptr: u32, report_size: u32);
125 #[link_name = "canCall"]
126 pub unsafe fn _can_call(report_ptr: u32, report_size: u32) -> u32;
127 #[link_name = "call"]
128 pub unsafe fn _call(report_ptr: u32, report_size: u32) -> u64;
129 #[link_name = "httpClient"]
130 pub unsafe fn _http_client(ptr: u32, size: u32) -> u64;
131}
132
133pub(crate) fn forge_conf() -> Result<ForgeConf, String> {
134 unsafe {
135 let ptr_size: u64 = _forge_conf();
136 if ptr_size == 0 {
137 return Err(String::from("can't get forge_conf"));
138 }
139 let ptr: u32 = (ptr_size >> 32) as u32;
140 let size: u32 = ptr_size as u32;
141 Ok(serde_json::from_str(ptr_to_string(ptr, size).as_str())
142 .expect("JSON was not well-formatted"))
143 }
144}
145
146pub(crate) fn copy_file(
147 from_fs: FsBase,
148 from_path: String,
149 to_fs: FsBase,
150 to_path: String,
151) -> Result<(), String> {
152 let (from_fs_ptr, from_fs_size) = string_to_ptr(from_fs.as_str());
153 let (from_path_ptr, from_path_size) = string_to_ptr(&from_path);
154 let (to_fs_ptr, to_fs_size) = string_to_ptr(to_fs.as_str());
155 let (to_path_ptr, to_path_size) = string_to_ptr(&to_path);
156 unsafe {
157 let ptr_size: u64 = _copy_file(
158 from_fs_ptr,
159 from_fs_size,
160 from_path_ptr,
161 from_path_size,
162 to_fs_ptr,
163 to_fs_size,
164 to_path_ptr,
165 to_path_size,
166 );
167 if ptr_size == 0 {
168 return Ok(());
169 }
170 let ptr: u32 = (ptr_size >> 32) as u32;
171 let size: u32 = ptr_size as u32;
172 return Err(ptr_to_string(ptr, size));
173 }
174}
175
176pub(crate) fn delete_file(from_fs: FsBase, from_path: String) -> Result<(), String> {
177 let (from_fs_ptr, from_fs_size) = string_to_ptr(from_fs.as_str());
178 let (from_path_ptr, from_path_size) = string_to_ptr(&from_path);
179 unsafe {
180 let ptr_size: u64 = _delete_file(from_fs_ptr, from_fs_size, from_path_ptr, from_path_size);
181 if ptr_size == 0 {
182 return Ok(());
183 }
184 let ptr: u32 = (ptr_size >> 32) as u32;
185 let size: u32 = ptr_size as u32;
186 return Err(ptr_to_string(ptr, size));
187 }
188}
189
190pub(crate) fn move_file(
191 from_fs: FsBase,
192 from_path: String,
193 to_fs: FsBase,
194 to_path: String,
195) -> Result<(), String> {
196 let (from_fs_ptr, from_fs_size) = string_to_ptr(from_fs.as_str());
197 let (from_path_ptr, from_path_size) = string_to_ptr(&from_path);
198 let (to_fs_ptr, to_fs_size) = string_to_ptr(to_fs.as_str());
199 let (to_path_ptr, to_path_size) = string_to_ptr(&to_path);
200 unsafe {
201 let ptr_size: u64 = _move_file(
202 from_fs_ptr,
203 from_fs_size,
204 from_path_ptr,
205 from_path_size,
206 to_fs_ptr,
207 to_fs_size,
208 to_path_ptr,
209 to_path_size,
210 );
211 if ptr_size == 0 {
212 return Ok(());
213 }
214 let ptr: u32 = (ptr_size >> 32) as u32;
215 let size: u32 = ptr_size as u32;
216 return Err(ptr_to_string(ptr, size));
217 }
218}
219
220pub(crate) fn replace_content(
221 from_fs: FsBase,
222 filepath: String,
223 old_content: String,
224 content: String,
225) -> Result<(), String> {
226 let (from_fs_ptr, from_fs_size) = string_to_ptr(from_fs.as_str());
227 let (filepath_ptr, filepath_size) = string_to_ptr(&filepath);
228 let (old_content_ptr, old_content_size) = string_to_ptr(&old_content);
229 let (content_ptr, content_size) = string_to_ptr(&content);
230 unsafe {
231 let ptr_size: u64 = _replace_content(
232 from_fs_ptr,
233 from_fs_size,
234 filepath_ptr,
235 filepath_size,
236 old_content_ptr,
237 old_content_size,
238 content_ptr,
239 content_size,
240 );
241 if ptr_size == 0 {
242 return Ok(());
243 }
244 let ptr: u32 = (ptr_size >> 32) as u32;
245 let size: u32 = ptr_size as u32;
246 return Err(ptr_to_string(ptr, size));
247 }
248}
249
250pub(crate) fn write_content(
251 from_fs: FsBase,
252 filepath: String,
253 content: String,
254) -> Result<(), String> {
255 let (from_fs_ptr, from_fs_size) = string_to_ptr(from_fs.as_str());
256 let (filepath_ptr, filepath_size) = string_to_ptr(&filepath);
257 let (content_ptr, content_size) = string_to_ptr(&content);
258 unsafe {
259 let ptr_size: u64 = _write_content(
260 from_fs_ptr,
261 from_fs_size,
262 filepath_ptr,
263 filepath_size,
264 content_ptr,
265 content_size,
266 );
267 if ptr_size == 0 {
268 return Ok(());
269 }
270 let ptr: u32 = (ptr_size >> 32) as u32;
271 let size: u32 = ptr_size as u32;
272 return Err(ptr_to_string(ptr, size));
273 }
274}
275
276pub(crate) fn commit_all(message: String) {
277 let (msg_ptr, msg_size) = string_to_ptr(&message);
278 unsafe {
279 _commit_all(msg_ptr, msg_size);
280 }
281}
282
283pub(crate) fn diff_with_parent(
284 hash: String,
285 oldfile: String,
286 newfile: String,
287) -> Result<String, String> {
288 let (hash_ptr, hash_size) = string_to_ptr(&hash);
289 let (oldfile_ptr, oldfile_size) = string_to_ptr(&oldfile);
290 let (newfile_ptr, newfile_size) = string_to_ptr(&newfile);
291 unsafe {
292 let ptr_size = _diff_with_parent(
293 hash_ptr,
294 hash_size,
295 oldfile_ptr,
296 oldfile_size,
297 newfile_ptr,
298 newfile_size,
299 );
300 if ptr_size == 0 {
301 return Err(String::from("can't get diff_with_parent"));
302 }
303 let ptr: u32 = (ptr_size >> 32) as u32;
304 let size: u32 = ptr_size as u32;
305 Ok(ptr_to_string(ptr, size))
306 }
307}
308
309pub(crate) fn log(message: &str) {
310 let (msg_ptr, msg_size) = string_to_ptr(message);
311 unsafe {
312 _log(msg_ptr, msg_size);
313 }
314}
315
316pub(crate) fn log_error(message: &str, err: &str) {
317 let (msg_ptr, msg_size) = string_to_ptr(message);
318 let (err_ptr, err_size) = string_to_ptr(err);
319 unsafe {
320 _log_error(msg_ptr, msg_size, err_ptr, err_size);
321 }
322}
323
324pub(crate) fn merge(from: String, to: String) {
325 let (from_ptr, from_size) = string_to_ptr(&from);
326 let (to_ptr, to_size) = string_to_ptr(&to);
327 unsafe {
328 _merge(from_ptr, from_size, to_ptr, to_size);
329 }
330}
331
332pub(crate) fn commits(from: String, to: String) -> Result<Vec<Commit>, String> {
333 let (from_ptr, from_size) = string_to_ptr(&from);
334 let (to_ptr, to_size) = string_to_ptr(&to);
335 unsafe {
336 let ptr_size = _commits(from_ptr, from_size, to_ptr, to_size);
337 if ptr_size == 0 {
338 return Err(String::from("can't get commits"));
339 }
340 let ptr: u32 = (ptr_size >> 32) as u32;
341 let size: u32 = ptr_size as u32;
342 Ok(serde_json::from_str(ptr_to_string(ptr, size).as_str())
343 .expect("JSON was not well-formatted"))
344 }
345}
346
347pub(crate) fn exec(exec: &Exec) -> Result<ExecStatus, String> {
348 let exec_json = serde_json::to_string(&exec).unwrap();
349 let (cmd_ptr, cmd_size) = string_to_ptr(&exec_json);
350 unsafe {
351 let ptr_size = _exec(cmd_ptr, cmd_size);
352 if ptr_size == 0 {
353 return Err(String::from("can't exec"));
354 }
355 let ptr: u32 = (ptr_size >> 32) as u32;
356 let size: u32 = ptr_size as u32;
357 Ok(serde_json::from_str(ptr_to_string(ptr, size).as_str())
358 .expect("JSON was not well-formatted"))
359 }
360}
361
362pub(crate) fn report_to_gitroot(report: ReportToGitroot) {
363 let report_json = serde_json::to_string(&report).unwrap();
364 let (cmd_ptr, cmd_size) = string_to_ptr(&report_json);
365 unsafe {
366 _report(cmd_ptr, cmd_size);
367 }
368}
369
370pub(crate) fn can_call(call: &Call) -> u32 {
371 let call_json = serde_json::to_string(&call).unwrap();
372 let (ptr, size) = string_to_ptr(&call_json);
373 unsafe {
374 return _can_call(ptr, size);
375 }
376}
377
378pub(crate) fn call(call: &Call) -> Result<CallRes, String> {
379 let call_json = serde_json::to_string(&call).unwrap();
380 let (ptr, size) = string_to_ptr(&call_json);
381 unsafe {
382 let ptr_size = _call(ptr, size);
383 if ptr_size == 0 {
384 return Err(String::from("can't call"));
385 }
386 let ptr: u32 = (ptr_size >> 32) as u32;
387 let size: u32 = ptr_size as u32;
388 Ok(serde_json::from_str(ptr_to_string(ptr, size).as_str())
389 .expect("JSON was not well-formatted"))
390 }
391}
392
393pub(crate) fn http_client(request: &HttpRequest) -> Result<HttpResponse, String> {
394 let request_json = serde_json::to_string(&request).unwrap();
395 let (ptr, size) = string_to_ptr(&request_json);
396 unsafe {
397 let ptr_size = _http_client(ptr, size);
398 if ptr_size == 0 {
399 return Err(String::from("can't http"));
400 }
401 let ptr: u32 = (ptr_size >> 32) as u32;
402 let size: u32 = ptr_size as u32;
403 Ok(serde_json::from_str(ptr_to_string(ptr, size).as_str())
404 .expect("JSON was not well-formatted"))
405 }
406}