mermaid-builder 0.1.2

A Rust library for generating Mermaid diagrams using the builder pattern.
Documentation
//! Submodule handling click events in Mermaid nodes which lead to calling
//! JavaScript functions, including the typing of the function signature
//! and the function call itself.

use alloc::{format, string::String, vec::Vec};
use core::fmt::Display;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// Represents a JavaScript function call that can be triggered by a click event
/// on a Mermaid node.
pub struct JsFunctionCall {
    /// The name of the JavaScript function to call.
    function_name: String,
    /// The arguments to pass to the JavaScript function.
    args: Vec<String>,
}

impl Display for JsFunctionCall {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let args_str = if self.args.is_empty() {
            String::new()
        } else {
            format!("({})", self.args.join(", "))
        };
        write!(f, "{}{}", self.function_name, args_str)
    }
}

#[cfg(test)]
mod tests {
    use alloc::{format, string::ToString, vec};

    use super::*;

    #[test]
    fn test_js_function_call_display() {
        let call = JsFunctionCall { function_name: "myFunc".to_string(), args: vec![] };
        assert_eq!(format!("{call}"), "myFunc");

        let call = JsFunctionCall {
            function_name: "myFunc".to_string(),
            args: vec!["arg1".to_string(), "arg2".to_string()],
        };
        assert_eq!(format!("{call}"), "myFunc(arg1, arg2)");
    }
}