aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNerdyPepper <[email protected]>2018-06-22 16:19:09 +0100
committerNerdyPepper <[email protected]>2018-06-22 16:19:09 +0100
commitcc487788e0404bb99e08f19e21098a613354bbd5 (patch)
treea6a08ffd8227e83f5e2ba150448da5a6f1a27c20 /src
parent59264a5516a6782608d9967015d0a7ab60b04b42 (diff)
Ignore serialization errors
Diffstat (limited to 'src')
-rw-r--r--src/content.rs35
1 files changed, 13 insertions, 22 deletions
diff --git a/src/content.rs b/src/content.rs
index ba1a460..117c6d0 100644
--- a/src/content.rs
+++ b/src/content.rs
@@ -9,7 +9,7 @@ use reqwest::Response;
9 9
10pub fn query_url_gen(title: &str) -> String { 10pub fn query_url_gen(title: &str) -> String {
11 11
12 title.replace(" ", "%20"); 12// /w/api.php?action=query&format=json&prop=extracts&titles=dota_2&exlimit=20&explaintext=1
13 13
14 // query config 14 // query config
15 let mut url = String::from("https://en.wikipedia.org"); 15 let mut url = String::from("https://en.wikipedia.org");
@@ -17,6 +17,7 @@ pub fn query_url_gen(title: &str) -> String {
17 url.push_str("action=query&"); 17 url.push_str("action=query&");
18 url.push_str("format=json&"); 18 url.push_str("format=json&");
19 url.push_str("prop=extracts&"); 19 url.push_str("prop=extracts&");
20 url.push_str("indexpageids=1&");
20 url.push_str("titles="); 21 url.push_str("titles=");
21 url.push_str(title); 22 url.push_str(title);
22 url.push_str("&"); 23 url.push_str("&");
@@ -43,11 +44,9 @@ pub fn search_url_gen(search: &str) -> String {
43 44
44} 45}
45 46
46pub fn get_extract(title: &str) -> Result<String, reqwest::Error> { 47pub fn get_extract(mut res: Response) -> Result<String, reqwest::Error> {
47 let url = query_url_gen(title);
48 let res = reqwest::get(&url[..])?;
49 48
50 let mut v: Value = match serde_json::from_str(&res.text()?) { 49 let v: Value = match serde_json::from_str(&res.text()?) {
51 Ok(x) => x, 50 Ok(x) => x,
52 Err(x) => panic!("Failed to parse json\nReceived error {}", x), 51 Err(x) => panic!("Failed to parse json\nReceived error {}", x),
53 }; 52 };
@@ -69,19 +68,19 @@ pub fn get_extract(title: &str) -> Result<String, reqwest::Error> {
69 } 68 }
70} 69}
71 70
72pub fn get_title(title: &str, mut res: Response) -> String { 71pub fn get_title(title: &str, mut res: Response) -> Result<String, reqwest::Error> {
73 let mut v: Value = serde_json::from_str(&res.text().unwrap()) 72 let v: Value = serde_json::from_str(&res.text()?)
74 .unwrap_or_else( |e| { 73 .unwrap_or_else( |e| {
75 panic!("Recieved error {:?}", e); 74 panic!("Recieved error {:?}", e);
76 } ); 75 } );
77 format!("{}", &v["query"]["normalized"][0]["to"]) 76 Ok(format!("{}", &v["query"]["normalized"][0]["to"]))
78} 77}
79 78
80pub fn get_search_results(search: &str) -> Result<Vec<String>, reqwest::Error> { 79pub fn get_search_results(search: &str) -> Result<Vec<String>, reqwest::Error> {
81 80
82 let url = search_url_gen(search); 81 let url = search_url_gen(search);
83 let res = reqwest::get(&url[..])?; 82 let mut res = reqwest::get(&url[..])?;
84 let mut v: Value = serde_json::from_str(&res.text().unwrap()) 83 let v: Value = serde_json::from_str(&res.text().unwrap())
85 .unwrap_or_else( |e| { 84 .unwrap_or_else( |e| {
86 panic!("Recieved error {:?}", e); 85 panic!("Recieved error {:?}", e);
87 } ); 86 } );
@@ -99,28 +98,20 @@ pub fn get_search_results(search: &str) -> Result<Vec<String>, reqwest::Error> {
99 98
100pub fn pop_error(s: &mut Cursive, msg: String) { 99pub fn pop_error(s: &mut Cursive, msg: String) {
101 s.add_layer(Dialog::text(format!("{}", msg)) 100 s.add_layer(Dialog::text(format!("{}", msg))
102 .title("Error")
103 .button("Ok", |s| s.quit())); 101 .button("Ok", |s| s.quit()));
104} 102}
105 103
106pub fn handler(e: reqwest::Error) -> String { 104pub fn handler(e: reqwest::Error) -> String {
107 let msg: String = String::new(); 105 let mut msg: String = String::new();
108 if e.is_http() { 106 if e.is_http() {
109 match e.url() { 107 match e.url() {
110 None => msg = format!("No URL given"), 108 None => msg.push_str(&format!("No URL given")),
111 Some(url) => msg = format!("Problem making request to: {}", url), 109 Some(url) => msg.push_str(&format!("Problem making request to: {}", url)),
112 } 110 }
113 } 111 }
114 // Inspect the internal error and output it
115 if e.is_serialization() {
116 let serde_error = match e.get_ref() {
117 Some(err) => err,
118 };
119 msg.push(format!("\nproblem parsing information {}", serde_error));
120 }
121 112
122 if e.is_redirect() { 113 if e.is_redirect() {
123 msg.push(format!("server redirecting too many times or making loop")); 114 msg.push_str(&format!("server redirecting too many times or making loop"));
124 } 115 }
125 116
126 msg 117 msg