openzeppelin_monitor/services/trigger/script/
factory.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//! Trigger script factory implementation.
//!
//! This module provides functionality to create script executors based on the script language.

use crate::{
	models::ScriptLanguage,
	services::trigger::script::executor::{
		BashScriptExecutor, JavaScriptScriptExecutor, PythonScriptExecutor, ScriptExecutor,
	},
};

/// Factory for creating script executors based on the script language.
pub struct ScriptExecutorFactory;

impl ScriptExecutorFactory {
	/// Creates a new script executor for the specified language and script path.
	///
	/// # Arguments
	///
	/// * `language` - The programming language of the script
	/// * `script_content` - The content of the script
	///
	/// # Returns
	///
	/// Returns a boxed (Rust will allocate on the heap) trait object implementing the
	/// `ScriptExecutor` trait
	pub fn create(language: &ScriptLanguage, script_content: &str) -> Box<dyn ScriptExecutor> {
		match language {
			ScriptLanguage::Python => Box::new(PythonScriptExecutor {
				script_content: script_content.to_string(),
			}),
			ScriptLanguage::JavaScript => Box::new(JavaScriptScriptExecutor {
				script_content: script_content.to_string(),
			}),
			ScriptLanguage::Bash => Box::new(BashScriptExecutor {
				script_content: script_content.to_string(),
			}),
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::models::ScriptLanguage;

	#[test]
	fn test_create_python_executor() {
		let script = "print('Hello')";
		let executor = ScriptExecutorFactory::create(&ScriptLanguage::Python, script);
		assert!(
			executor
				.as_any()
				.downcast_ref::<PythonScriptExecutor>()
				.unwrap()
				.script_content
				== script
		);

		// Test with empty script
		let empty_script = "";
		let executor = ScriptExecutorFactory::create(&ScriptLanguage::Python, empty_script);
		assert!(executor
			.as_any()
			.downcast_ref::<PythonScriptExecutor>()
			.unwrap()
			.script_content
			.is_empty());
	}

	#[test]
	fn test_create_javascript_executor() {
		let script = "console.log('Hello')";
		let executor = ScriptExecutorFactory::create(&ScriptLanguage::JavaScript, script);
		assert!(
			executor
				.as_any()
				.downcast_ref::<JavaScriptScriptExecutor>()
				.unwrap()
				.script_content
				== script
		);

		// Test with empty script
		let empty_script = "";
		let executor = ScriptExecutorFactory::create(&ScriptLanguage::JavaScript, empty_script);
		assert!(executor
			.as_any()
			.downcast_ref::<JavaScriptScriptExecutor>()
			.unwrap()
			.script_content
			.is_empty());
	}

	#[test]
	fn test_create_bash_executor() {
		let script = "echo 'Hello'";
		let executor = ScriptExecutorFactory::create(&ScriptLanguage::Bash, script);
		assert!(
			executor
				.as_any()
				.downcast_ref::<BashScriptExecutor>()
				.unwrap()
				.script_content
				== script
		);

		// Test with empty script
		let empty_script = "";
		let executor = ScriptExecutorFactory::create(&ScriptLanguage::Bash, empty_script);
		assert!(executor
			.as_any()
			.downcast_ref::<BashScriptExecutor>()
			.unwrap()
			.script_content
			.is_empty());
	}
}