Skip to content

Instantly share code, notes, and snippets.

Created April 23, 2017 15:01
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 anonymous/9ac364b7517cd787b1102b4849345620 to your computer and use it in GitHub Desktop.
Save anonymous/9ac364b7517cd787b1102b4849345620 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
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