openzeppelin_monitor/services/trigger/script/
factory.rs

1//! Trigger script factory implementation.
2//!
3//! This module provides functionality to create script executors based on the script language.
4
5use crate::{
6	models::ScriptLanguage,
7	services::trigger::script::executor::{
8		BashScriptExecutor, JavaScriptScriptExecutor, PythonScriptExecutor, ScriptExecutor,
9	},
10};
11
12/// Factory for creating script executors based on the script language.
13pub struct ScriptExecutorFactory;
14
15impl ScriptExecutorFactory {
16	/// Creates a new script executor for the specified language and script path.
17	///
18	/// # Arguments
19	///
20	/// * `language` - The programming language of the script
21	/// * `script_content` - The content of the script
22	///
23	/// # Returns
24	///
25	/// Returns a boxed (Rust will allocate on the heap) trait object implementing the
26	/// `ScriptExecutor` trait
27	pub fn create(language: &ScriptLanguage, script_content: &str) -> Box<dyn ScriptExecutor> {
28		match language {
29			ScriptLanguage::Python => Box::new(PythonScriptExecutor {
30				script_content: script_content.to_string(),
31			}),
32			ScriptLanguage::JavaScript => Box::new(JavaScriptScriptExecutor {
33				script_content: script_content.to_string(),
34			}),
35			ScriptLanguage::Bash => Box::new(BashScriptExecutor {
36				script_content: script_content.to_string(),
37			}),
38		}
39	}
40}
41
42#[cfg(test)]
43mod tests {
44	use super::*;
45	use crate::models::ScriptLanguage;
46
47	#[test]
48	fn test_create_python_executor() {
49		let script = "print('Hello')";
50		let executor = ScriptExecutorFactory::create(&ScriptLanguage::Python, script);
51		assert!(
52			executor
53				.as_any()
54				.downcast_ref::<PythonScriptExecutor>()
55				.unwrap()
56				.script_content
57				== script
58		);
59
60		// Test with empty script
61		let empty_script = "";
62		let executor = ScriptExecutorFactory::create(&ScriptLanguage::Python, empty_script);
63		assert!(executor
64			.as_any()
65			.downcast_ref::<PythonScriptExecutor>()
66			.unwrap()
67			.script_content
68			.is_empty());
69	}
70
71	#[test]
72	fn test_create_javascript_executor() {
73		let script = "console.log('Hello')";
74		let executor = ScriptExecutorFactory::create(&ScriptLanguage::JavaScript, script);
75		assert!(
76			executor
77				.as_any()
78				.downcast_ref::<JavaScriptScriptExecutor>()
79				.unwrap()
80				.script_content
81				== script
82		);
83
84		// Test with empty script
85		let empty_script = "";
86		let executor = ScriptExecutorFactory::create(&ScriptLanguage::JavaScript, empty_script);
87		assert!(executor
88			.as_any()
89			.downcast_ref::<JavaScriptScriptExecutor>()
90			.unwrap()
91			.script_content
92			.is_empty());
93	}
94
95	#[test]
96	fn test_create_bash_executor() {
97		let script = "echo 'Hello'";
98		let executor = ScriptExecutorFactory::create(&ScriptLanguage::Bash, script);
99		assert!(
100			executor
101				.as_any()
102				.downcast_ref::<BashScriptExecutor>()
103				.unwrap()
104				.script_content
105				== script
106		);
107
108		// Test with empty script
109		let empty_script = "";
110		let executor = ScriptExecutorFactory::create(&ScriptLanguage::Bash, empty_script);
111		assert!(executor
112			.as_any()
113			.downcast_ref::<BashScriptExecutor>()
114			.unwrap()
115			.script_content
116			.is_empty());
117	}
118}