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))]
pub struct JsFunctionCall {
function_name: String,
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)");
}
}