GitRoot

Craft your forge, Build your project, Grow your community freely
  1// SPDX-FileCopyrightText: 2026 Romain Maneschi <romain@gitroot.dev>
  2//
  3// SPDX-License-Identifier: EUPL-1.2
  4
  5#![no_main]
  6
  7use gitroot_plugin_sdk::{
  8    Plugin, Server,
  9    model::{Commit, File, PluginFunc, PluginRun, PluginWrite},
 10    register,
 11};
 12
 13pub use gitroot_plugin_sdk::exports::*;
 14pub use gitroot_plugin_sdk::imports::*;
 15use mermaid_rs_renderer::RenderOptions;
 16use mermaid_rs_renderer::render_with_options;
 17use serde::{Deserialize, Serialize};
 18use std::collections::HashMap;
 19use std::sync::Arc;
 20
 21struct ApexMermaid {}
 22
 23impl ApexMermaid {
 24    fn new(server: Arc<Server>) -> Self {
 25        server.export_func("renderCode", |args| {
 26            if let Some(value) = args.get("code") {
 27                let mut opts = RenderOptions::modern();
 28                opts.theme.background = "transparent".to_string();
 29                render_with_options(value, opts)
 30                    .map(|svg| HashMap::from([("html".to_string(), svg)]))
 31                    .map_err(|err| err.to_string())
 32            } else {
 33                Ok(HashMap::new())
 34            }
 35        });
 36        Self {}
 37    }
 38}
 39
 40#[derive(Serialize, Deserialize)]
 41struct Conf {}
 42
 43impl Plugin for ApexMermaid {
 44    fn init(
 45        &self,
 46        _repo_name: String,
 47        _conf_has_changed: bool,
 48        _serialized_conf: String,
 49    ) -> Result<(), String> {
 50        Ok(())
 51    }
 52
 53    fn start_commit(&self, _commit: Commit) -> Result<(), String> {
 54        Ok(())
 55    }
 56
 57    fn add_file(&self, _file: File) -> Result<(), String> {
 58        Ok(())
 59    }
 60
 61    fn mod_file(&self, _file: File) -> Result<(), String> {
 62        Ok(())
 63    }
 64
 65    fn del_file(&self, _file: File) -> Result<(), String> {
 66        Ok(())
 67    }
 68
 69    fn end_commit(&self, _commit: Commit) -> Result<(), String> {
 70        Ok(())
 71    }
 72
 73    fn finish(&self) -> Result<(), String> {
 74        Ok(())
 75    }
 76}
 77
 78#[cfg_attr(all(target_arch = "wasm32"), unsafe(export_name = "install"))]
 79pub extern "C" fn install() {
 80    let conf = PluginRun {
 81        path: String::from(""),
 82        branch: vec![],
 83        when: vec![],
 84        func: vec![PluginFunc {
 85            func_name: "renderCode".to_string(),
 86            args: vec!["code".to_string()],
 87            res: vec!["html".to_string()],
 88        }],
 89        write: PluginWrite {
 90            git: vec![],
 91            web: vec![],
 92            exec: vec![],
 93            call_func: vec![],
 94        },
 95        configuration: serde_json::to_value(Conf {}).unwrap(),
 96    };
 97    register(vec![conf], ApexMermaid::new);
 98}
 99
100#[cfg(test)]
101mod tests {
102    #[test]
103    fn test_plugin() {
104        assert_eq!(true, true);
105    }
106}