azure_functions/durable/
activity_output.rs

1use crate::rpc::{typed_data::Data, TypedData};
2use serde_json::Value;
3use std::iter::FromIterator;
4
5/// Represents the output of a Durable Functions activity function.
6///
7/// Supports conversion from JSON-compatible types.
8pub struct ActivityOutput(Value);
9
10impl<T> From<T> for ActivityOutput
11where
12    T: Into<Value>,
13{
14    fn from(t: T) -> Self {
15        ActivityOutput(t.into())
16    }
17}
18
19impl FromIterator<Value> for ActivityOutput {
20    fn from_iter<I>(iter: I) -> Self
21    where
22        I: IntoIterator<Item = Value>,
23    {
24        ActivityOutput(Value::from_iter(iter))
25    }
26}
27
28#[doc(hidden)]
29impl Into<TypedData> for ActivityOutput {
30    fn into(self) -> TypedData {
31        TypedData {
32            data: Some(Data::Json(self.0.to_string())),
33        }
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40    use serde_json::json;
41
42    #[test]
43    fn it_converts_from_json() {
44        let activity_output: ActivityOutput = json!({ "foo": "bar" }).into();
45
46        let data: TypedData = activity_output.into();
47        assert_eq!(data.data, Some(Data::Json(r#"{"foo":"bar"}"#.to_string())));
48    }
49
50    #[test]
51    fn it_converts_from_bool() {
52        let activity_output: ActivityOutput = true.into();
53
54        let data: TypedData = activity_output.into();
55        assert_eq!(data.data, Some(Data::Json(true.to_string())));
56    }
57
58    #[test]
59    fn it_converts_from_string() {
60        let activity_output: ActivityOutput = "foo".into();
61
62        let data: TypedData = activity_output.into();
63        assert_eq!(data.data, Some(Data::Json("\"foo\"".to_string())));
64    }
65}