use std::sync::Arc;
use log::error;
use crate as pipeless;
pub enum Context {
EmptyContext,
PythonContext(pipeless::stages::languages::python::PythonStageContext),
RustContext(pipeless::stages::languages::rust::RustStageContext)
}
pub trait ContextTrait<T> {
fn init_context(stage_name: &str, init_code: &str) -> T;
}
pub struct Stage {
name: String,
hooks: Vec<pipeless::stages::hook::Hook>,
context: Arc<Context>,
}
impl Stage {
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
hooks: vec![],
context: Arc::new(Context::EmptyContext)
}
}
pub fn get_name(&self) -> &str {
&self.name
}
pub fn add_hook(&mut self, new_hook: pipeless::stages::hook::Hook) {
let existing_hook = self.hooks.iter().find(|h| h.get_hook_type() == new_hook.get_hook_type());
if existing_hook.is_some() {
error!("⚠️ Failed to add duplicated hook type to the stage '{}'.", self.name);
} else {
self.hooks.push(new_hook);
}
}
pub fn get_hooks(&self) -> &Vec<pipeless::stages::hook::Hook> {
&self.hooks
}
pub fn set_context(&mut self, context: Context) {
self.context = Arc::new(context);
}
pub fn get_context(&self) -> Arc<Context> {
self.context.clone()
}
}