Created
April 23, 2017 15:01
-
-
Save anonymous/9ac364b7517cd787b1102b4849345620 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate rustc_serialize; | |
use rustc_serialize::{ Decodable, Decoder }; | |
#[derive(Debug)] | |
pub enum CallbackQueryData { | |
String(String), | |
GameShortName(String), | |
} | |
impl Decodable for CallbackQueryData { | |
fn decode<D: Decoder>(d: &mut D) -> Result<CallbackQueryData, D::Error> { | |
if let Some(data) = d.read_struct_field("data", 0, Decodable::decode)? { | |
return Ok(CallbackQueryData::String(data)); | |
} | |
if let Some(game_short_name) = d.read_struct_field("game_short_name", 0, Decodable::decode)? { | |
return Ok(CallbackQueryData::GameShortName(game_short_name)); | |
} | |
unreachable!() | |
} | |
} | |
// {"id":32,"chat_instance":"3986534","data":"Foo"} | |
// {"id":32,"chat_instance":"3986534","game_short_name":"My Game"} | |
#[derive(Debug)] | |
pub struct CallbackQuery { | |
id: u32, | |
chat_instance: String, | |
data: CallbackQueryData, | |
} | |
impl Decodable for CallbackQuery { | |
fn decode<D: Decoder>(d: &mut D) -> Result<CallbackQuery, D::Error> { | |
d.read_struct("CallbackQuery", 5, |d| { | |
let id = d.read_struct_field("id", 0, Decodable::decode)?; | |
let chat_instance = d.read_struct_field("chat_instance", 4, Decodable::decode)?; | |
let data = Decodable::decode(d)?; | |
Ok(CallbackQuery { id, chat_instance, data }) | |
}) | |
} | |
} | |
fn main () {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment