1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
use std::{
collections::HashMap,
fs::{copy, read_dir},
};
use crate::error::{EType, MLErr, MLE};
/// # Errors
pub fn get_cached_versions(path: &str) -> MLE<HashMap<String, String>> {
let mut versions: HashMap<String, String> = HashMap::new();
for file in read_dir(path).map_err(|_| MLErr::new(EType::IoError, "readdir"))? {
let path = file.map_err(|_| MLErr::new(EType::IoError, "file"))?.path();
if path.is_file() && path.extension().ok_or(MLErr::new(EType::IoError, "ext"))? == "jar" {
let pathstr = path.to_str().ok_or(MLErr::new(EType::IoError, "path"))?;
let namesplit: Vec<&str> = pathstr.split('.').collect();
versions.insert(
String::from(namesplit[namesplit.len() - 2]),
String::from(pathstr),
);
}
}
Ok(versions)
}
/// # Errors
pub fn copy_cached_version(version_path: &str, download_path: &str) -> MLE<()> {
let versplit: Vec<&str> = version_path.split('/').collect();
let download =
format!("{}/{}", download_path, versplit[versplit.len() - 1]);
copy(version_path, download).map_err(|err| MLErr::new(EType::IoError, &err.to_string()))?;
Ok(())
}
|