use crate::tasks::*;
use crate::handle::handle::TaskHandle;
use serde::Deserialize;
use std::sync::Arc;
const MODULE: &str = "echo";
#[derive(Deserialize,Debug)]
#[serde(deny_unknown_fields)]
pub struct EchoTask {
pub name: Option<String>,
pub msg: String,
pub with: Option<PreLogicInput>,
pub and: Option<PostLogicInput>
}
#[allow(dead_code)]
struct EchoAction {
pub name: String,
pub msg: String,
}
impl IsTask for EchoTask {
fn get_module(&self) -> String { String::from(MODULE) }
fn get_name(&self) -> Option<String> { self.name.clone() }
fn get_with(&self) -> Option<PreLogicInput> { self.with.clone() }
fn evaluate(&self, handle: &Arc<TaskHandle>, request: &Arc<TaskRequest>, tm: TemplateMode) -> Result<EvaluatedTask, Arc<TaskResponse>> {
return Ok(
EvaluatedTask {
action: Arc::new(EchoAction {
name: self.name.clone().unwrap_or(String::from(MODULE)),
msg: handle.template.string_unsafe_for_shell(request, tm, &String::from("msg"), &self.msg)?,
}),
with: Arc::new(PreLogicInput::template(handle, request, tm, &self.with)?),
and: Arc::new(PostLogicInput::template(handle, request, tm, &self.and)?),
}
);
}
}
impl IsAction for EchoAction {
fn dispatch(&self, handle: &Arc<TaskHandle>, request: &Arc<TaskRequest>) -> Result<Arc<TaskResponse>, Arc<TaskResponse>> {
match request.request_type {
TaskRequestType::Query => {
return Ok(handle.response.needs_passive(request));
},
TaskRequestType::Passive => {
handle.debug(&request, &self.msg);
return Ok(handle.response.is_passive(request));
},
_ => { return Err(handle.response.not_supported(request)); }
}
}
}