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_time()
.build()
.unwrap();
.expect("Can't build tokio runtime.");
loop {
let start = Instant::now();

View file

@ -20,7 +20,7 @@ async fn search() -> Option<String> {
let latest_version = {
let mut tag = github_latest_version.tag_name.chars();
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() {
@ -41,7 +41,10 @@ async fn search() -> Option<String> {
format!(
"{}://{}{}",
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()
)
})
@ -68,9 +71,19 @@ fn get_current_version() -> Option<semver::Version> {
}
Ok(paths) => {
for path in paths {
let re = Regex::new(r"revanced-patches-(?P<version>\d+\.\d+\.\d+)\.jar").unwrap();
if let Some(caps) = re.captures(&path.unwrap().path().display().to_string()) {
return Some(Version::parse(&caps["version"]).unwrap());
let re = Regex::new(r"revanced-patches-(?P<version>\d+\.\d+\.\d+)\.jar")
.expect("Can't build regex formula for revanced patches.");
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

View file

@ -11,7 +11,7 @@ pub fn get_data_directory() -> PathBuf {
/// Download a file to data directory
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
.path_segments()
.and_then(|segments| segments.last())
@ -24,10 +24,12 @@ pub async fn download_file(url: String) {
let filepath = get_data_directory().join(fname);
// 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
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),
},