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