some error "handling"

This commit is contained in:
Mylloon 2023-09-05 15:20:55 +02:00
parent 9be256eb51
commit 83108a20d7
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 24 additions and 9 deletions

View file

@ -27,7 +27,7 @@ async fn main() {
.enable_io() .enable_io()
.enable_time() .enable_time()
.build() .build()
.unwrap(); .expect("Can't build tokio runtime.");
loop { loop {
let start = Instant::now(); let start = Instant::now();

View file

@ -20,7 +20,7 @@ async fn search() -> Option<String> {
let latest_version = { let latest_version = {
let mut tag = github_latest_version.tag_name.chars(); let mut tag = github_latest_version.tag_name.chars();
tag.next(); tag.next();
Version::parse(tag.as_str()).unwrap() Version::parse(tag.as_str()).expect("Can't parse the latest version")
}; };
if let Some(current_version) = get_current_version() { if let Some(current_version) = get_current_version() {
@ -41,7 +41,10 @@ async fn search() -> Option<String> {
format!( format!(
"{}://{}{}", "{}://{}{}",
asset.browser_download_url.scheme(), asset.browser_download_url.scheme(),
asset.browser_download_url.host().unwrap(), asset
.browser_download_url
.host()
.expect("Can't get asset host"),
asset.browser_download_url.path() asset.browser_download_url.path()
) )
}) })
@ -68,9 +71,19 @@ fn get_current_version() -> Option<semver::Version> {
} }
Ok(paths) => { Ok(paths) => {
for path in paths { for path in paths {
let re = Regex::new(r"revanced-patches-(?P<version>\d+\.\d+\.\d+)\.jar").unwrap(); let re = Regex::new(r"revanced-patches-(?P<version>\d+\.\d+\.\d+)\.jar")
if let Some(caps) = re.captures(&path.unwrap().path().display().to_string()) { .expect("Can't build regex formula for revanced patches.");
return Some(Version::parse(&caps["version"]).unwrap()); if let Some(caps) = re.captures(
&path
.expect("Can't match patterns for revanced patches")
.path()
.display()
.to_string(),
) {
return Some(
Version::parse(&caps["version"])
.expect("No version found in the asset of revanced patches"),
);
} }
} }
None None

View file

@ -11,7 +11,7 @@ pub fn get_data_directory() -> PathBuf {
/// Download a file to data directory /// Download a file to data directory
pub async fn download_file(url: String) { pub async fn download_file(url: String) {
let source = reqwest::Url::parse(&url).unwrap(); let source = reqwest::Url::parse(&url).expect("Can't parse the URL");
let fname = source let fname = source
.path_segments() .path_segments()
.and_then(|segments| segments.last()) .and_then(|segments| segments.last())
@ -24,10 +24,12 @@ pub async fn download_file(url: String) {
let filepath = get_data_directory().join(fname); let filepath = get_data_directory().join(fname);
// Create file // Create file
let mut dest = File::create(filepath).unwrap(); let mut dest = File::create(&filepath)
.unwrap_or_else(|_| panic!("Can't create the file at {}.", filepath.display()));
// Write data to the file // Write data to the file
dest.write_all(&bytes).unwrap(); dest.write_all(&bytes)
.unwrap_or_else(|_| panic!("Can't write to file at {}.", filepath.display()));
} }
Err(..) => eprintln!("Can't download {}.", fname), Err(..) => eprintln!("Can't download {}.", fname),
}, },