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

@@ -2,7 +2,7 @@ use std::fs;
use std::io;
use directories::ProjectDirs;
use serde_derive::{Deserialize, Serialize};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Cache {

View File

@@ -1,4 +1,4 @@
use std::env;
use structopt::StructOpt;
mod context;
mod probe;
@@ -6,10 +6,14 @@ mod probe;
use crate::context::Context;
use crate::probe::*;
#[derive(Debug, StructOpt)]
#[structopt(name = "whoareyou", about = "Search for swedish phone numbers.")]
struct Opt {
number: String,
}
fn main() {
let number = env::args()
.nth(1)
.expect("must specify a number to search for");
let opt = Opt::from_args();
let mut probes: Vec<Box<Probe>> = vec![
Box::new(Eniro),
@@ -22,8 +26,8 @@ fn main() {
let mut ctx = Context::new();
for probe in &mut probes {
probe.search(&mut ctx, &number);
println!();
if probe.search(&mut ctx, &opt.number).is_ok() {
println!();
}
}
}

View File

@@ -13,5 +13,5 @@ pub use self::vem_ringde::VemRingde;
use crate::context::Context;
pub trait Probe {
fn search(&mut self, _: &mut Context, _: &str);
fn search(&mut self, _: &mut Context, _: &str) -> Result<(), ()>;
}

View File

@@ -15,7 +15,7 @@ struct Error {
pub struct Eniro;
impl Probe for Eniro {
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("eniro", &number) {
String::from_utf8(cache.data).unwrap()
} else {
@@ -28,12 +28,13 @@ impl Probe for Eniro {
ctx.cache_set("eniro", &number, body.as_bytes())
.expect("wut?! why not?!");
println!("eniro.se:");
if let Ok(error) = Error::from_html(&body) {
println!("eniro.se:");
println!(" {}", error.message);
Ok(())
} else {
println!(" Failed to find any data");
Err(())
}
}
}

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(())
}
}
}

View File

@@ -15,7 +15,7 @@ struct Info {
pub struct KonsumentInfo;
impl Probe for KonsumentInfo {
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("konsument_info", &number) {
String::from_utf8(cache.data).unwrap()
} else {
@@ -38,5 +38,7 @@ impl Probe for KonsumentInfo {
} else {
println!(" Failed to find any data");
}
Ok(())
}
}

View File

@@ -15,7 +15,7 @@ struct Info {
pub struct Telefonforsaljare;
impl Probe for Telefonforsaljare {
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("telefonforsaljare", &number) {
String::from_utf8(cache.data).unwrap()
} else {
@@ -38,5 +38,7 @@ impl Probe for Telefonforsaljare {
} else {
println!(" Failed to find any data");
}
Ok(())
}
}

View File

@@ -5,7 +5,7 @@ use crate::probe::Probe;
pub struct VemRingde;
impl Probe for VemRingde {
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("vem_ringde", &number) {
String::from_utf8(cache.data).unwrap()
} else {
@@ -17,5 +17,7 @@ impl Probe for VemRingde {
ctx.cache_set("vem_ringde", &number, body.as_bytes())
.expect("wut?! why not?!");
Err(())
}
}