use serde_json::Value;
pub trait TaskHost: Send + Sync {
fn caller_task_id(&self, lane: &str) -> Option<String>;
fn task_lane(&self, task_id: &str) -> Option<String>;
fn strategy(&self, definition: &str) -> Result<String, String>;
fn tasks(&self) -> Result<Vec<Value>, String>;
fn spawn(&self, request: &Value, parent: Option<&str>) -> Result<Value, String>;
fn result(&self, task_id: &str) -> Result<Value, String>;
fn cancel(&self, task_id: &str, reason: &str) -> Result<Value, String>;
fn steer(&self, task_id: &str, message: &str) -> Result<bool, String>;
fn usage(&self) -> Result<Option<Value>, String> {
Ok(None)
}
fn now(&self) -> i64;
fn pin_lane(&self, _lane: &str, _holder: &str) -> bool {
false
}
fn unpin_lane(&self, _lane: &str, _holder: &str) {}
}
pub struct RootOnly {
pub now: crate::util::Clock,
}
const NO_TASKS: &str = "Ultron task runtime is not available in this host";
impl TaskHost for RootOnly {
fn caller_task_id(&self, _lane: &str) -> Option<String> {
None
}
fn task_lane(&self, _task_id: &str) -> Option<String> {
None
}
fn strategy(&self, definition: &str) -> Result<String, String> {
Err(format!("Unknown Ultron definition: {definition}"))
}
fn tasks(&self) -> Result<Vec<Value>, String> {
Ok(Vec::new())
}
fn spawn(&self, _request: &Value, _parent: Option<&str>) -> Result<Value, String> {
Err(NO_TASKS.into())
}
fn result(&self, _task_id: &str) -> Result<Value, String> {
Err("Unknown Ultron task".into())
}
fn cancel(&self, _task_id: &str, _reason: &str) -> Result<Value, String> {
Err("Unknown Ultron task".into())
}
fn steer(&self, _task_id: &str, _message: &str) -> Result<bool, String> {
Ok(false)
}
fn now(&self) -> i64 {
(self.now)()
}
}