dapr_durabletask/api/external_event.rs
1use serde::de::DeserializeOwned;
2
3/// Result of waiting for an external event with a timeout.
4///
5/// Returned by [`OrchestrationContext::wait_for_external_event_with_timeout`].
6///
7/// [`OrchestrationContext::wait_for_external_event_with_timeout`]: crate::task::OrchestrationContext::wait_for_external_event_with_timeout
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum ExternalEventResult {
10 /// Event received before the timeout.
11 ///
12 /// Raw JSON payload, or `None` if the event carried no data.
13 Received(Option<String>),
14
15 /// Event not received before the timeout.
16 TimedOut,
17}
18
19impl ExternalEventResult {
20 /// `true` if the event was received.
21 pub fn is_received(&self) -> bool {
22 matches!(self, Self::Received(_))
23 }
24
25 /// `true` if the wait timed out.
26 pub fn is_timed_out(&self) -> bool {
27 matches!(self, Self::TimedOut)
28 }
29
30 /// Deserialise the payload, returning `None` if timed out or absent.
31 pub fn deserialize<T: DeserializeOwned>(&self) -> Option<super::Result<T>> {
32 match self {
33 Self::Received(Some(json)) => {
34 Some(serde_json::from_str(json).map_err(super::DurableTaskError::Serialization))
35 }
36 _ => None,
37 }
38 }
39}