use crate::{
rpc::{typed_data::Data, TypedData},
FromVec,
};
use serde::{Deserialize, Serialize};
use serde_json::{to_string, to_value, Value};
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TwilioSmsMessage {
pub to: String,
pub from: Option<String>,
pub body: Option<String>,
}
#[doc(hidden)]
impl Into<TypedData> for TwilioSmsMessage {
fn into(self) -> TypedData {
TypedData {
data: Some(Data::Json(
to_string(&self).expect("failed to convert Twilio SMS message to JSON string"),
)),
}
}
}
#[doc(hidden)]
impl FromVec<TwilioSmsMessage> for TypedData {
fn from_vec(vec: Vec<TwilioSmsMessage>) -> Self {
TypedData {
data: Some(Data::Json(
Value::Array(vec.into_iter().map(|m| to_value(m).unwrap()).collect()).to_string(),
)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_serializes_to_json() {
let json = to_string(&TwilioSmsMessage {
to: "foo".to_owned(),
from: Some("bar".to_owned()),
body: Some("baz".to_owned()),
})
.unwrap();
assert_eq!(json, r#"{"to":"foo","from":"bar","body":"baz"}"#);
}
#[test]
fn it_converts_to_typed_data() {
let message = TwilioSmsMessage {
to: "foo".to_owned(),
from: Some("bar".to_owned()),
body: Some("baz".to_owned()),
};
let data: TypedData = message.into();
assert_eq!(
data.data,
Some(Data::Json(
r#"{"to":"foo","from":"bar","body":"baz"}"#.to_string()
))
);
}
}