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    Exec,
  7    gitroot::{Commit, ForgeConf},
  8    ptr::{ptr_to_string, string_to_ptr},
  9};
 10
 11#[link(wasm_import_module = "gitroot")]
 12unsafe extern "C" {
 13    #[link_name = "forgeConf"]
 14    unsafe fn _forge_conf() -> u64;
 15    #[link_name = "modifyContent"]
 16    unsafe fn _modify_content(
 17        filepathPtr: u32,
 18        filepathSize: u32,
 19        contentPtr: u32,
 20        contentSize: u32,
 21    );
 22    #[link_name = "modifyWebContent"]
 23    unsafe fn _modify_web_content(
 24        filepathPtr: u32,
 25        filepathSize: u32,
 26        contentPtr: u32,
 27        contentSize: u32,
 28    );
 29    #[link_name = "replaceWebContent"]
 30    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    unsafe fn _modify_cache_content(
 40        filepathPtr: u32,
 41        filepathSize: u32,
 42        contentPtr: u32,
 43        contentSize: u32,
 44    );
 45    #[link_name = "commitAll"]
 46    unsafe fn _commit_all(ptr: u32, size: u32);
 47    #[link_name = "diffWithParent"]
 48    unsafe fn _diff_with_parent(
 49        hash_ptr: u32,
 50        hash_size: u32,
 51        oldfile_ptr: u32,
 52        oldfile_size: u32,
 53        newfile_ptr: u32,
 54        newfile_size: u32,
 55    ) -> u64;
 56    #[link_name = "log"]
 57    unsafe fn _log(ptr: u32, size: u32);
 58    #[link_name = "logError"]
 59    unsafe fn _log_error(messagePtr: u32, messageSize: u32, errPtr: u32, errSize: u32);
 60    #[link_name = "merge"]
 61    unsafe fn _merge(from_ptr: u32, from_size: u32, to_ptr: u32, to_size: u32);
 62    #[link_name = "commits"]
 63    unsafe fn _commits(from_ptr: u32, from_size: u32, to_ptr: u32, to_size: u32) -> u64;
 64    #[link_name = "exec"]
 65    unsafe fn _exec(cmd_ptr: u32, cmd_size: u32) -> u64;
 66}
 67
 68pub fn forge_conf() -> Result<ForgeConf, String> {
 69    unsafe {
 70        let ptr_size: u64 = _forge_conf();
 71        if ptr_size == 0 {
 72            return Err(String::from("can't get  forge_conf"));
 73        }
 74        let ptr: u32 = (ptr_size >> 32) as u32;
 75        let size: u32 = ptr_size as u32;
 76        Ok(serde_json::from_str(ptr_to_string(ptr, size).as_str())
 77            .expect("JSON was not well-formatted"))
 78    }
 79}
 80
 81pub fn modify_content(filepath: String, content: String) {
 82    let (filepath_ptr, filepath_size) = string_to_ptr(&filepath);
 83    let (content_ptr, content_size) = string_to_ptr(&content);
 84    unsafe {
 85        _modify_content(filepath_ptr, filepath_size, content_ptr, content_size);
 86    }
 87}
 88
 89pub fn modify_web_content(filepath: String, content: String) {
 90    let (filepath_ptr, filepath_size) = string_to_ptr(&filepath);
 91    let (content_ptr, content_size) = string_to_ptr(&content);
 92    unsafe {
 93        _modify_web_content(filepath_ptr, filepath_size, content_ptr, content_size);
 94    }
 95}
 96
 97pub fn replace_web_content(filepath: String, old_content: String, content: String) {
 98    let (filepath_ptr, filepath_size) = string_to_ptr(&filepath);
 99    let (old_content_ptr, old_content_size) = string_to_ptr(&old_content);
100    let (content_ptr, content_size) = string_to_ptr(&content);
101    unsafe {
102        _replace_web_content(
103            filepath_ptr,
104            filepath_size,
105            old_content_ptr,
106            old_content_size,
107            content_ptr,
108            content_size,
109        );
110    }
111}
112
113pub fn modify_cache_content(filepath: String, content: String) {
114    let (filepath_ptr, filepath_size) = string_to_ptr(&filepath);
115    let (content_ptr, content_size) = string_to_ptr(&content);
116    unsafe {
117        _modify_cache_content(filepath_ptr, filepath_size, content_ptr, content_size);
118    }
119}
120
121pub fn commit_all(message: String) {
122    let (msg_ptr, msg_size) = string_to_ptr(&message);
123    unsafe {
124        _commit_all(msg_ptr, msg_size);
125    }
126}
127
128pub fn diff_with_parent(hash: String, oldfile: String, newfile: String) -> Result<String, String> {
129    let (hash_ptr, hash_size) = string_to_ptr(&hash);
130    let (oldfile_ptr, oldfile_size) = string_to_ptr(&oldfile);
131    let (newfile_ptr, newfile_size) = string_to_ptr(&newfile);
132    unsafe {
133        let ptr_size = _diff_with_parent(
134            hash_ptr,
135            hash_size,
136            oldfile_ptr,
137            oldfile_size,
138            newfile_ptr,
139            newfile_size,
140        );
141        if ptr_size == 0 {
142            return Err(String::from("can't get diff_with_parent"));
143        }
144        let ptr: u32 = (ptr_size >> 32) as u32;
145        let size: u32 = ptr_size as u32;
146        Ok(ptr_to_string(ptr, size))
147    }
148}
149
150pub fn log(message: String) {
151    let (msg_ptr, msg_size) = string_to_ptr(&message);
152    unsafe {
153        _log(msg_ptr, msg_size);
154    }
155}
156
157pub fn log_error(message: String, err: String) {
158    let (msg_ptr, msg_size) = string_to_ptr(&message);
159    let (err_ptr, err_size) = string_to_ptr(&err);
160    unsafe {
161        _log_error(msg_ptr, msg_size, err_ptr, err_size);
162    }
163}
164
165pub fn merge(from: String, to: String) {
166    let (from_ptr, from_size) = string_to_ptr(&from);
167    let (to_ptr, to_size) = string_to_ptr(&to);
168    unsafe {
169        _merge(from_ptr, from_size, to_ptr, to_size);
170    }
171}
172
173pub fn commits(from: String, to: String) -> Result<Vec<Commit>, String> {
174    let (from_ptr, from_size) = string_to_ptr(&from);
175    let (to_ptr, to_size) = string_to_ptr(&to);
176    unsafe {
177        let ptr_size = _commits(from_ptr, from_size, to_ptr, to_size);
178        if ptr_size == 0 {
179            return Err(String::from("can't get  commits"));
180        }
181        let ptr: u32 = (ptr_size >> 32) as u32;
182        let size: u32 = ptr_size as u32;
183        Ok(serde_json::from_str(ptr_to_string(ptr, size).as_str())
184            .expect("JSON was not well-formatted"))
185    }
186}
187
188pub fn exec(exec: &[Exec]) -> Result<String, String> {
189    let exec_json = serde_json::to_string(&exec).unwrap();
190    let (cmd_ptr, cmd_size) = string_to_ptr(&exec_json);
191    unsafe {
192        let ptr_size = _exec(cmd_ptr, cmd_size);
193        if ptr_size == 0 {
194            return Err(String::from("can't exec"));
195        }
196        let ptr: u32 = (ptr_size >> 32) as u32;
197        let size: u32 = ptr_size as u32;
198        Ok(ptr_to_string(ptr, size))
199    }
200}