Skip to content

Instantly share code, notes, and snippets.

@tommymalmqvist
Created April 17, 2022 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommymalmqvist/2189f007f5a27e318803291c91c64284 to your computer and use it in GitHub Desktop.
Save tommymalmqvist/2189f007f5a27e318803291c91c64284 to your computer and use it in GitHub Desktop.
#[cfg(test)]
mod tests {
use serde_json;
#[test]
fn test_new() {
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase", untagged)]
enum Kind {
Authorization(AuthorizationKind),
Notification(NotificationKind),
}
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase", untagged)]
enum AuthorizationKind {
Request(AuthorizationRequest),
Response(AuthorizationResponse),
}
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
struct AuthorizationRequest {
request: String,
code: usize,
secret: String,
}
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
struct AuthorizationResponse {
response: String,
code: usize,
authorized: bool,
token: Token,
}
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
struct Token {
token: String,
}
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
enum NotificationKind {
Request(NotificationRequest),
Response(NotificationResponse),
}
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
struct NotificationRequest {
request: String,
code: usize,
}
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
struct NotificationResponse {
response: String,
}
let json = r#"["authorize request incoming",2,"TH15 15 MY! 53CR3T"]"#;
let request: Result<Kind, _> = serde_json::from_str(&json);
assert_eq!(request.is_ok(), true);
let request = request.unwrap();
let back_to_json = serde_json::to_string(&request).unwrap();
println!("{back_to_json}");
assert_eq!(json, back_to_json);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment