use crate::{
http::Body,
rpc::{typed_data::Data, TypedData},
};
use serde::{Deserialize, Serialize};
use serde_json::{from_str, to_string};
use std::borrow::Cow;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SignalRConnectionInfo {
pub url: String,
pub access_token: String,
}
#[doc(hidden)]
impl From<TypedData> for SignalRConnectionInfo {
fn from(data: TypedData) -> Self {
match &data.data {
Some(Data::Json(s)) => from_str(s).expect("failed to parse SignalR connection info"),
_ => panic!("expected JSON data for SignalR connection info"),
}
}
}
impl<'a> Into<Body<'a>> for SignalRConnectionInfo {
fn into(self) -> Body<'a> {
Body::Json(Cow::from(
to_string(&self).expect("failed to serialize SignalR connection info"),
))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_serializes_to_json() {
let json = to_string(&SignalRConnectionInfo {
url: "foo".to_owned(),
access_token: "bar".to_owned(),
})
.unwrap();
assert_eq!(json, r#"{"url":"foo","accessToken":"bar"}"#);
}
#[test]
fn it_converts_from_typed_data() {
let data = TypedData {
data: Some(Data::Json(
r#"{ "url": "foo", "accessToken": "bar"}"#.to_owned(),
)),
};
let info: SignalRConnectionInfo = data.into();
assert_eq!(info.url, "foo");
assert_eq!(info.access_token, "bar");
}
#[test]
fn it_converts_to_body() {
let info = SignalRConnectionInfo {
url: "foo".to_owned(),
access_token: "bar".to_owned(),
};
let body: Body = info.into();
assert_eq!(
body.as_str().unwrap(),
r#"{"url":"foo","accessToken":"bar"}"#
);
}
}