Skip to main content

eoka_protocol/
operations.rs

1pub use eoka::{
2    Action, ActionOptions, ActionOutcome, ActionReceipt, Destination, DispatchState, Expectation,
3    InputMode, LocatorQuery, LocatorSpec,
4};
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
8pub struct ActionPerformArgs {
9    pub operation_id: String,
10    pub action: Action,
11    #[serde(default)]
12    pub options: ActionOptions,
13}
14
15#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
16#[cfg_attr(feature = "clap", derive(clap::Args))]
17pub struct OperationIdArgs {
18    #[cfg_attr(feature = "clap", arg(help = crate::args::OPERATION_ID_HELP))]
19    #[serde(alias = "operation_id")]
20    #[schemars(description = "Operation ID: the id field of the action's result.")]
21    pub id: String,
22}
23
24#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
25#[cfg_attr(feature = "clap", derive(clap::Args))]
26pub struct OperationWaitArgs {
27    #[cfg_attr(feature = "clap", arg(help = crate::args::OPERATION_ID_HELP))]
28    #[serde(alias = "operation_id")]
29    #[schemars(description = "Operation ID: the id field of the action's result.")]
30    pub id: String,
31    #[cfg_attr(
32        feature = "clap",
33        arg(
34            long,
35            default_value_t = 30000,
36            help = "How long to wait; the operation keeps running after it"
37        )
38    )]
39    #[serde(default = "wait_timeout")]
40    #[schemars(range(max = 86400000))]
41    pub timeout_ms: u64,
42}
43fn wait_timeout() -> u64 {
44    30_000
45}
46
47#[derive(Debug, Clone, Default, Serialize, Deserialize, schemars::JsonSchema)]
48pub struct OperationListArgs {
49    #[serde(default)]
50    pub before_ms: Option<u64>,
51    #[serde(default)]
52    pub limit: Option<u32>,
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
56#[serde(rename_all = "snake_case")]
57pub enum OperationState {
58    Running,
59    Finished,
60    Interrupted,
61    Expired,
62}
63
64#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
65pub struct OperationRecord {
66    pub id: String,
67    pub runtime_id: String,
68    pub target_id: String,
69    pub action: String,
70    pub state: OperationState,
71    pub outcome: ActionOutcome,
72    pub dispatch_intent: bool,
73    pub preparation_intent: bool,
74    pub started_at_ms: u64,
75    pub finished_at_ms: Option<u64>,
76    pub receipt: Option<ActionReceipt>,
77    pub error_code: Option<String>,
78    #[serde(default)]
79    pub error: Option<crate::ErrorDetail>,
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
83pub struct ActionResult {
84    #[serde(flatten)]
85    pub record: OperationRecord,
86    pub status: OperationStatus,
87}
88
89impl From<OperationRecord> for ActionResult {
90    fn from(record: OperationRecord) -> Self {
91        Self {
92            status: record.status(),
93            record,
94        }
95    }
96}
97
98#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
99#[serde(rename_all = "snake_case")]
100pub enum OperationStatus {
101    Running,
102    VerifiedComplete,
103    NotDispatched,
104    OutcomeUnknown,
105}
106
107impl OperationStatus {
108    pub fn exit_code(self) -> i32 {
109        match self {
110            Self::VerifiedComplete => 0,
111            Self::NotDispatched => 1,
112            Self::Running => 3,
113            Self::OutcomeUnknown => 4,
114        }
115    }
116}
117
118impl OperationRecord {
119    pub fn status(&self) -> OperationStatus {
120        if matches!(self.state, OperationState::Running) {
121            return OperationStatus::Running;
122        }
123        match self.outcome {
124            ActionOutcome::VerifiedComplete if self.error_code.is_none() => {
125                OperationStatus::VerifiedComplete
126            }
127            ActionOutcome::NotDispatched => OperationStatus::NotDispatched,
128            _ => OperationStatus::OutcomeUnknown,
129        }
130    }
131}
132
133#[derive(Debug, Clone, Default, Serialize, Deserialize, schemars::JsonSchema)]
134pub struct ActionSettings {
135    #[serde(default, skip_serializing_if = "Option::is_none")]
136    #[schemars(
137        description = "Optional UUID. Generated when omitted and returned as id. Send your own to retry safely after a lost response."
138    )]
139    pub operation_id: Option<String>,
140    #[serde(default, skip_serializing_if = "Option::is_none")]
141    #[schemars(
142        description = "A condition that is false now and true after the action, for example {\"kind\":\"url\",\"value\":\"/submit\",\"contains\":true}. A wrong guess fails after the action has already run."
143    )]
144    pub expect: Option<Expectation>,
145    #[serde(default, skip_serializing_if = "Option::is_none")]
146    pub timeout_ms: Option<u64>,
147}
148
149#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
150pub struct ActionCommandArgs {
151    pub operation_id: String,
152    pub command: String,
153    pub arguments: serde_json::Value,
154}
155
156#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
157pub struct ActionBatchArgs {
158    pub actions: Vec<ActionPerformArgs>,
159}