1use std::cmp::Ordering;
2use std::ffi::OsStr;
3use std::str::FromStr;
4
5use regex::Regex;
6use semver::Version;
7
8pub trait OsStrExt {
9 fn to_str_direct(&self) -> &str;
10 fn to_string_direct(&self) -> String;
11}
12
13impl OsStrExt for OsStr {
14 fn to_str_direct(&self) -> &str {
15 self.to_str().unwrap()
16 }
17
18 fn to_string_direct(&self) -> String {
19 self.to_str().unwrap().to_string()
20 }
21}
22
23pub trait VersionCompareTrait {
24 fn cmp_version(&self, version: &str) -> Option<Ordering>;
25}
26
27impl VersionCompareTrait for String {
28 fn cmp_version(&self, version: &str) -> Option<Ordering> {
29 let regex = Regex::new(r"^v").unwrap();
30
31 let self_version = Version::from_str(®ex.replace(self, ""));
32 let comparing_version = Version::from_str(®ex.replace(version, ""));
33
34 if self_version.is_err() || comparing_version.is_err() {
35 return Some(Ordering::Equal);
36 }
37
38 self_version
39 .unwrap()
40 .partial_cmp(&comparing_version.unwrap())
41 }
42}