ya_client_model/activity/
activity_state.rs

1/*
2 * Yagna Activity API
3 *
4 * 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).
5 *
6 * The version of the OpenAPI document: v1
7 *
8 * Generated by: https://openapi-generator.tech
9 */
10
11use serde::{Deserialize, Serialize};
12
13#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
14pub struct ActivityState {
15    #[serde(rename = "state")]
16    pub state: StatePair,
17    /// Reason for Activity termination (specified when Activity in Terminated state).
18    #[serde(rename = "reason", skip_serializing_if = "Option::is_none")]
19    pub reason: Option<String>,
20    /// If error caused state change - error message shall be provided.
21    #[serde(rename = "errorMessage", skip_serializing_if = "Option::is_none")]
22    pub error_message: Option<String>,
23}
24
25impl ActivityState {
26    pub fn alive(&self) -> bool {
27        self.state.alive()
28    }
29}
30
31impl From<&StatePair> for ActivityState {
32    fn from(pending: &StatePair) -> Self {
33        ActivityState {
34            state: *pending,
35            reason: None,
36            error_message: None,
37        }
38    }
39}
40
41impl From<StatePair> for ActivityState {
42    fn from(pending: StatePair) -> Self {
43        ActivityState {
44            state: pending,
45            reason: None,
46            error_message: None,
47        }
48    }
49}
50
51#[derive(
52    Clone, Copy, Default, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize,
53)]
54pub struct StatePair(pub State, pub Option<State>);
55
56impl StatePair {
57    pub fn alive(&self) -> bool {
58        !matches!(
59            (&self.0, &self.1),
60            (State::Terminated, _) | (_, Some(State::Terminated))
61        )
62    }
63
64    pub fn to_pending(&self, state: State) -> Self {
65        StatePair(self.0, Some(state))
66    }
67}
68
69impl From<State> for StatePair {
70    fn from(state: State) -> Self {
71        StatePair(state, None)
72    }
73}
74
75#[derive(
76    Clone, Default, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize,
77)]
78pub enum State {
79    #[default]
80    New,
81    Initialized,
82    Deployed,
83    Ready,
84    Terminated,
85    Unresponsive,
86}