tauri_api/
version.rs

1use semver::Version;
2use std::cmp::Ordering;
3
4/// Compare two semver versions
5pub fn compare(first: &str, second: &str) -> crate::Result<i32> {
6  let v1 = Version::parse(first)?;
7  let v2 = Version::parse(second)?;
8  match v1.cmp(&v2) {
9    Ordering::Greater => Ok(-1),
10    Ordering::Less => Ok(1),
11    Ordering::Equal => Ok(0),
12  }
13}
14
15/// Check if the "second" semver is compatible with the "first"
16pub fn is_compatible(first: &str, second: &str) -> crate::Result<bool> {
17  let first = Version::parse(first)?;
18  let second = Version::parse(second)?;
19  Ok(if second.major == 0 && first.major == 0 {
20    first.minor == second.minor && second.patch > first.patch
21  } else if second.major > 0 {
22    first.major == second.major
23      && ((second.minor > first.minor)
24        || (first.minor == second.minor && second.patch > first.patch))
25  } else {
26    false
27  })
28}
29
30/// Check if a the "other" version is a major bump from the "current"
31pub fn is_major(current: &str, other: &str) -> crate::Result<bool> {
32  let current = Version::parse(current)?;
33  let other = Version::parse(other)?;
34  Ok(other.major > current.major)
35}
36
37/// Check if a the "other" version is a minor bump from the "current"
38pub fn is_minor(current: &str, other: &str) -> crate::Result<bool> {
39  let current = Version::parse(current)?;
40  let other = Version::parse(other)?;
41  Ok(current.major == other.major && other.minor > current.minor)
42}
43
44/// Check if a the "other" version is a patch bump from the "current"
45pub fn is_patch(current: &str, other: &str) -> crate::Result<bool> {
46  let current = Version::parse(current)?;
47  let other = Version::parse(other)?;
48  Ok(current.major == other.major && current.minor == other.minor && other.patch > current.patch)
49}