From 8f0af3e76bda8e2c21707a0a5216a143219c3977 Mon Sep 17 00:00:00 2001 From: nyyu Date: Fri, 29 Oct 2021 19:59:29 +0200 Subject: [PATCH] fix panic --- src/cgg_data_source.rs | 23 ++++++++++++----------- src/pb_data_source.rs | 34 ++++++++++++++++++---------------- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/cgg_data_source.rs b/src/cgg_data_source.rs index f9ee824..48fe98a 100644 --- a/src/cgg_data_source.rs +++ b/src/cgg_data_source.rs @@ -199,17 +199,18 @@ impl DataSource for CGGDataSource { } } - let stat = champs_stats.get(&key).unwrap(); - data.push(( - position.to_owned(), - blocks, - Stat { - win_rate: stat.win_rate.unwrap(), - games: stat.games.unwrap(), - kda: stat.kda.unwrap(), - patch: champ.patch.to_owned(), - }, - )); + if let Some(stat) = champs_stats.get(&key) { + data.push(( + position.to_owned(), + blocks, + Stat { + win_rate: stat.win_rate.unwrap(), + games: stat.games.unwrap(), + kda: stat.kda.unwrap(), + patch: champ.patch.to_owned(), + }, + )); + } } } data diff --git a/src/pb_data_source.rs b/src/pb_data_source.rs index d64dc21..685ea96 100644 --- a/src/pb_data_source.rs +++ b/src/pb_data_source.rs @@ -61,26 +61,28 @@ impl DataSource for PBDataSource { ) -> Vec<(String, Vec, Stat)> { let mut builds = vec![]; - let page = client + let rep = client .get(format!("https://www.probuilds.net/ajax/games?limit=1&sort=gameDate-desc&championId={}&olderThan=0&lane=®ion=", champ.key).as_str()) .set("Accept", "application/json") - .call().unwrap().into_string().unwrap(); + .call(); - let mut items = vec![]; - let mut build = &page[page.find("'items'").unwrap()..]; - while build.contains("items") { - let pos = build.find("data-id=").unwrap(); - build = &build[pos + 10..]; - let end = build.find('\\').unwrap(); - items.push(&build[..end]); - build = &build[end + 20..]; + if let Ok(page) = rep { + let str = page.into_string().unwrap(); + let mut items = vec![]; + let mut build = &str[str.find("'items'").unwrap()..]; + while build.contains("items") { + let pos = build.find("data-id=").unwrap(); + build = &build[pos + 10..]; + let end = build.find('\\').unwrap(); + items.push(&build[..end]); + build = &build[end + 20..]; + } + + builds.push((positions[0].to_owned(), vec![json!({ + "items": items.iter().map(|x| json!({"id": x.to_string(), "count": 1})).collect::>(), + "type": "Set" + })], Stat{win_rate:0.0,games:1,kda:0.0,patch:"".to_string()})); } - - builds.push((positions[0].to_owned(), vec![json!({ - "items": items.iter().map(|x| json!({"id": x.to_string(), "count": 1})).collect::>(), - "type": "Set" - })], Stat{win_rate:0.0,games:1,kda:0.0,patch:"".to_string()})); - builds } }