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