1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! Handle task representations of a task

use crate::definitions::tst as definitions_tst;
use crate::{delegation, uuid, Error, Result};
use definitions_tst::NodeTrait;
use serde::{Deserialize, Serialize};

//  _____         _     ____            _        _
// |_   _|_ _ ___| | __/ ___|___  _ __ | |_ __ _(_)_ __   ___ _ __
//   | |/ _` / __| |/ / |   / _ \| '_ \| __/ _` | | '_ \ / _ \ '__|
//   | | (_| \__ \   <| |__| (_) | | | | || (_| | | | | |  __/ |
//   |_|\__,_|___/_|\_\\____\___/|_| |_|\__\__,_|_|_| |_|\___|_|
//

/// Contains the different definition of a task
#[derive(Serialize, Deserialize, Clone)]
pub enum TaskContainer
{
  /// A TST-based task definition
  Tst(definitions_tst::Node),
}

//  _______        _
// |__   __|      | |
//    | | __ _ ___| | __
//    | |/ _` / __| |/ /
//    | | (_| \__ \   <
//    |_|\__,_|___/_|\_\

/// Represent a task
#[derive(Serialize, Deserialize, Clone)]
pub struct Task
{
  container: TaskContainer,
}

impl Task
{
  /// Create a new task from a TST definition
  pub fn from_tst(node: definitions_tst::Node) -> Task
  {
    Task {
      container: TaskContainer::Tst(node),
    }
  }
  /// Return the container
  pub fn get_container(&self) -> &TaskContainer
  {
    &self.container
  }
}

impl delegation::Task for Task
{
  fn from_description(task_type: &String, description: &String) -> Result<Self>
  {
    match task_type.as_str()
    {
      crate::consts::TST => Ok(Task::from_tst(
        definitions_tst::Node::from_json_string(&description).unwrap(),
      )),
      unknown_task_type => Err(Error::UnknownTaskType(unknown_task_type.to_string())),
    }
  }
  fn task_id(&self) -> uuid::Uuid
  {
    match &self.container
    {
      TaskContainer::Tst(tst) => tst.common_params_ref().node_uuid,
    }
  }
  fn task_type(&self) -> &str
  {
    match self.container
    {
      TaskContainer::Tst(_) => crate::consts::TST,
    }
  }
  fn to_description(&self) -> String
  {
    match &self.container
    {
      TaskContainer::Tst(task) => task.to_json_string().unwrap(),
    }
  }
}