dapr 0.19.0-rc.0

Rust SDK for dapr
Documentation
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use std::time::SystemTime;

use dapr_durabletask::api::{HistoryPropagationScope, RetryPolicy};
use serde::Serialize;

/// Options for scheduling a new workflow instance.
#[derive(Debug, Clone, Default)]
pub struct ScheduleOptions {
    /// Optional caller-supplied instance ID. When `None`, the sidecar generates one.
    pub instance_id: Option<String>,
    /// Optional JSON-serialized input passed to the workflow.
    pub input: Option<serde_json::Value>,
    /// Optional start time. When `None`, the workflow starts immediately.
    pub start_time: Option<SystemTime>,
}

impl ScheduleOptions {
    /// Create an empty set of schedule options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set an explicit instance ID for the new workflow.
    ///
    /// # Arguments
    ///
    /// * `instance_id` - Caller-supplied identifier for the workflow instance.
    pub fn with_instance_id(mut self, instance_id: impl Into<String>) -> Self {
        self.instance_id = Some(instance_id.into());
        self
    }

    /// Set the JSON-serializable input passed to the workflow.
    ///
    /// # Panics
    ///
    /// Panics if `input` cannot be serialized to JSON. Prefer
    /// [`ScheduleOptions::try_with_input`] for fallible serialization.
    ///
    /// # Arguments
    ///
    /// * `input` - Value serialized to JSON and delivered as the workflow input.
    pub fn with_input<T: Serialize>(self, input: T) -> Self {
        self.try_with_input(input)
            .expect("workflow input must be JSON serializable")
    }

    /// Set the JSON-serializable input passed to the workflow, returning an
    /// error if serialization fails.
    ///
    /// # Arguments
    ///
    /// * `input` - Value serialized to JSON and delivered as the workflow input.
    pub fn try_with_input<T: Serialize>(mut self, input: T) -> Result<Self, serde_json::Error> {
        self.input = Some(serde_json::to_value(input)?);
        Ok(self)
    }

    /// Set the scheduled start time for the workflow.
    ///
    /// # Arguments
    ///
    /// * `start_time` - Earliest time at which the workflow should begin executing.
    pub fn with_start_time(mut self, start_time: SystemTime) -> Self {
        self.start_time = Some(start_time);
        self
    }

    pub(crate) fn input_json(self) -> dapr_durabletask::api::Result<Option<String>> {
        self.input
            .map(|value| serde_json::to_string(&value))
            .transpose()
            .map_err(Into::into)
    }

    pub(crate) fn start_time_utc(&self) -> Option<chrono::DateTime<chrono::Utc>> {
        self.start_time.map(chrono::DateTime::<chrono::Utc>::from)
    }
}

/// Options for calling an activity from a workflow.
#[derive(Debug, Clone, Default)]
pub struct ActivityOptions {
    /// Optional JSON-serialized input passed to the activity.
    pub input: Option<serde_json::Value>,
    /// Optional retry policy applied to the activity invocation.
    pub retry_policy: Option<RetryPolicy>,
    /// Optional scope controlling how caller history is propagated to the activity.
    pub history_propagation: Option<HistoryPropagationScope>,
}

impl ActivityOptions {
    /// Create an empty set of activity options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the JSON-serializable input passed to the activity.
    ///
    /// # Panics
    ///
    /// Panics if `input` cannot be serialized to JSON. Prefer
    /// [`ActivityOptions::try_with_input`] for fallible serialization.
    ///
    /// # Arguments
    ///
    /// * `input` - Value serialized to JSON and delivered as the activity input.
    pub fn with_input<T: Serialize>(self, input: T) -> Self {
        self.try_with_input(input)
            .expect("activity input must be JSON serializable")
    }

    /// Set the JSON-serializable input passed to the activity, returning an
    /// error if serialization fails.
    ///
    /// # Arguments
    ///
    /// * `input` - Value serialized to JSON and delivered as the activity input.
    pub fn try_with_input<T: Serialize>(mut self, input: T) -> Result<Self, serde_json::Error> {
        self.input = Some(serde_json::to_value(input)?);
        Ok(self)
    }

