Better handling of errors, not good, but better.

This commit is contained in:
2019-01-17 10:46:46 +01:00
parent b214f9b621
commit a3e515c3a7
11 changed files with 162 additions and 25 deletions

View File

@@ -1,5 +1,5 @@
use regex::Regex;
use serde_derive::Deserialize;
use serde::Deserialize;
use crate::context::Context;
use crate::probe::Probe;
@@ -43,7 +43,7 @@ struct Comment {
pub struct Hitta;
impl Probe for Hitta {
fn search(&mut self, ctx: &mut Context, number: &str) {
fn search(&mut self, ctx: &mut Context, number: &str) -> Result<(), ()> {
let body = if let Some(cache) = ctx.cache_get("hitta", &number) {
String::from_utf8(cache.data).unwrap()
} else {
@@ -61,17 +61,20 @@ impl Probe for Hitta {
if let Some(result) = re.captures(&body) {
let json = result.get(1).unwrap().as_str();
println!("hitta.se:");
if let Ok(data) = serde_json::from_str::<Data>(&json) {
println!("hitta.se:");
println!(" {}", data.props.page_props.phone_data.statistics_text);
for comment in &data.props.page_props.phone_data.comments {
println!(" * {}", comment.comment);
}
Ok(())
} else {
println!(" Failed to find any data");
Err(())
}
} else {
Err(())
}
}
}