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
/*
 * Yagna Activity API
 *
 * It conforms with capability level 1 of the [Activity API specification](https://golem-network.gitbook.io/golem-internal-documentation-test/golem-activity-protocol/golem-activity-api).
 *
 * The version of the OpenAPI document: v1
 *
 * Generated by: https://openapi-generator.tech
 */

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ActivityState {
    #[serde(rename = "state")]
    pub state: StatePair,
    /// Reason for Activity termination (specified when Activity in Terminated state).
    #[serde(rename = "reason", skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
    /// If error caused state change - error message shall be provided.
    #[serde(rename = "errorMessage", skip_serializing_if = "Option::is_none")]
    pub error_message: Option<String>,
}

impl ActivityState {
    pub fn alive(&self) -> bool {
        self.state.alive()
    }
}

impl From<&StatePair> for ActivityState {
    fn from(pending: &StatePair) -> Self {
        ActivityState {
            state: *pending,
            reason: None,
            error_message: None,
        }
    }
}

impl From<StatePair> for ActivityState {
    fn from(pending: StatePair) -> Self {
        ActivityState {
            state: pending,
            reason: None,
            error_message: None,
        }
    }
}

#[derive(
    Clone, Copy, Default, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize,
)]
pub struct StatePair(pub State, pub Option<State>);

impl StatePair {
    pub fn alive(&self) -> bool {
        !matches!(
            (&self.0, &self.1),
            (State::Terminated, _) | (_, Some(State::Terminated))
        )
    }

    pub fn to_pending(&self, state: State) -> Self {
        StatePair(self.0, Some(state))
    }
}

impl From<State> for StatePair {
    fn from(state: State) -> Self {
        StatePair(state, None)
    }
}

#[derive(
    Clone, Default, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize,
)]
pub enum State {
    #[default]
    New,
    Initialized,
    Deployed,
    Ready,
    Terminated,
    Unresponsive,
}