Skip to content

Instantly share code, notes, and snippets.

@nlinker
Forked from rust-play/playground.rs
Created November 17, 2018 09:41
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 nlinker/ee6af9b5caae1365e6c522327397cc03 to your computer and use it in GitHub Desktop.
Save nlinker/ee6af9b5caae1365e6c522327397cc03 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#[derive(Debug)]
struct OrderBook {
pair_name: String,
pair_id: u64,
bids: HashMap<String, String>,
asks: HashMap<String, String>,
}
fn parse_poloniex_full_order_book(msg: &str) -> OrderBook{
let f:Value = serde_json::from_str(msg).unwrap();
let pair_id = f[0].as_u64().unwrap();
let pair_name = f[2][0][1]["currencyPair"].as_str().unwrap();
let asks = &f[2][0][1]["orderBook"][0].as_object().unwrap();
let bids = &f[2][0][1]["orderBook"][1].as_object().unwrap();
//println!("{:?}", asks);
let asks: HashMap<String, String>= asks.iter().map(|(price, vol)|{
(price.clone(), vol.as_str().unwrap().to_string())
}).collect();
let bids: HashMap<String, String>= bids.iter().map(|(price, vol)|{
(price.clone(), vol.as_str().unwrap().to_string())
}).collect();
OrderBook {
pair_name: pair_name.to_string(),
pair_id,
asks,
bids,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment