change Version from str to Version type and sort by each segment of version so as to properly sort versions like 1.10.0

This commit is contained in:
Dan Ballard 2022-12-16 12:16:49 -08:00
parent 2b3a26b1a3
commit 4d99e76218
1 changed files with 28 additions and 6 deletions

View File

@ -24,15 +24,34 @@ const BOT_NAME: &str = "Update Bot";
const LAST_OFFERED_KEY: &str = "profile.last_version_offered";
#[derive(Debug, Clone)]
struct Version {
major: u8,
minor: u8,
fix: u8,
path: PathBuf,
}
struct UpdateBot {
versions_dirs: Vec<PathBuf>,
versions_dirs: Vec<Version>,
latest_version: PathBuf,
version: String,
}
impl Version {
pub fn new(path: PathBuf) -> Self {
let vs = path.to_str().unwrap().strip_prefix(DIST_DIR).unwrap().trim_start_matches("/v");
let parts: Vec<&str> = vs.split(".").collect();
if parts.len() != 3 {
return Version{ major: 0, minor: 0, fix: 0, path: PathBuf::new()}
}
return Version{ major: parts[0].parse::<u8>().unwrap(), minor: parts[1].parse::<u8>().unwrap(), fix: parts[2].parse::<u8>().unwrap(), path: path}
}
}
impl UpdateBot {
pub fn new() -> Self {
let mut versions_dirs = vec![];
let mut versions_dirs: Vec<Version> = vec![];
for entry in
read_dir(Path::new(DIST_DIR)).expect(&format!("could not open '{}' dir", DIST_DIR))
{
@ -40,16 +59,19 @@ impl UpdateBot {
let path: PathBuf = entry.path();
if path.is_dir() {
println!("version: {}", path.to_str().unwrap());
versions_dirs.push(path);
//versions_dirs.push(path.clone());
let v = Version::new(path);
println!("{:?}", v);
versions_dirs.push(v);
}
}
if versions_dirs.len() == 0 {
panic!("no cwtch versions detected in {}!", DIST_DIR)
}
versions_dirs.sort();
versions_dirs.sort_unstable_by_key(|item| (item.major, item.minor, item.fix));
println!("sorted vd: {:?}", versions_dirs);
let latest_version = versions_dirs[versions_dirs.len() - 1].clone();
let version: String = latest_version
let version: String = latest_version.path
.strip_prefix(DIST_DIR)
.unwrap()
.to_str()
@ -57,7 +79,7 @@ impl UpdateBot {
.to_string();
let bot = UpdateBot {
versions_dirs: versions_dirs,
latest_version: latest_version,
latest_version: latest_version.path,
version: version,
};
println!("versions: {:?}\n", bot.versions_dirs);