agent_tk/definitions/
task.rs1use crate::definitions::goal as definitions_goal;
4use crate::definitions::tst as definitions_tst;
5use crate::{delegation, uuid, Error, Result};
6use definitions_tst::NodeTrait;
7use serde::{Deserialize, Serialize};
8
9#[derive(Serialize, Deserialize, Clone)]
18pub enum TaskContainer
19{
20 Tst(definitions_tst::Node),
22 Goal(definitions_goal::Goal),
24}
25
26#[derive(Serialize, Deserialize, Clone)]
35pub struct Task
36{
37 container: TaskContainer,
38}
39
40impl Task
41{
42 pub fn from_tst(node: definitions_tst::Node) -> Task
44 {
45 Task {
46 container: TaskContainer::Tst(node),
47 }
48 }
49 pub fn from_goal(goal: definitions_goal::Goal) -> Task
51 {
52 Task {
53 container: TaskContainer::Goal(goal),
54 }
55 }
56 pub fn get_container(&self) -> &TaskContainer
58 {
59 &self.container
60 }
61}
62
63impl delegation::Task for Task
64{
65 fn from_description(task_type: impl AsRef<str>, description: impl AsRef<str>) -> Result<Self>
66 {
67 match task_type.as_ref()
68 {
69 crate::consts::TST => Ok(Task::from_tst(
70 definitions_tst::Node::from_json_string(description).unwrap(),
71 )),
72 crate::consts::GOAL => Ok(Task::from_goal(
73 definitions_goal::Goal::from_json_string(description).unwrap(),
74 )),
75 unknown_task_type => Err(Error::UnknownTaskType(unknown_task_type.to_string())),
76 }
77 }
78 fn task_id(&self) -> uuid::Uuid
79 {
80 match &self.container
81 {
82 TaskContainer::Tst(tst) => tst.common_params_ref().node_uuid,
83 TaskContainer::Goal(goal) => goal.uuid,
84 }
85 }
86 fn task_type(&self) -> &str
87 {
88 match self.container
89 {
90 TaskContainer::Tst(_) => crate::consts::TST,
91 TaskContainer::Goal(_) => crate::consts::GOAL,
92 }
93 }
94 fn to_description(&self) -> String
95 {
96 match &self.container
97 {
98 TaskContainer::Tst(task) => task.to_json_string().unwrap(),
99 TaskContainer::Goal(goal) => goal.to_json_string().unwrap(),
100 }
101 }
102}