fix: remove unwrap usage
This commit is contained in:
parent
6e98743d01
commit
43377441bd
3 changed files with 88 additions and 40 deletions
|
@ -70,7 +70,7 @@ pub trait DataSource {
|
|||
positions: &[String],
|
||||
path: &Path,
|
||||
client: &ureq::Agent,
|
||||
) {
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
info!(
|
||||
"{}: Retrieving data for {} at {}",
|
||||
self.get_alias(),
|
||||
|
@ -126,6 +126,10 @@ pub trait DataSource {
|
|||
champ.name,
|
||||
build.0
|
||||
);
|
||||
|
||||
let json_string = serde_json::to_string_pretty(&item_set)
|
||||
.map_err(|e| format!("Failed to serialize item set: {}", e))?;
|
||||
|
||||
fs::write(
|
||||
path.join(format!(
|
||||
"{}_{}_{}.json",
|
||||
|
@ -133,9 +137,10 @@ pub trait DataSource {
|
|||
champ.id,
|
||||
build.0
|
||||
)),
|
||||
serde_json::to_string_pretty(&item_set).unwrap(),
|
||||
json_string,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
.map_err(|e| format!("Failed to write item set file: {}", e))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
27
src/main.rs
27
src/main.rs
|
@ -74,7 +74,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
Ok(x) => x,
|
||||
Err(_e) => PathBuf::from(DEFAULT_LOL_CHAMPS_DIR),
|
||||
};
|
||||
info!("LoL Champs Folder: {}", lol_champs_dir.to_str().unwrap());
|
||||
info!("LoL Champs Folder: {}", lol_champs_dir.display());
|
||||
|
||||
let client: Agent = Agent::config_builder()
|
||||
.user_agent(USER_AGENT_VALUE)
|
||||
|
@ -175,8 +175,21 @@ fn get_and_write_item_set(
|
|||
error!("{}: {} empty positions", data_source.get_alias(), &champ_id);
|
||||
} else {
|
||||
let path = lol_champs_dir.join(&champ_id).join("Recommended");
|
||||
fs::create_dir_all(&path).unwrap();
|
||||
data_source.write_item_set(champ, positions, &path, client);
|
||||
match fs::create_dir_all(&path) {
|
||||
Ok(_) => match data_source.write_item_set(champ, positions, &path, client) {
|
||||
Ok(_) => (),
|
||||
Err(e) => error!(
|
||||
"{}: Failed to write item set for {} at {}: {}",
|
||||
data_source.get_alias(),
|
||||
champ.name,
|
||||
positions.join(", "),
|
||||
e
|
||||
),
|
||||
},
|
||||
Err(e) => {
|
||||
error!("Failed to create directory for {}: {}", champ_id, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
error!("{} not found in LoL champs", &champ_id);
|
||||
|
@ -193,8 +206,10 @@ fn lol_champ_dir() -> Result<PathBuf, Error> {
|
|||
REG_KEY_LOL_RADS
|
||||
);
|
||||
let val: String = node.get_value("LocalRootFolder")?;
|
||||
// TODO: remplacer ce .unwrap()
|
||||
PathBuf::from(val).parent().unwrap().to_path_buf()
|
||||
match PathBuf::from(val).parent() {
|
||||
Some(parent) => parent.to_path_buf(),
|
||||
None => return Err(Error::from(ErrorKind::NotFound)),
|
||||
}
|
||||
} else if let Ok(node) = hklm.open_subkey(REG_KEY_LOL_INC) {
|
||||
debug!(
|
||||
"Use registry key {} for relative champ directory",
|
||||
|
@ -238,7 +253,7 @@ fn find_subnode_from_path(reg: RegKey, path: &str, key: &str) -> io::Result<RegK
|
|||
if let Ok(node) = reg.open_subkey(path) {
|
||||
if let Some(k) = node
|
||||
.enum_keys()
|
||||
.map(|x| x.unwrap())
|
||||
.filter_map(Result::ok)
|
||||
.find(|x| x.starts_with(key))
|
||||
{
|
||||
return node.open_subkey(k);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use indexmap::IndexMap;
|
||||
use log::error;
|
||||
use regex::Regex;
|
||||
use serde_derive::Deserialize;
|
||||
use serde_json::Value;
|
||||
|
@ -19,19 +20,21 @@ struct MSChampion {
|
|||
fn get_champ_from_name(champs: &Champion, name: String) -> Option<u32> {
|
||||
for champ in champs.data.values() {
|
||||
if name == champ.name || name == champ.id {
|
||||
return Some(champ.key.parse::<u32>().unwrap());
|
||||
return champ.key.parse::<u32>().ok();
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn find_next_number(rest: &str) -> f32 {
|
||||
let re = Regex::new(r"([0-9]+\.?[0-9]+)").unwrap();
|
||||
let re = Regex::new(r"([0-9]+\.?[0-9]+)");
|
||||
if let Ok(re) = re {
|
||||
if let Some(cap) = re.captures(rest) {
|
||||
if let Some(matched) = cap.get(1) {
|
||||
return matched.as_str().parse::<f32>().unwrap_or(0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
0.0
|
||||
}
|
||||
|
||||
|
@ -51,16 +54,17 @@ impl DataSource for MSDataSource {
|
|||
) -> IndexMap<u32, Vec<String>> {
|
||||
let mut champs = IndexMap::new();
|
||||
|
||||
let champions: Vec<MSChampion> = client
|
||||
let champions: Vec<MSChampion> = match client
|
||||
.get("https://www.metasrc.com/lol/search/lol")
|
||||
.call()
|
||||
.unwrap()
|
||||
.body_mut()
|
||||
.read_json()
|
||||
.unwrap();
|
||||
.and_then(|mut resp| resp.body_mut().read_json())
|
||||
{
|
||||
Ok(champs) => champs,
|
||||
Err(_) => return champs,
|
||||
};
|
||||
|
||||
for champ in champions {
|
||||
let id = get_champ_from_name(champion, champ.name).unwrap();
|
||||
if let Some(id) = get_champ_from_name(champion, champ.name.to_owned()) {
|
||||
let allowed_roles = ["TOP", "ADC", "SUPPORT", "JUNGLE", "MID"];
|
||||
let roles = champ
|
||||
.search_terms
|
||||
|
@ -69,6 +73,9 @@ impl DataSource for MSDataSource {
|
|||
.filter(|role| allowed_roles.contains(&role.as_str()))
|
||||
.collect::<Vec<String>>();
|
||||
champs.insert(id, roles);
|
||||
} else {
|
||||
error!("Could not find champ {} in champion data", champ.name);
|
||||
}
|
||||
}
|
||||
|
||||
champs
|
||||
|
@ -93,26 +100,47 @@ impl DataSource for MSDataSource {
|
|||
)
|
||||
.call();
|
||||
if let Ok(mut p) = rep {
|
||||
let page = p.body_mut().read_to_string().unwrap();
|
||||
let mut pos = page.find("Patch ").unwrap();
|
||||
let patch = find_next_number(&page[pos..]).to_string();
|
||||
let page = match p.body_mut().read_to_string() {
|
||||
Ok(s) => s,
|
||||
Err(_) => return builds,
|
||||
};
|
||||
|
||||
pos = page.find("Win").unwrap();
|
||||
let win_rate: f32 = find_next_number(&page[pos..]);
|
||||
let mut pos = page.find("Patch ");
|
||||
let patch = if let Some(p) = pos {
|
||||
find_next_number(&page[p..]).to_string()
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
pos = page.find("KDA:").unwrap();
|
||||
let kda: f32 = find_next_number(&page[pos..]);
|
||||
pos = page.find("Win");
|
||||
let win_rate: f32 = if let Some(p) = pos {
|
||||
find_next_number(&page[p..])
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
|
||||
pos = page.find("Games:").unwrap();
|
||||
let games: u32 = find_next_number(&page[pos..]) as u32;
|
||||
pos = page.find("KDA:");
|
||||
let kda: f32 = if let Some(p) = pos {
|
||||
find_next_number(&page[p..])
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
|
||||
pos = page.find("Games:");
|
||||
let games: u32 = if let Some(p) = pos {
|
||||
find_next_number(&page[p..]) as u32
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
let mut items = vec![];
|
||||
let mut pos: Option<usize> = page.find("/item/");
|
||||
while let Some(mut p) = pos {
|
||||
p += 6;
|
||||
let i = &page[p..p + page[p..].find('.').unwrap()];
|
||||
|
||||
if let Some(dot_pos) = page[p..].find('.') {
|
||||
let i = &page[p..p + dot_pos];
|
||||
items.push(i);
|
||||
}
|
||||
|
||||
let next = page[p..].find("/item/");
|
||||
if let Some(n) = next {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue