Better handling of errors, not good, but better.
This commit is contained in:
@@ -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 {
|
||||
|
||||
18
src/main.rs
18
src/main.rs
@@ -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!();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<(), ()>;
|
||||
}
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user