-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
multithreading, error handling
- Loading branch information
Showing
6 changed files
with
313 additions
and
215 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,31 @@ | ||
use colored::*; | ||
use std::{fs, io::Write, path::Path, str}; | ||
use std::{fs, io::Write, path::Path}; | ||
|
||
pub fn get_body(url: &str) -> Vec<u8> { | ||
pub fn get_body(url: &str) -> Result<Vec<u8>, String> { | ||
let mut res: Vec<u8> = Vec::new(); | ||
http_req::request::get(url, &mut res).unwrap_or_else(|error| { | ||
panic!("\n\n{}:\n{:?}", "Error".bright_red(), error); | ||
}); | ||
|
||
res | ||
http_req::request::get(url, &mut res) | ||
.map_err(|e| format!("Failed to download from {}: {:#?}", url, e))?; | ||
Ok(res) | ||
} | ||
|
||
pub fn get_body_string(url: &str) -> String { | ||
String::from_utf8(get_body(url)).unwrap() | ||
pub fn get_body_string(url: &str) -> Result<String, String> { | ||
let body = get_body(url)?; | ||
String::from_utf8(body).map_err(|e| format!("Failed to parse response as UTF-8: {:#?}", e)) | ||
} | ||
|
||
pub fn download_file(url: &str, file_path: &Path) { | ||
let body = get_body(url); | ||
pub fn download_file(url: &str, file_path: &Path) -> Result<(), String> { | ||
let body = get_body(url)?; | ||
|
||
let parent = file_path | ||
.parent() | ||
.ok_or_else(|| format!("Failed to get parent directory of {}", file_path.display()))?; | ||
|
||
fs::create_dir_all(parent) | ||
.map_err(|e| format!("Failed to create directory {}: {:#?}", parent.display(), e))?; | ||
|
||
fs::File::create(file_path) | ||
.map_err(|e| format!("Failed to create file {}: {:#?}", file_path.display(), e))? | ||
.write_all(&body) | ||
.map_err(|e| format!("Failed to write to file {}: {:#?}", file_path.display(), e))?; | ||
|
||
let mut f = fs::File::create(file_path).unwrap_or_else(|error| { | ||
panic!("\n\n{}:\n{:?}", "Error".bright_red(), error); | ||
}); | ||
f.write_all(&body).unwrap_or_else(|error| { | ||
panic!("\n\n{}:\n{:?}", "Error".bright_red(), error); | ||
}); | ||
Ok(()) | ||
} |
Oops, something went wrong.