agent_tk/definitions/
task.rs

1//! Handle task representations of a task
2
3use 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//  _____         _     ____            _        _
10// |_   _|_ _ ___| | __/ ___|___  _ __ | |_ __ _(_)_ __   ___ _ __
11//   | |/ _` / __| |/ / |   / _ \| '_ \| __/ _` | | '_ \ / _ \ '__|
12//   | | (_| \__ \   <| |__| (_) | | | | || (_| | | | | |  __/ |
13//   |_|\__,_|___/_|\_\\____\___/|_| |_|\__\__,_|_|_| |_|\___|_|
14//
15
16/// Contains the different definition of a task
17#[derive(Serialize, Deserialize, Clone)]
18pub enum TaskContainer
19{
20  /// A TST-based task definition
21  Tst(definitions_tst::Node),
22  /// A goal-based task definition
23  Goal(definitions_goal::Goal),
24}
25
26//  _______        _
27// |__   __|      | |
28//    | | __ _ ___| | __
29//    | |/ _` / __| |/ /
30//    | | (_| \__ \   <
31//    |_|\__,_|___/_|\_\
32
33/// Represent a task
34#[derive(Serialize, Deserialize, Clone)]
35pub struct Task
36{
37  container: TaskContainer,
38}
39
40impl Task
41{
42  /// Create a new task from a TST definition
43  pub fn from_tst(node: definitions_tst::Node) -> Task
44  {
45    Task {
46      container: TaskContainer::Tst(node),
47    }
48  }
49  /// Create a new task from a Goal definition
50  pub fn from_goal(goal: definitions_goal::Goal) -> Task
51  {
52    Task {
53      container: TaskContainer::Goal(goal),
54    }
55  }
56  /// Return the container
57  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}