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::{Call, CallRes, Commit, Exec, ExecStatus, ForgeConf, ReportToGitroot},
  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
141pub(crate) fn copy_file(
142    from_fs: FsBase,
143    from_path: String,
144    to_fs: FsBase,
145    to_path: String,
146) -> Result<(), String> {
147    let (from_fs_ptr, from_fs_size) = string_to_ptr(from_fs.as_str());
148    let (from_path_ptr, from_path_size) = string_to_ptr(&from_path);
149    let (to_fs_ptr, to_fs_size) = string_to_ptr(to_fs.as_str());
150    let (to_path_ptr, to_path_size) = string_to_ptr(&to_path);
151    unsafe {
152        let ptr_size: u64 = _copy_file(
153            from_fs_ptr,
154            from_fs_size,
155            from_path_ptr,
156            from_path_size,
157            to_fs_ptr,
158            to_fs_size,
159            to_path_ptr,
160            to_path_size,
161        );
162        if ptr_size == 0 {
163            return Ok(());
164        }
165        let ptr: u32 = (ptr_size >> 32) as u32;
166        let size: u32 = ptr_size as u32;
167        return Err(ptr_to_string(ptr, size));
168    }
169}
170
171pub(crate) fn delete_file(from_fs: FsBase, from_path: String) -> Result<(), String> {
172    let (from_fs_ptr, from_fs_size) = string_to_ptr(from_fs.as_str());
173    let (from_path_ptr, from_path_size) = string_to_ptr(&from_path);
174    unsafe {
175        let ptr_size: u64 = _delete_file(from_fs_ptr, from_fs_size, from_path_ptr, from_path_size);
176        if ptr_size == 0 {
177            return Ok(());
178        }
179        let ptr: u32 = (ptr_size >> 32) as u32;
180        let size: u32 = ptr_size as u32;
181        return Err(ptr_to_string(ptr, size));
182    }
183}
184
185pub(crate) fn move_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 = _move_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 replace_content(
216    from_fs: FsBase,
217    filepath: String,
218    old_content: String,
219    content: String,
220) -> Result<(), String> {
221    let (from_fs_ptr, from_fs_size) = string_to_ptr(from_fs.as_str());
222    let (filepath_ptr, filepath_size) = string_to_ptr(&filepath);
223    let (old_content_ptr, old_content_size) = string_to_ptr(&old_content);
224    let (content_ptr, content_size) = string_to_ptr(&content);
225    unsafe {
226        let ptr_size: u64 = _replace_content(
227            from_fs_ptr,
228            from_fs_size,
229            filepath_ptr,
230            filepath_size,
231            old_content_ptr,
232            old_content_size,
233            content_ptr,
234            content_size,
235        );
236        if ptr_size == 0 {
237            return Ok(());
238        }
239        let ptr: u32 = (ptr_size >> 32) as u32;
240        let size: u32 = ptr_size as u32;
241        return Err(ptr_to_string(ptr, size));
242    }
243}
244
245pub(crate) fn write_content(
246    from_fs: FsBase,
247    filepath: String,
248    content: String,
249) -> Result<(), String> {
250    let (from_fs_ptr, from_fs_size) = string_to_ptr(from_fs.as_str());
251    let (filepath_ptr, filepath_size) = string_to_ptr(&filepath);
252    let (content_ptr, content_size) = string_to_ptr(&content);
253    unsafe {
254        let ptr_size: u64 = _write_content(
255            from_fs_ptr,
256            from_fs_size,
257            filepath_ptr,
258            filepath_size,
259            content_ptr,
260            content_size,
261        );
262        if ptr_size == 0 {
263            return Ok(());
264        }
265        let ptr: u32 = (ptr_size >> 32) as u32;
266        let size: u32 = ptr_size as u32;
267        return Err(ptr_to_string(ptr, size));
268    }
269}
270
271pub(crate) fn commit_all(message: String) {
272    let (msg_ptr, msg_size) = string_to_ptr(&message);
273    unsafe {
274        _commit_all(msg_ptr, msg_size);
275    }
276}
277
278pub(crate) fn diff_with_parent(
279    hash: String,
280    oldfile: String,
281    newfile: String,
282) -> Result<String, String> {
283    let (hash_ptr, hash_size) = string_to_ptr(&hash);
284    let (oldfile_ptr, oldfile_size) = string_to_ptr(&oldfile);
285    let (newfile_ptr, newfile_size) = string_to_ptr(&newfile);
286    unsafe {
287        let ptr_size = _diff_with_parent(
288            hash_ptr,
289            hash_size,
290            oldfile_ptr,
291            oldfile_size,
292            newfile_ptr,
293            newfile_size,
294        );
295        if ptr_size == 0 {
296            return Err(String::from("can't get diff_with_parent"));
297        }
298        let ptr: u32 = (ptr_size >> 32) as u32;
299        let size: u32 = ptr_size as u32;
300        Ok(ptr_to_string(ptr, size))
301    }
302}
303
304pub(crate) fn log(message: &str) {
305    let (msg_ptr, msg_size) = string_to_ptr(message);
306    unsafe {
307        _log(msg_ptr, msg_size);
308    }
309}
310
311pub(crate) fn log_error(message: &str, err: &str) {
312    let (msg_ptr, msg_size) = string_to_ptr(message);
313    let (err_ptr, err_size) = string_to_ptr(err);
314    unsafe {
315        _log_error(msg_ptr, msg_size, err_ptr, err_size);
316    }
317}
318
319pub(crate) fn merge(from: String, to: String) {
320    let (from_ptr, from_size) = string_to_ptr(&from);
321    let (to_ptr, to_size) = string_to_ptr(&to);
322    unsafe {
323        _merge(from_ptr, from_size, to_ptr, to_size);
324    }
325}
326
327pub(crate) fn commits(from: String, to: String) -> Result<Vec<Commit>, String> {
328    let (from_ptr, from_size) = string_to_ptr(&from);
329    let (to_ptr, to_size) = string_to_ptr(&to);
330    unsafe {
331        let ptr_size = _commits(from_ptr, from_size, to_ptr, to_size);
332        if ptr_size == 0 {
333            return Err(String::from("can't get  commits"));
334        }
335        let ptr: u32 = (ptr_size >> 32) as u32;
336        let size: u32 = ptr_size as u32;
337        Ok(serde_json::from_str(ptr_to_string(ptr, size).as_str())
338            .expect("JSON was not well-formatted"))
339    }
340}
341
342pub(crate) fn exec(exec: &Exec) -> Result<ExecStatus, String> {
343    let exec_json = serde_json::to_string(&exec).unwrap();
344    let (cmd_ptr, cmd_size) = string_to_ptr(&exec_json);
345    unsafe {
346        let ptr_size = _exec(cmd_ptr, cmd_size);
347        if ptr_size == 0 {
348            return Err(String::from("can't exec"));
349        }
350        let ptr: u32 = (ptr_size >> 32) as u32;
351        let size: u32 = ptr_size as u32;
352        Ok(serde_json::from_str(ptr_to_string(ptr, size).as_str())
353            .expect("JSON was not well-formatted"))
354    }
355}
356
357pub(crate) fn report_to_gitroot(report: ReportToGitroot) {
358    let report_json = serde_json::to_string(&report).unwrap();
359    let (cmd_ptr, cmd_size) = string_to_ptr(&report_json);
360    unsafe {
361        _report(cmd_ptr, cmd_size);
362    }
363}
364
365pub(crate) fn can_call(call: &Call) -> u32 {
366    let call_json = serde_json::to_string(&call).unwrap();
367    let (ptr, size) = string_to_ptr(&call_json);
368    unsafe {
369        return _can_call(ptr, size);
370    }
371}
372
373pub(crate) fn call(call: &Call) -> Result<CallRes, String> {
374    let call_json = serde_json::to_string(&call).unwrap();
375    let (ptr, size) = string_to_ptr(&call_json);
376    unsafe {
377        let ptr_size = _call(ptr, size);
378        if ptr_size == 0 {
379            return Err(String::from("can't call"));
380        }
381        let ptr: u32 = (ptr_size >> 32) as u32;
382        let size: u32 = ptr_size as u32;
383        Ok(serde_json::from_str(ptr_to_string(ptr, size).as_str())
384            .expect("JSON was not well-formatted"))
385    }
386}