    /// Set the retry policy applied to the activity invocation.
    ///
    /// # Arguments
    ///
    /// * `retry_policy` - Policy describing retry attempts, intervals, and backoff.
    pub fn with_retry_policy(mut self, retry_policy: RetryPolicy) -> Self {
        self.retry_policy = Some(retry_policy);
        self
    }

    /// Set the history propagation scope applied to the activity invocation.
    ///
    /// # Arguments
    ///
    /// * `scope` - Scope controlling how caller history is propagated to the activity.
    pub fn with_history_propagation(mut self, scope: HistoryPropagationScope) -> Self {
        self.history_propagation = Some(scope);
        self
    }

    pub(crate) fn into_parts(self) -> (serde_json::Value, dapr_durabletask::task::ActivityOptions) {
        let input = self.input.unwrap_or(serde_json::Value::Null);
        let mut options = dapr_durabletask::task::ActivityOptions::new();
        if let Some(policy) = self.retry_policy {
            options = options.with_retry_policy(policy);
        }
        if let Some(scope) = self.history_propagation {
            options = options.with_history_propagation(scope);
        }
        (input, options)
    }
}

/// Options for calling a child workflow from a workflow.
#[derive(Debug, Clone, Default)]
pub struct SubWorkflowOptions {
    /// Optional JSON-serialized input passed to the child workflow.
    pub input: Option<serde_json::Value>,
    /// Optional caller-supplied instance ID for the child workflow.
    pub instance_id: Option<String>,
    /// Optional target app ID hosting the child workflow.
    pub app_id: Option<String>,
    /// Optional retry policy applied to the child workflow invocation.
    pub retry_policy: Option<RetryPolicy>,
    /// Optional scope controlling how caller history is propagated to the child workflow.
    pub history_propagation: Option<HistoryPropagationScope>,
}

impl SubWorkflowOptions {
    /// Create an empty set of child workflow options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the JSON-serializable input passed to the child workflow.
    ///
    /// # Panics
    ///
    /// Panics if `input` cannot be serialized to JSON. Prefer
    /// [`SubWorkflowOptions::try_with_input`] for fallible serialization.
    ///
    /// # Arguments
    ///
    /// * `input` - Value serialized to JSON and delivered as the child workflow input.
    pub fn with_input<T: Serialize>(self, input: T) -> Self {
        self.try_with_input(input)
            .expect("child workflow input must be JSON serializable")
    }

    /// Set the JSON-serializable input passed to the child workflow, returning
    /// an error if serialization fails.
    ///
    /// # Arguments
    ///
    /// * `input` - Value serialized to JSON and delivered as the child workflow input.
    pub fn try_with_input<T: Serialize>(mut self, input: T) -> Result<Self, serde_json::Error> {
        self.input = Some(serde_json::to_value(input)?);
        Ok(self)
    }

    /// Set an explicit instance ID for the child workflow.
    ///
    /// # Arguments
    ///
    /// * `instance_id` - Caller-supplied identifier for the child workflow instance.
    pub fn with_instance_id(mut self, instance_id: impl Into<String>) -> Self {
        self.instance_id = Some(instance_id.into());
        self
    }

    /// Set the target app ID hosting the child workflow.
    ///
    /// # Arguments
    ///
    /// * `app_id` - Dapr app ID that should execute the child workflow.
    pub fn with_app_id(mut self, app_id: impl Into<String>) -> Self {
        self.app_id = Some(app_id.into());
        self
    }

    /// Set the retry policy applied to the child workflow invocation.
    ///
    /// # Arguments
    ///
    /// * `retry_policy` - Policy describing retry attempts, intervals, and backoff.
    pub fn with_retry_policy(mut self, retry_policy: RetryPolicy) -> Self {
        self.retry_policy = Some(retry_policy);
        self
    }

