From 4d241131fc88c962cf7f5abf77b53c77b6d238c7 Mon Sep 17 00:00:00 2001 From: Waffle Date: Thu, 5 Sep 2019 19:12:03 +0300 Subject: [PATCH] Make `ResponseParameters` more rust-friendly --- Cargo.toml | 2 ++ src/core/types/response_parameter.rs | 31 +++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ec12fd2f..f9f76f12 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,5 @@ serde = {version = "1.0.92", features = ["derive"] } lazy_static = "1.3" apply = "0.2.2" derive_more = "0.15.0" + +tokio = "0.1" \ No newline at end of file diff --git a/src/core/types/response_parameter.rs b/src/core/types/response_parameter.rs index 9940925b..5ec3c8dd 100644 --- a/src/core/types/response_parameter.rs +++ b/src/core/types/response_parameter.rs @@ -1,5 +1,30 @@ #[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone)] -pub struct ResponseParameters { - pub migrate_to_chat_id: Option, - pub retry_after: Option, +#[serde(tag = "type")] +#[serde(rename_all = "snake_case")] +pub enum ResponseParameters { + MigrateToChatId(i64), + RetryAfter(i64), +} + +#[cfg(test)] +mod tests { + #[test] + fn migrate_to_chat_id_deserialization() { + let expected_struct = ResponseParameters::MigrateToChatId(123456); + let actual_json: ResponseParameters = serde_json::from_str( + r#"{"migrate_to_chat_id":123456}"# + ).unwrap(); + + assert_eq!(expected_json, actual_json); + } + + #[test] + fn retry_after_deserialization() { + let expected_struct = ResponseParameters::RetryAfter(123456); + let actual_json: ResponseParameters = serde_json::from_str( + r#"{"retry_after":123456}"# + ).unwrap(); + + assert_eq!(expected_json, actual_json); + } } \ No newline at end of file