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)]
48#[cfg_attr(feature = "clap", derive(clap::Args))]
49pub struct OperationListArgs {
50    #[cfg_attr(feature = "clap", arg(skip))]
51    #[serde(default)]
52    pub before_ms: Option<u64>,
53    #[cfg_attr(
54        feature = "clap",
55        arg(long, help = "Maximum records to return [default: 100]")
56    )]
57    #[serde(default)]
58    pub limit: Option<u32>,
59}
60
61#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
62#[serde(rename_all = "snake_case")]
63pub enum OperationState {
64    Running,
65    Finished,
66    Interrupted,
67    Expired,
68}
69
70#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
71pub struct OperationRecord {
72    pub id: String,
73    pub runtime_id: String,
74    pub target_id: String,
75    pub action: String,
76    pub state: OperationState,
77    pub outcome: ActionOutcome,
78    pub dispatch_intent: bool,
79    pub preparation_intent: bool,
80    pub started_at_ms: u64,
81    pub finished_at_ms: Option<u64>,
82    pub receipt: Option<ActionReceipt>,
83    pub error_code: Option<String>,
84    #[serde(default)]
85    pub error: Option<crate::ErrorDetail>,
86}
87
88#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
89pub struct ActionResult {
90    #[serde(flatten)]
91    pub record: OperationRecord,
92    pub status: OperationStatus,
93}
94
95impl From<OperationRecord> for ActionResult {
96    fn from(record: OperationRecord) -> Self {
97        Self {
98            status: record.status(),
99            record,
100        }
101    }
102}
103
104#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
105#[serde(rename_all = "snake_case")]
106pub enum OperationStatus {
107    Running,
108    VerifiedComplete,
109    NotDispatched,
110    OutcomeUnknown,
111}
112
113impl OperationStatus {
114    pub fn exit_code(self) -> i32 {
115        match self {
116            Self::VerifiedComplete => 0,
117            Self::NotDispatched => 1,
118            Self::Running => 3,
119            Self::OutcomeUnknown => 4,
120        }
121    }
122}
123
124impl OperationRecord {
125    pub fn status(&self) -> OperationStatus {
126        if matches!(self.state, OperationState::Running) {
127            return OperationStatus::Running;
128        }
129        match self.outcome {
130            ActionOutcome::VerifiedComplete if self.error_code.is_none() => {
131                OperationStatus::VerifiedComplete
132            }
133            ActionOutcome::NotDispatched => OperationStatus::NotDispatched,
134            _ => OperationStatus::OutcomeUnknown,
135        }
136    }
137}
138
139#[derive(Debug, Clone, Default, Serialize, Deserialize, schemars::JsonSchema)]
140pub struct ActionSettings {
141    #[serde(default, skip_serializing_if = "Option::is_none")]
142    #[schemars(
143        description = "Optional UUID. Generated when omitted and returned as id. Send your own to retry safely after a lost response."
144    )]
145    pub operation_id: Option<String>,
146    #[serde(default, skip_serializing_if = "Option::is_none")]
147    #[schemars(
148        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."
149    )]
150    pub expect: Option<Expectation>,
151    #[serde(default, skip_serializing_if = "Option::is_none")]
152    pub timeout_ms: Option<u64>,
153}
154
155#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
156pub struct ActionCommandArgs {
157    pub operation_id: String,
158    pub command: String,
159    pub arguments: serde_json::Value,
160}
161
162#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
163pub struct ActionBatchArgs {
164    pub actions: Vec<ActionPerformArgs>,
165}