    /// Set the history propagation scope applied to the child workflow invocation.
    ///
    /// # Arguments
    ///
    /// * `scope` - Scope controlling how caller history is propagated to the child workflow.
    pub fn with_history_propagation(mut self, scope: HistoryPropagationScope) -> Self {
        self.history_propagation = Some(scope);
        self
    }

    pub(crate) fn into_parts(
        self,
    ) -> (
        serde_json::Value,
        dapr_durabletask::task::SubOrchestratorOptions,
    ) {
        let input = self.input.unwrap_or(serde_json::Value::Null);
        let mut options = dapr_durabletask::task::SubOrchestratorOptions::new();
        if let Some(instance_id) = self.instance_id {
            options = options.with_instance_id(instance_id);
        }
        if let Some(app_id) = self.app_id {
            options = options.with_app_id(app_id);
        }
        if let Some(policy) = self.retry_policy {
            options = options.with_retry_policy(policy);
        }
        if let Some(scope) = self.history_propagation {
            options = options.with_history_propagation(scope);
        }
        (input, options)
    }
}

/// Options for raising an event to a workflow instance.
#[derive(Debug, Clone, Default)]
pub struct EventOptions {
    /// Optional JSON-serialized payload delivered with the event.
    pub payload: Option<serde_json::Value>,
}

impl EventOptions {
    /// Create an empty set of event options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the JSON-serializable payload delivered with the event.
    ///
    /// # Panics
    ///
    /// Panics if `payload` cannot be serialized to JSON. Prefer
    /// [`EventOptions::try_with_payload`] for fallible serialization.
    ///
    /// # Arguments
    ///
    /// * `payload` - Value serialized to JSON and delivered as the event payload.
    pub fn with_payload<T: Serialize>(self, payload: T) -> Self {
        self.try_with_payload(payload)
            .expect("event payload must be JSON serializable")
    }

    /// Set the JSON-serializable payload delivered with the event, returning
    /// an error if serialization fails.
    ///
    /// # Arguments
    ///
    /// * `payload` - Value serialized to JSON and delivered as the event payload.
    pub fn try_with_payload<T: Serialize>(mut self, payload: T) -> Result<Self, serde_json::Error> {
        self.payload = Some(serde_json::to_value(payload)?);
        Ok(self)
    }

    pub(crate) fn payload_json(self) -> dapr_durabletask::api::Result<Option<String>> {
        self.payload
            .map(|value| serde_json::to_string(&value))
            .transpose()
            .map_err(Into::into)
    }
}

/// Options for fetching workflow metadata.
#[derive(Debug, Clone, Copy, Default)]
pub struct FetchOptions {
    /// When `true`, include workflow inputs and outputs in the returned metadata.
    pub fetch_payloads: bool,
}

impl FetchOptions {
    /// Create an empty set of fetch options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set whether workflow inputs and outputs are included in the returned metadata.
    ///
    /// # Arguments
    ///
    /// * `fetch_payloads` - When `true`, include workflow inputs and outputs.
    pub fn with_fetch_payloads(mut self, fetch_payloads: bool) -> Self {
        self.fetch_payloads = fetch_payloads;
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::ser::Error as _;
    use serde::{Deserialize, Serialize, Serializer};
    use std::time::{Duration, UNIX_EPOCH};

    #[derive(Serialize, Deserialize)]
    struct Payload {
        name: String,
        count: u32,
    }

    struct AlwaysFails;

    impl Serialize for AlwaysFails {
        fn serialize<S: Serializer>(&self, _serializer: S) -> Result<S::Ok, S::Error> {
            Err(S::Error::custom("intentional serialization failure"))
        }
    }

    #[test]
    fn schedule_options_default_input_json_is_none() {
        let opts = ScheduleOptions::new();
        assert!(opts.input_json().unwrap().is_none());
    }

    #[test]
    fn schedule_options_input_json_round_trips_struct() {
        let opts = ScheduleOptions::new().with_input(Payload {
            name: "hello".into(),
            count: 3,
        });
        let json = opts.input_json().unwrap().expect("input present");
        let v: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(v["name"], "hello");
        assert_eq!(v["count"], 3);
    }

    #[test]
    fn schedule_options_input_json_preserves_escapes() {
        let opts = ScheduleOptions::new().with_input("line1\nline2\t\"quoted\"");
        let json = opts.input_json().unwrap().expect("input present");
        let decoded: String = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded, "line1\nline2\t\"quoted\"");
    }

