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
//! Module for generic Azure Function bindings.
use crate::{
    http::Body,
    rpc::{typed_data::Data, TypedData},
};
use serde_json::from_str;
use std::borrow::Cow;

/// Represents a value passed to or from a generic binding.
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
    /// No data is present.
    None,
    /// The value is a string.
    String(String),
    /// The value is a JSON value.
    Json(serde_json::Value),
    /// The value is a sequence of bytes.
    Bytes(Vec<u8>),
    /// The value is an integer.
    Integer(i64),
    /// The value is a double.
    Double(f64),
}

impl<'a> Into<Body<'a>> for Value {
    fn into(self) -> Body<'a> {
        match self {
            Value::None => Body::Empty,
            Value::String(s) => Body::String(Cow::Owned(s)),
            Value::Json(v) => Body::Json(Cow::Owned(v.to_string())),
            Value::Bytes(b) => Body::Bytes(Cow::Owned(b)),
            Value::Integer(i) => Body::String(Cow::Owned(i.to_string())),
            Value::Double(d) => Body::String(Cow::Owned(d.to_string())),
        }
    }
}

#[doc(hidden)]
impl From<TypedData> for Value {
    fn from(data: TypedData) -> Self {
        match data.data {
            None => Value::None,
            Some(Data::String(s)) => Value::String(s),
            Some(Data::Json(s)) => Value::Json(from_str(&s).unwrap()),
            Some(Data::Bytes(b)) => Value::Bytes(b),
            Some(Data::Stream(b)) => Value::Bytes(b),
            Some(Data::Http(_)) => panic!("generic bindings cannot contain HTTP data"),
            Some(Data::Int(i)) => Value::Integer(i),
            Some(Data::Double(d)) => Value::Double(d),
        }
    }
}

#[doc(hidden)]
impl Into<TypedData> for Value {
    fn into(self) -> TypedData {
        match self {
            Value::None => TypedData { data: None },
            Value::String(s) => TypedData {
                data: Some(Data::String(s)),
            },
            Value::Json(v) => TypedData {
                data: Some(Data::Json(v.to_string())),
            },
            Value::Bytes(b) => TypedData {
                data: Some(Data::Bytes(b)),
            },
            Value::Integer(i) => TypedData {
                data: Some(Data::Int(i)),
            },
            Value::Double(d) => TypedData {
                data: Some(Data::Double(d)),
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rpc::typed_data::Data;
    use serde_json::json;

    #[test]
    fn it_converts_from_no_typed_data() {
        let value: Value = TypedData { data: None }.into();

        assert_eq!(value, Value::None);
    }

    #[test]
    fn it_converts_from_string_typed_data() {
        let value: Value = TypedData {
            data: Some(Data::String("hello world".into())),
        }
        .into();

        assert_eq!(value, Value::String("hello world".into()));
    }

    #[test]
    fn it_converts_from_json_typed_data() {
        let value: Value = TypedData {
            data: Some(Data::Json(r#"{ "foo": "bar" }"#.to_string())),
        }
        .into();

        assert_eq!(value, Value::Json(json!({ "foo": "bar" })));
    }

    #[test]
    fn it_converts_from_bytes_typed_data() {
        let value: Value = TypedData {
            data: Some(Data::Bytes(vec![1, 2, 3])),
        }
        .into();

        assert_eq!(value, Value::Bytes(vec![1, 2, 3]));
    }

    #[test]
    fn it_converts_from_stream_typed_data() {
        let value: Value = TypedData {
            data: Some(Data::Stream(vec![1, 2, 3])),
        }
        .into();

        assert_eq!(value, Value::Bytes(vec![1, 2, 3]));
    }

    #[test]
    fn it_converts_from_integer_typed_data() {
        let value: Value = TypedData {
            data: Some(Data::Int(12345)),
        }
        .into();

        assert_eq!(value, Value::Integer(12345));
    }

    #[test]
    fn it_converts_from_double_typed_data() {
        let value: Value = TypedData {
            data: Some(Data::Double(12345.6)),
        }
        .into();

        assert_eq!(value, Value::Double(12345.6));
    }

    #[test]
    fn it_converts_to_no_typed_data() {
        let data: TypedData = Value::None.into();

        assert_eq!(data.data, None);
    }

    #[test]
    fn it_converts_to_string_typed_data() {
        let data: TypedData = Value::String("hello world".to_owned()).into();

        assert_eq!(data.data, Some(Data::String("hello world".to_owned())));
    }

    #[test]
    fn it_converts_to_json_typed_data() {
        let data: TypedData = Value::Json(json!({ "foo": "bar"})).into();

        assert_eq!(data.data, Some(Data::Json(r#"{"foo":"bar"}"#.to_string())));
    }

    #[test]
    fn it_converts_to_bytes_typed_data() {
        let data: TypedData = Value::Bytes(vec![1, 2, 3]).into();

        assert_eq!(data.data, Some(Data::Bytes(vec![1, 2, 3])));
    }

    #[test]
    fn it_converts_to_integer_typed_data() {
        let data: TypedData = Value::Integer(12345).into();

        assert_eq!(data.data, Some(Data::Int(12345)));
    }

    #[test]
    fn it_converts_to_double_typed_data() {
        let data: TypedData = Value::Double(12345.6).into();

        assert_eq!(data.data, Some(Data::Double(12345.6)));
    }
}