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
// Souk - task_progress.rs
// Copyright (C) 2022-2023  Felix Häcker <haeckerfelix@gnome.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use serde::{Deserialize, Serialize};
use zbus::zvariant::{Optional, Type};

use crate::shared::task::{TaskProgress, TaskResult};

#[derive(Deserialize, Serialize, Type, PartialEq, Debug, Clone)]
pub struct TaskResponse {
    /// The UUID of the corresponding task
    pub uuid: String,
    pub kind: TaskResponseKind,

    // This should have been an enum, unfortunately not supported by zbus / dbus
    /// Initial response that provides information about this task and all
    /// related steps / subtasks
    pub initial_response: Optional<Vec<TaskProgress>>,
    pub update_response: Optional<TaskProgress>,
    pub result_response: Optional<TaskResult>,
}

impl TaskResponse {
    pub fn new_initial(uuid: String, steps: Vec<TaskProgress>) -> Self {
        Self {
            uuid,
            kind: TaskResponseKind::Initial,
            initial_response: Some(steps).into(),
            update_response: None.into(),
            result_response: None.into(),
        }
    }

    pub fn new_update(uuid: String, step: TaskProgress) -> Self {
        Self {
            uuid,
            kind: TaskResponseKind::Update,
            initial_response: None.into(),
            update_response: Some(step).into(),
            result_response: None.into(),
        }
    }

    pub fn new_result(uuid: String, result: TaskResult) -> Self {
        Self {
            uuid,
            kind: TaskResponseKind::Result,
            initial_response: None.into(),
            update_response: None.into(),
            result_response: Some(result).into(),
        }
    }
}

#[derive(Deserialize, Serialize, Type, Eq, PartialEq, Debug, Clone, Hash)]
pub enum TaskResponseKind {
    /// Initial (first) response of a task. This includes a detailed list of all
    /// steps, see [TaskResponse.initial_response].
    Initial,
    /// Update response of a task, contains updated information of a single
    /// step, see [TaskResponse.update_response].
    Update,
    /// Task ended. See [TaskResponse.result_response] for more details.
    Result,
}