    #[test]
    fn schedule_options_try_with_input_propagates_error() {
        let result = ScheduleOptions::new().try_with_input(AlwaysFails);
        assert!(result.is_err());
    }

    #[test]
    fn schedule_options_start_time_utc_handles_epoch() {
        let opts = ScheduleOptions::new().with_start_time(UNIX_EPOCH);
        let dt = opts.start_time_utc().expect("start time set");
        assert_eq!(dt.timestamp(), 0);
    }

    #[test]
    fn schedule_options_start_time_utc_none_by_default() {
        assert!(ScheduleOptions::new().start_time_utc().is_none());
    }

    #[test]
    fn event_options_payload_json_handles_none() {
        assert!(EventOptions::new().payload_json().unwrap().is_none());
    }

    #[test]
    fn event_options_payload_json_round_trips() {
        let opts = EventOptions::new().with_payload(vec![1, 2, 3]);
        let json = opts.payload_json().unwrap().expect("payload present");
        let v: Vec<i32> = serde_json::from_str(&json).unwrap();
        assert_eq!(v, vec![1, 2, 3]);
    }

    #[test]
    fn event_options_try_with_payload_propagates_error() {
        let result = EventOptions::new().try_with_payload(AlwaysFails);
        assert!(result.is_err());
    }

    #[test]
    fn activity_options_into_parts_defaults_to_null_input() {
        let (input, _) = ActivityOptions::new().into_parts();
        assert!(input.is_null());
    }

    #[test]
    fn activity_options_into_parts_carries_input() {
        let (input, _) = ActivityOptions::new().with_input(42_i32).into_parts();
        assert_eq!(input, serde_json::json!(42));
    }

    #[test]
    fn activity_options_into_parts_with_retry_and_propagation_compiles() {
        // We can't introspect the lowered ActivityOptions, but we can verify
        // the call chain accepts all builder methods and consumes self once.
        let policy = RetryPolicy::new(2, Duration::from_millis(10));
        let (_input, _task_opts) = ActivityOptions::new()
            .with_input("payload")
            .with_retry_policy(policy)
            .with_history_propagation(HistoryPropagationScope::OwnHistory)
            .into_parts();
    }

    #[test]
    fn sub_workflow_options_into_parts_defaults_to_null_input() {
        let (input, _) = SubWorkflowOptions::new().into_parts();
        assert!(input.is_null());
    }

    #[test]
    fn sub_workflow_options_into_parts_carries_input() {
        let (input, _) = SubWorkflowOptions::new()
            .with_input(Payload {
                name: "child".into(),
                count: 7,
            })
            .into_parts();
        assert_eq!(input["name"], "child");
        assert_eq!(input["count"], 7);
    }

    #[test]
    fn sub_workflow_options_full_builder_compiles() {
        let policy = RetryPolicy::new(3, Duration::from_millis(50));
        let (_input, _task_opts) = SubWorkflowOptions::new()
            .with_input("payload")
            .with_instance_id("child-id")
            .with_app_id("worker-app")
            .with_retry_policy(policy)
            .with_history_propagation(HistoryPropagationScope::Lineage)
            .into_parts();
    }

    #[test]
    fn fetch_options_with_fetch_payloads_toggles() {
        let opts = FetchOptions::new();
        assert!(!opts.fetch_payloads);
        let opts = opts.with_fetch_payloads(true);
        assert!(opts.fetch_payloads);
    }
}