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
use serde::{Deserialize, Serialize};

#[derive(Default, Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase", default)]
/// The Task Id allows clients to uniquely identify a BSP task and establish a client-parent
/// relationship with another task id.
pub struct TaskId {
    /// A unique identifier
    id: String,

    /// The parent task ids, if any. A non-empty parents field means
    ///  * this task is a sub-task of every parent task id. The child-parent
    ///  * relationship of tasks makes it possible to render tasks in
    ///  * a tree-like user interface or inspect what caused a certain task
    ///  * execution.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    parents: Vec<String>,
}

impl From<String> for TaskId {
    fn from(id: String) -> Self {
        Self {
            id,
            parents: Default::default(),
        }
    }
}

impl From<&str> for TaskId {
    fn from(id: &str) -> Self {
        Self {
            id: id.into(),
            parents: Default::default(),
        }
    }
}

impl TaskId {
    pub fn new(id: String, parents: Vec<String>) -> Self {
        Self { id, parents }
    }

    pub fn add_parent(&mut self, value: String) {
        self.parents.push(value)
    }
}